首页 > 解决方案 > 如何在不使用继承类的情况下将形状绘制为对象

问题描述

我有一个类来绘制一些带有鼠标事件和一些属性的形状。如果我使用继承类(继承控件),则一切正常,否则没有什么可显示的。因为我将位置和大小的 myclass 设置为使用鼠标事件。如何在不使用继承类的情况下将形状作为对象。

Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class Shape





Public x1 As Integer
    Public x2 As Integer
    Public y1 As Integer
    Public y2 As Integer
    Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, ByRef g As Graphics)
        MyBase.New()
        Me.x1 = x1 : Me.x2 = x2 : Me.y1 = y1 : Me.y2 = y2
        draw(g)

        Me.Left = x1
        Me.Top = y1
        Me.Width = x2 - x1
        Me.Height = y2 - y1

    End Sub
    Public Sub draw(ByRef egraphics As Graphics)
        Dim _mybrush As Brush ' brush to fill vertex
        _mybrush = New System.Drawing.SolidBrush(Color.White)
        Dim _mypen As New Pen(Color.Black, 5) ' make pen to draw
        _mypen.DashStyle = DashStyle.Solid
        egraphics.FillRectangle(_mybrush, New Rectangle(x1, y1, (x2 - x1), (y2 - y1))) ' dont change between this line and next line if change you can not to see border
        egraphics.DrawRectangle(_mypen, New Rectangle(x1, y1, (x2 - x1), (y2 - y1))) ' dont change between this line and next line if change you can not to see border
    End Sub

End Class

标签: vb.netclassinheritancecontrolsshape

解决方案


推荐阅读