Paint is a few pixels off...

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
When drawing graphics at a particular point, using a control's location or PointToScreen function, there are a few pixels difference between the actual locations that the graphics object appears on the screen when running the code within the IDE and in the compiled version. Is this the norm or is there a problem with my logic here? I'm just making a label flash when it gets clicked...

In an inherited form, there is a panel(NavPanel1) that contains a label(UpLabel)...
VB.NET:
    CFlash(New Rectangle(NavPanel1.PointToScreen(New Point(UpLabel.Location)), UpLabel.Size))

And the base class form method that handles this call...
VB.NET:
    Friend Sub CFlash(ByRef rect As Rectangle)
        G = Me.CreateGraphics
        G.FillRectangle(Brushes.White, rect)    '   Paint the label white
        System.Threading.Thread.Sleep(80)       '   Wait briefly
        Me.Refresh()                            '   Remove the white rectangle
    End Sub

When running in the IDE, everything is perfect. In the compiled version, It paints the white rectangle about 3 or 4 pixels too high & to the left. I could compensate for this by adding values to rect's location, but I figured that I messed up somewhere. ????
 
I don't really get why you need to use PointToScreen or even use the paint method for this. You could simply set the BackColor property of the label to white and back to transparent again.

But to answer your question, PointToScreen will tell you the location of the label's position relatively to the screen, not the form. However, the graphics object expects a position relative to self. The quick and dirty solution would probably be this :

VB.NET:
CFlash(New Rectangle(me.PointToClient( NavPanel1.PointToScreen(New Point(UpLabel.Location))), UpLabel.Size))

Also, you should use a Timer component instead of having your main UI thread sleep. 80 milisecs is probably insignificant, but you may notice that nothing responds while the label is white.
 
Some of the labels have an image for their background so I opted to just paint over them momentarily. Your solution worked beautifully! Thank you for taking the time to help me out !
 
Back
Top