OnNcLButtonUp怎么双击后才执行?

zjfhgdx 2010-12-23 12:01:13
大家也是这样吗?我单击非客户区不会执行OnNcLButtonUp,双击后才会执行。。。
...全文
215 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilingshui 2010-12-28
  • 打赏
  • 举报
回复
//about the OnNcLButtonUp message

//在窗口处于正常状态时(可以通过拖动标题栏来移动窗口),如果在标题栏上按下鼠标左键,
//系统会捕获鼠标,所以直到放开鼠标左键后,窗口是不会收到鼠标消息的。
//如果窗口处于最大化状态,或者在客户区按下鼠标、移到标题栏上放开,
//或者点击菜单栏等都可以收到WM_NCLBUTTONUP消息。

//可以通过给程序添加一个鼠标钩子来实现,当OnNcLButtonDown按下时,松开后会收到lbuttonup消息,但程序不会响应
//通过钩子,在收到lbuttonup消息后,向应用程序发送OnNcLButtonUp消息,从而解决这个问题。
//如下为原文介绍:


//WM_NCLBUTTONUP message patch

//If you spend time investigating what happens when you click and release the left button over the title bar,
//you will find out that instead of getting "non-client left button up" message, you just get "left button up".
//One could actually work with it, if the message found it's way into the application. It does not.


//I was in a middle of writing an application when I discovered this.
//I looked all over the internet for articles regarding WM_NCLBUTTONUP problem,
//but the only thing I could find were questions about the problem.
//After some more investigating I have come up with a patch that could be adopted by each application requiring such notification.

//The patch consists of installing a "windows hook" that will intercept all mouse messages for this application before they enter into the message pump.
//To do that you need to call SetWindowsHookEx(...) function, soon after the main window is created. Here is the call:

hMHook = SetWindowsHookEx(
// hook type:
WH_MOUSE,
// hook procedure:
(HOOKPROC) MouseHookProc,
// handle to application instance:
AfxGetInstanceHandle(),
// thread identifier:
AfxGetThread()->m_nThreadID
);


//It is very important that you supply handle to application instance and thread identifier,
//otherwise every application running on your computer will attempt to hook
//it's mouse messages through your program and it could be disastrous.
//By supplying these two parameters you will insure that only messages from your application will end up in your callback function.

//Equally important is a call to remove the hook before your application terminates.
//The UnhookWindowsHookEx(...) function removes a hook procedure installed in a hook chain.
//Most likely you will call it somewhere in OnDestroy(), like this:

if(hMHook != NULL)
UnhookWindowsHookEx(hMHook);


//The callback function is where you will receive WM_NCLBUTTONDOWN message and the next
//time you receive WM_LBUTTONUP message you will post WM_NCLBUTTONUP directly into the application message pump.
//Therefore, no special handling will be required to service these messages.
//You will simply write your code inside of OnNcLButtonUp(...), just like you would for any other message.

//Here is the callback code:

// ////////////////////////////////////////////////////
// handle to the mouse hook
HHOOK hMHook = NULL;

// status of non-client left button down
BOOL bNcLButtonDown = FALSE;

// /////////////////////////////////////////////////////
// Mouse hook process

LRESULT CALLBACK MouseHookProc( int nCode,
WPARAM wParam,
LPARAM lParam)
{
if(nCode == HC_ACTION)
{
// get a pointer to the mouse hook struct.
PMOUSEHOOKSTRUCT mhs = (PMOUSEHOOKSTRUCT) lParam;

// intercept messages for left button down and up
switch(wParam)
{
case WM_NCLBUTTONDOWN:
{
// get the pointer to the main window
CWnd *pWnd = AfxGetMainWnd();

// if the message is from your window and
// the hit test indicates title bar
if((mhs->hwnd == pWnd->GetSafeHwnd())
&& (mhs->wHitTestCode == HTCAPTION))
{
// then indicate non-client left button down
bNcLButtonDown = TRUE;

// there is no problem with this message
// so you don't have to do anything else
}
}
break;

case WM_NCLBUTTONUP:
// you will get this message if you double-click
// on the title bar
// reset the status
bNcLButtonDown = FALSE;
break;

case WM_LBUTTONUP:
{
// get the pointer to the main window
CWnd *pWnd = AfxGetMainWnd();

// if the message is from your window and
// non-client left button is down
if((mhs->hwnd == pWnd->GetSafeHwnd())
&& (bNcLButtonDown == TRUE))
{
// then post WM_NCLBUTTONUP message directly
// into your window message pump
// Note: I'm hardcoding HTCAPTION because the
// left button was down, and while it is down,
// the mouse does not move in respect to the
// window, but it does in respect to the screen,
// so the mouse should still be over the caption
// bar of your window when you release the button.
pWnd->PostMessage(WM_NCLBUTTONUP, HTCAPTION,
MAKELONG(mhs->pt.x,mhs->pt.y));

// reset non-client left button down
bNcLButtonDown = FALSE;
}
}
break;

default:
break;
}
}
// let the messages through to the next hook
return CallNextHookEx(hMHook, nCode, wParam, lParam);
}


//I am including two sample projects. The "nclbxExample" is technical, and the "AlphaDialogExample" is more practical.
//The "nclbxExample" is better documented so you can see how and were I have implemented the code.

原文地址:http://www.codeguru.com/Cpp/misc/misc/windowsmessaging/article.php/c3885/
zjfhgdx 2010-12-23
  • 打赏
  • 举报
回复
关闭按钮是gdi直接画的,不是控件,我要处理up消息
zjfhgdx 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 visualeleven 的回复:]
引用 4 楼 zjfhgdx 的回复:
那怎么办?

自己模拟手动发送一个WM_NCLBUTTONUP消息,PostMessage(WM_NCLBUTTONUP, ...);在你的WM_NCLBUTTONDOWN消息的响应函数中
[/Quote]
我是在非客户区放了一个自己的关闭按钮不想让它的down的时候关闭
dianhui 2010-12-23
  • 打赏
  • 举报
回复
这个鼠标在非客户区上放开就可以执行的。
Eleven 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zjfhgdx 的回复:]
那怎么办?
[/Quote]
自己模拟手动发送一个WM_NCLBUTTONUP消息,PostMessage(WM_NCLBUTTONUP, ...);在你的WM_NCLBUTTONDOWN消息的响应函数中
LuciferStar 2010-12-23
  • 打赏
  • 举报
回复
为什么非要Up呢?不能Down么?
zjfhgdx 2010-12-23
  • 打赏
  • 举报
回复
那怎么办?
Eleven 2010-12-23
  • 打赏
  • 举报
回复
确实是这样的,非客户区LButton UP的消息,只有在两种情况下会相应,一个是在非客户区双击,一个是在客户区LButton Down下,但是在非客户区Up
zjfhgdx 2010-12-23
  • 打赏
  • 举报
回复
请问楼有试过?
zjfhgdx 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 visualeleven 的回复:]
引用 7 楼 zjfhgdx 的回复:
引用 6 楼 visualeleven 的回复:
引用 4 楼 zjfhgdx 的回复:
那怎么办?

自己模拟手动发送一个WM_NCLBUTTONUP消息,PostMessage(WM_NCLBUTTONUP, ...);在你的WM_NCLBUTTONDOWN消息的响应函数中

我是在非客户区放了一个自己的关闭按钮不想让它的down的时候关闭……
[/Quote]
现在的问题是单击不会产生up消息,只有双击的时候才会产生。这样我的程序要双击才能关闭了。
不知道系统的关闭按钮是怎么弄的?为什么系统的按钮down之后不会关闭?
Eleven 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zjfhgdx 的回复:]
引用 6 楼 visualeleven 的回复:
引用 4 楼 zjfhgdx 的回复:
那怎么办?

自己模拟手动发送一个WM_NCLBUTTONUP消息,PostMessage(WM_NCLBUTTONUP, ...);在你的WM_NCLBUTTONDOWN消息的响应函数中

我是在非客户区放了一个自己的关闭按钮不想让它的down的时候关闭
[/Quote]
你不如它执行关闭的流程不就是了,代码不是你自己写的话,想怎样都可以

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧