none
自定义控件画长方形的方法无效 RRS feed

  • 问题

  •  Public Overloads Sub Rectangle(ByVal X1 As Integer, ByVal X2 As Integer, ByVal Y1 As Integer, ByVal Y2 As Integer)
      Dim Rect As New Rectangle
      Rect.X = X1
      Rect.Y = Y1
      If X2 > X1 Then '计算长与宽
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X2 - X1
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X2 - X1
       End If
      Else
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X1 - X2
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X1 - X2
       End If
      End If
      GDI.DrawRectangle(GPEN, Rect)
     End Sub

    这个是控件里的画长方形的方法。

    GDI是Graphics的实例对象,绑定了usercontrol的一个picturebox的image

    调用的时候这样调用

       GdiDrawer2.Pen = Pens.Black
       GdiDrawer2.Brush = Brushes.Black
       GdiDrawer2.Rectangle(2, 2, 80, 80)

    pen和brush属性对应graphics的pen和brush

    为什么就画不出来?而一个画圆的方法却能画出来。

    2010年8月23日 2:38

答案

全部回复

  • GDI.DrawRectangle(GPEN, Rect)改为me.invalidate

    画图放到paint事件


    http://feiyun0112.cnblogs.com/
    2010年8月24日 1:28
    版主
  • 画圆、画线、输出文字都行 为什么画长方形不行,还是我的net framework坏了?

    2010年8月24日 5:28
  • 你好:

    你的画笔的颜色和所画的线条颜色重合了吧。请检查。

    希望对你有所帮助。

    2010年8月24日 6:49
    版主
  • 还是没用。背景设置的白色,画笔黑色,但没图像
    2010年8月25日 8:51
  • 2010年8月25日 9:09
    版主
  • 要全部发么?太多了。168行哪

    Public Class GDIDrawer
     '变量定义
     Private GDI As Graphics
     Private BMP As Bitmap
     Private GPEN As Pen
     Private GBRUSH As Brush
     Private GFONTFAM As String
     Private GFONTSZ As Integer
     Private GFONTSTYLE As FontStyle
     Private First As Boolean
     '初始化
     Private Sub GDIDrawer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      pbxMain.Top = 0 '画布
      pbxMain.Left = 0
      pbxMain.Height = Me.Height
      pbxMain.Width = Me.Width
      GFONTSZ = 9
      GFONTFAM = "宋体"
      GFONTSTYLE = FontStyle.Regular
     End Sub
     Public Sub New()

      ' 此调用是 Windows 窗体设计器所必需的。
      InitializeComponent()

      ' 在 InitializeComponent() 调用之后添加任何初始化。
      BMP = New Bitmap(Me.Width, Me.Height)
      GDI = Graphics.FromImage(BMP)
      GPEN = New Pen(Color.Black, 1)
      pbxMain.Image = BMP
      GFONTSZ = 9
      GFONTFAM = "宋体"
      GFONTSTYLE = FontStyle.Regular
     End Sub
     '方法定义
     Public ReadOnly Property Bitmap() As Bitmap '只读属性 位图
      Get
       Return BMP
      End Get
     End Property
     Public Sub PSet(ByVal X As Integer, ByVal Y As Integer, ByVal Color As System.Drawing.Color) '画点 PSET为了与VB6表示兼容 其实就是API的封装(CLR的方法)
      BMP.SetPixel(X, Y, Color)
     End Sub
     Public Function Point(ByVal X As Integer, ByVal Y As Integer) As System.Drawing.Color '从点取色 Point 为了兼容VB6
      Return BMP.GetPixel(X, Y)
     End Function
     Public Overloads Sub Circle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) '为VB6.0保留的方法名称,但内容不相同
      Dim Rect As New RectangleF
      Rect.X = X1
      Rect.Y = Y1
      If X2 > X1 Then '计算长与宽
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X2 - X1
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X2 - X1
       End If
      Else
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X1 - X2
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X1 - X2
       End If
      End If
      GDI.DrawEllipse(GPEN, Rect)
     End Sub
     Public Overloads Sub Circle(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer) '这个重载方法是为VB6所保留的 前两个参数定圆心,后一个半径
      Circle(X - Int(Math.Sqrt(Width * Width)), Y - Int(Math.Sqrt(Width * Width)), Int(Math.Sqrt(Width * Width)), Int(Math.Sqrt(Width * Width))) '用勾股定理求(X2,Y2),再调用上个重载方法  引用方法要把原写清楚
     End Sub
     Public Overloads Sub Circle(ByVal Rect As RectangleF) '直接调用GDI+的绘图方法
      GDI.DrawEllipse(GPEN, Rect)
     End Sub
     Public Overloads Sub Rectangle(ByVal X1 As Integer, ByVal X2 As Integer, ByVal Y1 As Integer, ByVal Y2 As Integer)
      Dim Rect As New Rectangle
      Rect.X = X1
      Rect.Y = Y1
      If X2 > X1 Then '计算长与宽
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X2 - X1
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X2 - X1
       End If
      Else
       If Y2 > Y1 Then
        Rect.Height = Y2 - Y1
        Rect.Width = X1 - X2
       Else
        Rect.Height = Y1 - Y2
        Rect.Width = X1 - X2
       End If
      End If
      GDI.DrawRectangle(GPEN, Rect)
     End Sub
     Public Sub TextOut(ByVal X As Integer, ByVal Y As Integer, ByVal Text As String) '输出文字(没有使用print名称,因为print与此有很大不同)
      GDI.DrawString(Text, Font, GBRUSH, X, Y)
     End Sub

     Public Overloads Sub Arc(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal StartAngle As Integer, ByVal SweepAngle As Integer) '画弧(椭圆)
      GDI.DrawArc(GPEN, X, Y, Width, Height, StartAngle, SweepAngle)
     End Sub
     Public Overloads Sub Arc(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal StartAngle As Integer, ByVal SweepAngle As Integer) '画正圆周上的弧
      GDI.DrawArc(GPEN, X, Y, Width, Width, StartAngle, SweepAngle)
     End Sub
     Public Overloads Sub Arc(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Angle As Integer) '以水平线(0°角)为起角画正圆周上的弧
      GDI.DrawArc(GPEN, X, Y, Width, Width, 0, Angle)
     End Sub
     Public Overloads Sub InsertBitmap(ByVal Bitmap As Bitmap, ByVal X As Integer, ByVal Y As Integer) '插入位图
      Dim P As PointF
      P.X = X
      P.Y = Y
      GDI.DrawImage(Bitmap, P)
     End Sub
     Public Overloads Sub InsertBitmap(ByVal Bitmap As Bitmap, ByVal X As Integer, ByVal Y As Integer, ByVal srcRect As Rectangle)
      GDI.DrawImage(Bitmap, X, Y, srcRect, GraphicsUnit.Pixel)
     End Sub
     Public Overloads Sub InsertBitmap(ByVal Bitmap As Bitmap, ByVal X As Integer, ByVal Y As Integer, ByVal srcRect As Rectangle, ByVal GraphUnit As GraphicsUnit)
      GDI.DrawImage(Bitmap, X, Y, srcRect, GraphUnit)
     End Sub
     Public Overloads Sub InsertBitmap(ByVal Bitmap As Bitmap, ByVal X As Integer, ByVal Y As Integer, ByVal srcRect As Rectangle, ByVal destRect As Rectangle)
      GDI.DrawImage(Bitmap, X, Y, destRect.Width, destRect.Height)
     End Sub

     

     ' 属性定义
     Public Property Pen() As Pen '当前GDI+的钢笔对象
      Get
       Return GPEN
      End Get
      Set(ByVal value As Pen)
       GPEN = value
      End Set
     End Property
     Public Property Brush() As Brush '当前GDI+的刷子对象
      Get
       Return GBRUSH
      End Get
      Set(ByVal value As Brush)
       GBRUSH = value
      End Set
     End Property
     Public Overrides Property Font() As Font 'GDI+的字体对象 部分不可用 颜色请设置Brush对象
      Get
       If First = False Then '第一次 If 代码段是为了防止NullReferenceException空引用异常,误删除,否则窗体设计器加载不了
        Return New Font(GFONTFAM, 9, GFONTSTYLE)
        First = True
       Else
        Return New Font(GFONTFAM, GFONTSZ, GFONTSTYLE)
       End If
      End Get
      Set(ByVal value As Font)
       GFONTFAM = value.FontFamily.Name
       GFONTSZ = value.Size
       GFONTSTYLE = value.Style
      End Set
     End Property
    End Class

    2010年8月26日 0:56
  • 没看到paint事件画图代码


    http://feiyun0112.cnblogs.com/
    2010年8月26日 2:07
    版主
  • 加了paint还是画不出来
    2010年8月26日 13:10
  • Graphics用paint事件的e.Graphics
    http://feiyun0112.cnblogs.com/
    2010年8月27日 3:10
    版主