/ src / modules / MouseWithoutBorders / App / Helper / FormDot.cs
FormDot.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Drawing;
 7  using System.Drawing.Imaging;
 8  using System.Windows.Forms;
 9  
10  using static MouseWithoutBorders.NativeMethods;
11  
12  namespace MouseWithoutBorders
13  {
14      internal sealed partial class FormDot : System.Windows.Forms.Form
15      {
16          private int left;
17          private int top;
18  
19          internal FormDot()
20          {
21              InitializeComponent();
22              Cursor = CreateDotCursor();
23          }
24  
25          public void SetPosition(int left, int top)
26          {
27              this.left = left - 2;
28              this.top = top - 2;
29          }
30  
31          private Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
32          {
33              IconInfo tmp = default;
34              tmp.xHotspot = xHotSpot;
35              tmp.yHotspot = yHotSpot;
36              tmp.fIcon = false;
37              tmp.hbmColor = bmp.GetHbitmap();
38              tmp.hbmMask = bmp.GetHbitmap();
39              return new Cursor(NativeMethods.CreateIconIndirect(ref tmp));
40          }
41  
42          private Cursor CreateDotCursor()
43          {
44              Bitmap bitmap = new(32, 32, PixelFormat.Format24bppRgb);
45              bitmap.MakeTransparent(Color.Black);
46              Graphics g = Graphics.FromImage(bitmap);
47              g.DrawLine(Pens.Gray, 0, 0, 1, 1);
48              Cursor c = CreateCursor(bitmap, 0, 0);
49              bitmap.Dispose();
50              return c;
51          }
52  
53          private void FormDot_FormClosing(object sender, FormClosingEventArgs e)
54          {
55              if (e.CloseReason == CloseReason.UserClosing)
56              {
57                  e.Cancel = true;
58              }
59          }
60  
61          private void FormDot_Shown(object sender, EventArgs e)
62          {
63              MoveMe();
64          }
65  
66          private void MoveMe()
67          {
68              Left = left;
69              Top = top;
70              Width = 4;
71              Height = 4;
72          }
73  
74          private void FormDot_Click(object sender, EventArgs e)
75          {
76              IntPtr foreGroundWindow = NativeMethods.GetForegroundWindow();
77              Program.FormHelper.SendLog($"{nameof(FormDot_Click)}.Foreground window ({foreGroundWindow}) is " + (foreGroundWindow == Handle ? string.Empty : "not ") + $"the dot form: {Program.DotForm.Handle}.");
78          }
79  
80          private void FormDot_VisibleChanged(object sender, EventArgs e)
81          {
82              MoveMe();
83          }
84      }
85  }