使鼠标move消息在你的鼠标离开窗体后,还发到当前窗体上来,不过是在当前窗体还处于激活状态,如你在别的窗体上按了左右健,活动焦点离开后,就不起作用了!
解决方案 »
- 工具栏中添加组合框遇到的问题
- CDateTimeCtrl 如何实现只对时间选择,而非年月日的选择.
- 着急啊,各位,请教,我添加了一个MFC类CMyView继承自CView,为什么会出现断言错误???
- 新手!请教,关于窗口问题。
- 关于CListCtrl省略显示的问题。
- 一个简单的问题!
- 高分求VC++/VB简单记事本源码,以供学习、研究
- 在编译这么简单的程序时出现的错误,在线等待
- 气死我了!
- 请问::用何种软件编制 .chm文件?在哪儿可以下载编辑器,VC6.0的 helpwork有这个功能吗?
- 请问能否把在CDC中画的矢量图,如一条直线或圆转换成bmp形式??????
- 请问有关IUnknown *类型的参数传递的问题
HWND hWnd // handle to window
);
{
HDC hdc; // handle to device context
RECT rcClient; // client area rectangle
POINT ptClientUL; // client upper left corner
POINT ptClientLR; // client lower right corner
static POINTS ptsBegin; // beginning point
static POINTS ptsEnd; // new endpoint
static POINTS ptsPrevEnd; // previous endpoint
static BOOL fPrevLine = FALSE; // previous line flag
switch (uMsg)
{
case WM_LBUTTONDOWN:
// Capture mouse input.
SetCapture(hwndMain);
// Retrieve the screen coordinates of the client area,
// and convert them into client coordinates.
GetClientRect(hwndMain, &rcClient);
ptClientUL.x = rcClient.left;
ptClientUL.y = rcClient.top;
// Add one to the right and bottom sides, because the
// coordinates retrieved by GetClientRect do not
// include the far left and lowermost pixels.
ptClientLR.x = rcClient.right + 1;
ptClientLR.y = rcClient.bottom + 1;
ClientToScreen(hwndMain, &ptClientUL);
ClientToScreen(hwndMain, &ptClientLR);
// Copy the client coordinates of the client area
// to the rcClient structure. Confine the mouse cursor
// to the client area by passing the rcClient structure
// to the ClipCursor function.
SetRect(&rcClient, ptClientUL.x, ptClientUL.y,
ptClientLR.x, ptClientLR.y);
ClipCursor(&rcClient);
// Convert the cursor coordinates into a POINTS
// structure, which defines the beginning point of the
// line drawn during a WM_MOUSEMOVE message.
ptsBegin = MAKEPOINTS(lParam);
return 0;
case WM_MOUSEMOVE:
// When moving the mouse, the user must hold down
// the left mouse button to draw lines.
if (wParam & MK_LBUTTON)
{
// Retrieve a device context (DC) for the client area.
hdc = GetDC(hwndMain);
// The following function ensures that pixels of
// the previously drawn line are set to white and
// those of the new line are set to black.
SetROP2(hdc, R2_NOTXORPEN);
// If a line was drawn during an earlier WM_MOUSEMOVE
// message, draw over it. This erases the line by
// setting the color of its pixels to white.
if (fPrevLine)
{
MoveToEx(hdc, ptsBegin.x, ptsBegin.y,
(LPPOINT) NULL);
LineTo(hdc, ptsPrevEnd.x, ptsPrevEnd.y);
}
// Convert the current cursor coordinates to a
// POINTS structure, and then draw a new line.
ptsEnd = MAKEPOINTS(lParam);
MoveToEx(hdc, ptsBegin.x, ptsBegin.y, (LPPOINT) NULL);
LineTo(hdc, ptsEnd.x, ptsEnd.y);
// Set the previous line flag, save the ending
// point of the new line, and then release the DC.
fPrevLine = TRUE;
ptsPrevEnd = ptsEnd;
ReleaseDC(hwndMain, hdc);
}
break;
case WM_LBUTTONUP:
// The user has finished drawing the line. Reset the
// previous line flag, release the mouse cursor, and
// release the mouse capture.
fPrevLine = FALSE;
ClipCursor(NULL);
ReleaseCapture();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
break;
// Process other messages.
移出窗体后还可以再捕获,VC中不一样。
比如想做一个程序,用鼠标的位置得到窗口的相关信息,在主程序窗口
点击一次后,不放开鼠标,移到别的程序窗口上,在VC 中如果不用SetCapture
放开鼠标的消息,主程序窗口就收不到,没办法得到目标窗口的信息了。
SetCapture后就可以了。不过别忘了再ReleaseCapture,不然可坏了,呵呵
说明
将鼠标捕获设置到指定的窗口。在鼠标按钮按下的时候,这个窗口会为当前应用程序或整个系统接收所有鼠标输入
返回值
Long,之前拥有鼠标捕获的窗口的句柄
参数表
参数 类型及说明
hwnd ,要接收所有鼠标输入的窗口的句柄
我的理解:与ReleaseCapture函数一起使用,用于判断鼠标离开
if(..)
...
SetCapture();
}CDialog::OnLButtonUp(..) {
if(..)
..
ReleaseCapture();
}
在这个例子里,鼠标左键按下去以前,你根本不需要知道鼠标移动,鼠标目前位置的任何信息......
但一旦鼠标左键一旦按下去以后,以及鼠标左键松开之前,你需要随时知道鼠标的位置情况......这个时候就可以用到SetCapture()
在LButtonDown(...)里,SetCapture();
在LButtonUp(....)里,添加: if(GetCapture() == this) ReleaseCapture();
在OnMouseMove(..)里,添加:
if(GetCapture() == this)
{
//添加你对鼠标位置的响应函数
}