Skip to content

Commit

Permalink
Fix dragging bug -- by github copilot
Browse files Browse the repository at this point in the history
  • Loading branch information
isaleh-sb committed Sep 30, 2024
1 parent e45609c commit d1c8446
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 55 deletions.
Binary file modified InstallForgeProject/OpenAdhan.ifp
Binary file not shown.
78 changes: 23 additions & 55 deletions OpenAdhanForWindowsX/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -139,7 +133,6 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
e.Cancel = true;
this.Hide();
this.timer1.Stop();
this.dragCheckTimer.Stop();
}

}
Expand Down Expand Up @@ -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;
}

}
Expand Down

0 comments on commit d1c8446

Please sign in to comment.