Reputation: 690
I am getting X and Y coordinates from an external controller through its own SDK.
So, I want to transform this coordinates in an circle translucent and simulate a mouse cursor.
I have the following code, but I only get draw translucent circles, and I cannot "erase" previous circles.
I would like draw translucent circles and erase them when I draw the next circle. I should draw some kind of transition between a coordinates and the following coordinates for simulating "movement". Another issues I have found, I cannot draw circle over standard components as buttons, text box, etc…
//...
System.Drawing.Graphics g = this.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
System.Drawing.Color translucentYellow = System.Drawing.Color.FromArgb(128, Color.Yellow);
System.Drawing.SolidBrush aBrush = new System.Drawing.SolidBrush(translucenYellow);
g.CompositingQuality = system.Drawing.Drawing2D.CompositingQuality.GammaCorrected;
g.FillEllipse(aBrush, X, Y, width, height);
//.….
Upvotes: 2
Views: 182
Reputation: 54433
Don't paint a cursor, when the system does it so much better for you.
Ideally all you'd need to do is:
Cursor = new Cursor("D:\\circle1.cur");
Unfortunately this will not work with many versions of cur files. To be precise anything over 32x32 pixels and with colors.
So you will want to use a more flexible routine, which I found on this post, see below..!
Use it like this
Cursor = CreateCursorNoResize(bmp, 16, 16);
And set the cursor position like this:
Cursor.Position = new Point(yourX, yourY);
whenever the controller comes up with a change..
Here is the ever so slightly modified routine:
using System.Runtime.InteropServices;
// ..
public struct IconInfo
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
[DllImport("user32.dll")]
public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
public static Cursor CreateCursorNoResize(Bitmap bmp, int xHotSpot, int yHotSpot)
{
IntPtr ptr = bmp.GetHicon();
IconInfo tmp = new IconInfo();
GetIconInfo(ptr, ref tmp);
tmp.xHotspot = xHotSpot;
tmp.yHotspot = yHotSpot;
tmp.fIcon = false;
ptr = CreateIconIndirect(ref tmp);
return new Cursor(ptr);
}
Notes:
Cursor found on rw-designer:
Upvotes: 1
Reputation: 171178
Draw a static ellipse to a simple control such as a Panel
. Move that panel over the screen. That way you can control overlapping other windows and controls. You also don't have to redraw the ellipse all the time.
If you want to overlap other windows or applications you need to draw this ellipse into a Form
with TopMost = true
. You can remove the borders from the form.
You can set transparency for a Form
as well.
Upvotes: 1