Results 1 to 7 of 7

Thread: [RESOLVED] Graphics wont stick

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [RESOLVED] Graphics wont stick

    I want to put an outline around each control on a panel, as a test case I used some checkboxes. It draws the outlines fine, but if I mouseover the checkboxes, they dont repaint. I cant see anything wrong with this (or I might have code blindness). Can anyone see what's wrong?

    VB Code:
    1. Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
    2.     Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
    3.     Dim MyPanel As New Panel
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim d As IntPtr = New IntPtr(GetWindowDC(MyPanel.Handle.ToInt32))
    6.         Dim g As Graphics = Graphics.FromHdc(d)
    7.         PaintCon(MyPanel, g)
    8.         g.Dispose()
    9.     End Sub
    10.     Private Sub PaintCon(ByVal Con As Control, ByVal g As Graphics)
    11.         For Each ctl As Control In Con.Controls
    12.             If ctl.Controls.Count > 0 Then
    13.                 PaintCon(ctl, g)
    14.             Else
    15.                 If ctl.GetType.ToString.ToLower = "system.windows.forms.checkbox" Then
    16.                     Dim ConRect As New Rectangle(MyPanel.PointToClient(ctl.Parent.PointToScreen(ctl.Location)), ctl.Size)
    17.                     g.DrawRectangle(Pens.Black, ConRect)
    18.                     RemoveHandler Con.Paint, AddressOf ConPaint
    19.                     AddHandler Con.Paint, AddressOf ConPaint
    20.                 End If
    21.             End If
    22.         Next
    23.     End Sub
    24.     Private Sub ConPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    25.         Dim con As Control = DirectCast(sender, Control)
    26.         Dim ConRect As New Rectangle(MyPanel.PointToClient(con.Parent.PointToScreen(con.Location)), con.Size)
    27.         e.Graphics.DrawRectangle(Pens.Black, ConRect)
    28.     End Sub

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: Graphics wont stick

    Moveing the mouse over it's not going to fire any paint event's. Only something that causes a refresh, like moving another window over, will repaint.

    Put the .Refresh() method call in the mousemove events for the controls.
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Graphics wont stick

    I'm assuming your "ConPaint" method is supposed to be the paint event that is raised when the control is repainted? If so you need to add the paint handler for the controls to the method.

    i.e.
    VB Code:
    1. Private Sub conPaint(sender as object, e as eventargs) [COLOR=Red]Handles CheckBox1.Paint[/COLOR]
    2.  
    3. End Sub
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Graphics wont stick

    If I mouseover the checkbox, the outline I drew disappears. If I drag another window over the form, the outlines also disappear.

    Isnt this sufficient to call the paint event?

    VB Code:
    1. AddHandler Con.Paint, AddressOf ConPaint

  5. #5
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: Graphics wont stick

    I took the libertry of creating a sample project of how I would perform this task.
    I would make a control that inherits from a Panel:

    VB Code:
    1. ''' <summary>
    2. ''' A Panel control that draws a rectangle around checkboxes
    3. ''' </summary>
    4. ''' <remarks></remarks>
    5. Public Class BullDogPanel
    6.     Inherits Panel
    7.  
    8.     Private m_drawRectangleAroundCheckBox As Boolean
    9.  
    10.     <System.ComponentModel.Category("Appearance")> _
    11.     <System.ComponentModel.DisplayName("Draws a rectangle around any checkboxs on 'Me'")> _
    12.     Public Property DrawRectangleAroundCheckBox() As Boolean
    13.         Get
    14.             Return Me.m_drawRectangleAroundCheckBox
    15.         End Get
    16.         Set(ByVal value As Boolean)
    17.             Me.m_drawRectangleAroundCheckBox = value
    18.             Me.Invalidate()
    19.         End Set
    20.     End Property
    21.  
    22.     Private Sub BullDogPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    23.  
    24.         If Me.m_drawRectangleAroundCheckBox Then
    25.             For Each control As Control In Me.Controls
    26.                 If TypeOf control Is CheckBox Then
    27.                     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(control.Location.X - 1, control.Location.Y - 1, _
    28.                                              control.ClientRectangle.Width + 1, control.ClientRectangle.Height + 1))
    29.                 End If
    30.             Next control
    31.         End If
    32.  
    33.     End Sub
    34.  
    35. End Class

    The sample is VS2005, so you may or may not be able to read!


    Hope this helps


    Julian
    Attached Files Attached Files

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Graphics wont stick

    Sheesh, thats fantastic!

    You guys do things in 5 minutes that it takes me a whole day to build (and which usually doesnt work)! Thanks so much for doing that.

    Your code is miles better, but looking at my original code, its not obvious to me what I was doing wrong? Just for my own learning, I guess I cant point at a paint event with a handler, was that the problem?

  7. #7
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: Graphics wont stick

    Hi, no problem.

    I did manage to duplicate the problem, but it was not immediately obvious why it was not working, I thought I would spend the time re doing rather than looking for the problem!!

    I have changed the code slightly, you should really override the OnPaint event, its better practice:



    VB Code:
    1. ''' <summary>
    2. ''' A Panel control that draws a rectangle around checkboxes
    3. ''' </summary>
    4. ''' <remarks></remarks>
    5. Public Class BullDogPanel
    6.     Inherits Panel
    7.  
    8.     Private m_drawRectangleAroundCheckBox As Boolean
    9.  
    10.     <System.ComponentModel.Category("Appearance")> _
    11.     <System.ComponentModel.DisplayName("Draws a rectangle around any checkboxs on 'Me'")> _
    12.     Public Property DrawRectangleAroundCheckBox() As Boolean
    13.         Get
    14.             Return Me.m_drawRectangleAroundCheckBox
    15.         End Get
    16.         Set(ByVal value As Boolean)
    17.             Me.m_drawRectangleAroundCheckBox = value
    18.             Me.Invalidate()
    19.         End Set
    20.     End Property
    21.  
    22.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    23.  
    24.         If Me.m_drawRectangleAroundCheckBox Then
    25.             For Each control As Control In Me.Controls
    26.                 If TypeOf control Is CheckBox Then
    27.                     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(control.Location.X - 1, control.Location.Y - 1, _
    28.                                              control.ClientRectangle.Width + 1, control.ClientRectangle.Height + 1))
    29.                 End If
    30.             Next control
    31.         End If
    32.         MyBase.OnPaint(e)
    33.  
    34.     End Sub
    35.  
    36. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width