对话框头文件中
/*
逻辑顺序:
1、先在对话框的初始函数中定义好游标
2、获取被拖动图像的位置
3、鼠标左键点击事件按下的时候,开始捕获游标(拖动图像的位置)
4、鼠标左键释放掉游标(拖动图像的位置),之后需要转换(ClientToScreen),获取鼠标释放时候的XY坐标,主要获取句柄还是通过WindowFromPoint,和事件释放后的XY坐标
*/
HICON m_hIcon;
HCURSOR mCursor; //定义鼠标
RECT rect; //定义鼠标形状
HWND hDest; //获得的目标句柄
BOOL CMy9Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
....
mCursor = LoadCursor(NULL, IDC_SIZEALL); //默认带有箭头
GetDlgItem(IDC_STATIC)->GetWindowRect(&rect);
...
void CMy9Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if (point.x >= rect.left && point.x <= rect.right || point.y >= rect.top && point.y <= rect.bottom) {
SetCapture(); //开始捕获
SetCursor(mCursor); //当范围匹配,就对当前光标进行改变
}
CDialogEx::OnLButtonDown(nFlags, point);
}
void CMy9Dlg::OnLButtonUp(UINT nFlags, CPoint point)
{
POINT pt = point;
CString mString;
wchar_t sName[64];
wchar_t sContent[64];
ReleaseCapture();
ClientToScreen(&pt);
hDest = ::WindowFromPoint(pt); //获取指定POINT的窗口的句柄
mString.Format(_T("0x%.8x"), hDest);
SetDlgItemText(IDC_EDIT1, mString);
GetClassName(hDest, sName, MAX_PATH); //获取类名
SetDlgItemText(IDC_EDIT2, sName);
::SendMessage(hDest, WM_GETTEXT, (WPARAM)64, (LPARAM)sContent); //获取标题
SetDlgItemText(IDC_EDIT3, sContent);
CDialogEx::OnLButtonUp(nFlags, point);
}