將標籤添加到您的窗體並連接其MouseMove()和QueryContinueDrag()事件。使用WindowFromPoint()和GetAncestor()API獲取包含光標位置的主窗口句柄,然後使用ScreenToClient()API將屏幕座標轉換爲該窗體的客戶端座標。運行應用程序並將標籤拖到您的目標應用程序中的旋鈕上。標題欄應相對於應用程序的當前鼠標位置的客戶COORDS更新結束後:
private const uint GA_ROOT = 2;
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true)]
private static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.DoDragDrop(label1, DragDropEffects.Copy);
}
}
private void label1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
Point pt = Cursor.Position;
IntPtr wnd = WindowFromPoint(pt.X, pt.Y);
IntPtr mainWnd = GetAncestor(wnd, GA_ROOT);
POINT PT;
PT.X = pt.X;
PT.Y = pt.Y;
ScreenToClient(mainWnd, ref PT);
this.Text = String.Format("({0}, {1})", PT.X.ToString(), PT.Y.ToString());
}
這真是棒極了...!非常感謝...這比我想象的更復雜。我如何給你信貸? – user337447
您應該使用MouseMove事件而不是Cursor.Position的座標。他們是與硬件事件相關的人。 –
@用戶您提出了很多問題,從未接受過答案,也從未投過票。你能解決這個問題嗎? –