功能描述:
把对话框中的图标拖到需要置顶的窗口上,该窗口就会被置顶
所用到的API:
SetCapture(); //设置鼠标属于当前线程
ReleaseCapture(); //释放鼠标
WindowFromPoint(POINT pt); //获取鼠标处的窗口句柄
代码:
新建对话框工程TopWindow,添加的资源有:IDC_CURSOR1(光标),IDI_ICON1,IDI_ICON2,IDI_ICON3 (图标)
对话框中的控件有:PICTRUE控件,ID为IDC_PIC,类型为ICON,选择IDI_ICON3
由于PICTRUE控件没有OnLButtonDown,OnLButtonUp,所以新建一个MFC类CMyPIC,它的基类为Static,产生左键按下,左键按起消息;在CTopWindow的中OnInitDialog把CMyPIC与IDC_PIC控件关联起来,代码为:
m_pic.SubclassDlgItem(IDC_PIC,this);
CMyPIC中的代码为:
void CMyPIC::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//设置鼠标属于当前线程
SetCapture();
//加载鼠标
HCURSOR hc=LoadCursor(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
::SetCursor(hc);
//加载图标
HICON hicon2=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON2));
this->SetIcon(hicon2);
CStatic::OnLButtonDown(nFlags, point);
}
void CMyPIC::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//释放鼠标
ReleaseCapture();
//还原图标
HICON hicon1=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON1));
this->SetIcon(hicon1);
//获得所选窗口句柄
POINT pt;
::GetCursorPos(&pt);
//获得鼠标位置处的窗口句柄
HWND hWnd=::WindowFromPoint(pt);
//窗口置顶
::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
CStatic::OnLButtonUp(nFlags, point);
}