I'm trying to draw a string on the screen (attached to the mouse cursor) using C#, and the string value and its location change with the mouse movement. The problem here is when hovering the mouse, the string is drawn multiple times in multiple locations and then disappears after a while (< 1 second but it's remarkable). Is there any way to make it stable and not behave like that?Image I'm aiming to make it smooth as it behaves in this video: 30 seconds Video This is part of the code I used:
System.Drawing.Point drawPoint = new System.Drawing.Point(mouse_click_x, mouse_click_y);Graphics g = Graphics.FromHdc(desktopPtr);
g.DrawString(oState1.SmartTagText(oPath), drawFont, drawBrush, drawPoint);
It sounds like your core issue is not restoring the screen area before updating the label position. Therefore, your old rendering of text still exists. You need to restore that screen area to how it was before you rendered it.
For a quick example, create a console application and make this your Main method...
You can exit the sample console application by hitting [Enter] once running.
The
ScreenPositionLabelclass is as follows (with inline comments)......and the above makes use of a ClipRegion class, which is used to save a portion of the screen as an image, which you set before rendering text. You also redraw this image when the cursor position is updated, so that you restore the screen to how it would be without text. Then you draw the text at the updated position.
The ClipRegion class is...
It is possible for the label to disappear when something else grabs focus. If the mouse is simply moved, the label will reappear.