MainForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Drawing.Imaging;
  2. using DummyCursor.Spy;
  3. using Timer = System.Threading.Timer;
  4. namespace DummyCursor
  5. {
  6. public partial class MainForm : Form
  7. {
  8. private const int DummySize = 128;
  9. private nint _hWndWatch;
  10. private nint _hWndShowDummy;
  11. private Form _dummy = new Form()
  12. {
  13. FormBorderStyle = FormBorderStyle.None,
  14. ControlBox = false,
  15. Width = DummySize,
  16. Height = DummySize,
  17. BackColor = Color.White,
  18. TransparencyKey = Color.White,
  19. MinimumSize = Size.Empty,
  20. TopMost = true,
  21. BackgroundImageLayout = ImageLayout.Center
  22. };
  23. public MainForm()
  24. {
  25. InitializeComponent();
  26. }
  27. private void ChooseWatchingWindowButton_Click(object sender, EventArgs e)
  28. {
  29. SpyAgent sa = null;
  30. sa = new SpyAgent(w =>
  31. {
  32. _hWndWatch = w.Handle;
  33. ChooseWatchingWindowButton.Text = w.Caption;
  34. sa!.EndSpying();
  35. });
  36. sa.BeginSpying();
  37. }
  38. private void ChooseShowDummyOnWindowButton_Click(object sender, EventArgs e)
  39. {
  40. SpyAgent sa = null;
  41. sa = new SpyAgent(w =>
  42. {
  43. _hWndShowDummy = w.Handle;
  44. ChooseShowDummyOnWindowButton.Text = w.Caption;
  45. sa!.EndSpying();
  46. });
  47. sa.BeginSpying();
  48. }
  49. private void ActiveCheckButton_CheckedChanged(object sender, EventArgs e)
  50. {
  51. _dummy.Visible = ActiveCheckButton.Checked;
  52. if (_dummy.Visible)
  53. {
  54. var oldBmp = _dummy.BackgroundImage;
  55. using var cur = Icon.FromHandle(Cursor.Current.Handle).ToBitmap();
  56. var newBmp = new Bitmap(DummySize, DummySize, PixelFormat.Format32bppArgb);
  57. using var grp = Graphics.FromImage(newBmp);
  58. grp.DrawImage(cur, DummySize / 2f - 0, DummySize / 2f - 1, new RectangleF(0, 0, cur.Width, cur.Height), GraphicsUnit.Pixel);
  59. grp.DrawLine(Pens.White, 0, DummySize / 2, DummySize, DummySize / 2);
  60. grp.DrawLine(Pens.Black, 0, DummySize / 2 - 1, DummySize, DummySize / 2 - 1);
  61. grp.DrawLine(Pens.Black, 0, DummySize / 2 + 1, DummySize, DummySize / 2 + 1);
  62. grp.DrawLine(Pens.White, DummySize / 2, 0, DummySize / 2, DummySize);
  63. grp.DrawLine(Pens.Black, DummySize / 2 - 1, 0, DummySize / 2 - 1, DummySize);
  64. grp.DrawLine(Pens.Black, DummySize / 2 + 1, 0, DummySize / 2 + 1, DummySize);
  65. _dummy.BackgroundImage = newBmp;
  66. oldBmp?.Dispose();
  67. timer1.Enabled = true;
  68. }
  69. else
  70. {
  71. timer1.Enabled = false;
  72. }
  73. }
  74. private void timer1_Tick(object sender, EventArgs e)
  75. {
  76. if (_hWndWatch == 0) return;
  77. if (_hWndShowDummy == 0) return;
  78. //check mouse in watching window
  79. var wndWatch = new SpiedWindow(_hWndWatch);
  80. var rcWatch = wndWatch.ClientArea;
  81. var mPos = MousePosition;
  82. if (rcWatch.Contains(mPos) == false)
  83. {
  84. return;
  85. }
  86. int bp = 0;
  87. //calc % of w and h
  88. var wcx = mPos.X - rcWatch.X;
  89. var wcy = mPos.Y - rcWatch.Y;
  90. var percentX = (float)wcx / rcWatch.Width;
  91. var percentY = (float)wcy / rcWatch.Height;
  92. //calc show target x and y
  93. var wndShow = new SpiedWindow(_hWndShowDummy);
  94. var rcShow = wndShow.Area;
  95. var showX = rcShow.X + rcShow.Width * percentX - DummySize / 2f;
  96. var showY = rcShow.Y + rcShow.Height * percentY - DummySize / 2f;
  97. //move dummy to show
  98. _dummy.Location = new Point((int)showX, (int)showY);
  99. }
  100. }
  101. }