From d1c844645bcdcb988917ac0f0037d58188a82c5e Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Sun, 29 Sep 2024 21:09:09 -0700 Subject: [PATCH] Fix dragging bug -- by github copilot --- InstallForgeProject/OpenAdhan.ifp | Bin 3783 -> 3789 bytes OpenAdhanForWindowsX/Form1.cs | 78 +++++++++--------------------- 2 files changed, 23 insertions(+), 55 deletions(-) diff --git a/InstallForgeProject/OpenAdhan.ifp b/InstallForgeProject/OpenAdhan.ifp index 8ea47bf8a072ef82859718102479a876110da434..a27dc67a097eed1e2f437d7ef6c867a4fae7514c 100644 GIT binary patch delta 19 acmX>udscRXHyfL-p`M|h!Der^8V&$HZ3TG% delta 12 TcmX>rdt7#dH{0ewwi*rqAvgqH diff --git a/OpenAdhanForWindowsX/Form1.cs b/OpenAdhanForWindowsX/Form1.cs index e6fffb4..9c2bd16 100644 --- a/OpenAdhanForWindowsX/Form1.cs +++ b/OpenAdhanForWindowsX/Form1.cs @@ -7,6 +7,15 @@ namespace OpenAdhanForWindowsX { public partial class Form1 : Form { + private const int WM_NCLBUTTONDOWN = 0xA1; + private const int HTCAPTION = 0x2; + + [DllImport("user32.dll")] + private static extern bool ReleaseCapture(); + + [DllImport("user32.dll")] + private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); + bool closeOnExit = false; ContextMenu contextMenu1; MenuItem exitMenuItem; @@ -17,10 +26,6 @@ public partial class Form1 : Form private RegistrySettingsHandler registryHandler; - private bool isDragging = false; - private Point lastLocation; - private Timer dragCheckTimer; - public Form1() { InitializeComponent(); @@ -32,16 +37,7 @@ public Form1() //sunMoonAnimation.ToggleDebugMode(true); CustomizeMenuStrip(); - - // Initialize and start the drag check timer - dragCheckTimer = new Timer(); - dragCheckTimer.Interval = 50; // Check every 50 milliseconds - dragCheckTimer.Tick += DragCheckTimer_Tick; - dragCheckTimer.Start(); - - // Add event handlers - this.MouseMove += Form1_MouseMove; - this.MouseUp += Form1_MouseUp; + menuStrip1.MouseDown += new MouseEventHandler(MenuStrip1_MouseDown); registryHandler = new RegistrySettingsHandler(false); if (registryHandler.SafeLoadBoolRegistryValue(RegistrySettingsHandler.bismillahOnStartupKey)) @@ -124,8 +120,6 @@ private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) this.WindowState = FormWindowState.Normal; this.Activate(); // Brings the form to the front. notifyIcon.Visible = true; - this.timer1.Start(); - this.dragCheckTimer.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) @@ -139,7 +133,6 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e) e.Cancel = true; this.Hide(); this.timer1.Stop(); - this.dragCheckTimer.Stop(); } } @@ -280,55 +273,30 @@ private void button1_MouseClick(object sender, MouseEventArgs e) this.Close(); } - private void DragCheckTimer_Tick(object sender, EventArgs e) + private void MenuStrip1_MouseDown(object sender, MouseEventArgs e) { - if (!isDragging && Control.MouseButtons == MouseButtons.Left) + if (e.Button == MouseButtons.Left && !IsOverMenuItem(e.Location)) { - Point cursorPos = this.PointToClient(Cursor.Position); - if (menuStrip1.Bounds.Contains(cursorPos) && !IsOverMenuItem(cursorPos)) - { - StartDragging(cursorPos); - } + StartDragging(); } } - private bool IsOverMenuItem(Point point) + private void StartDragging() { - if (button1.Bounds.Contains(point)) - { - return true; - } - - // Convert point to menuStrip coordinates - Point menuStripPoint = menuStrip1.PointToClient(this.PointToScreen(point)); - - // Check if the point is over any ToolStripItem - ToolStripItem item = menuStrip1.GetItemAt(menuStripPoint); - return item != null; - } - - private void StartDragging(Point startPoint) - { - isDragging = true; - lastLocation = startPoint; - this.Capture = true; + ReleaseCapture(); + SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } - private void Form1_MouseMove(object sender, MouseEventArgs e) + private bool IsOverMenuItem(Point location) { - if (isDragging) + foreach (ToolStripMenuItem item in menuStrip1.Items) { - this.Location = new Point( - (this.Location.X - lastLocation.X) + e.X, - (this.Location.Y - lastLocation.Y) + e.Y); - this.Update(); + if (item.Bounds.Contains(location)) + { + return true; + } } - } - - private void Form1_MouseUp(object sender, MouseEventArgs e) - { - isDragging = false; - this.Capture = false; + return false; } }