MFC 不可点击按钮(MouseMove 上远离光标)
如果光标足够接近 MFC 中按钮的中心,我将如何制作一个按钮来更改其在 MouseMove 事件上的位置?
How would i make a button that will change it's position on MouseMove Event if the cursor is close enough to the center of the button in MFC ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果光标不在按钮上(并且未被捕获,但您不希望这样),则 WM_MOUSEMOVE 不会传递到按钮。所以你必须在父对话框中处理WM_MOUSEMOVE。如果您希望按钮成为一个独立的控件,则必须在创建按钮时对父窗口进行子类化。
在这种情况下,子类化意味着:
- 使用 GetParent()->GetWindowLong(GWL_WNDPROC) 检索并存储父级窗口过程地址
- 您可以使用 SetWindowLong() 将其设置为您的过程
- 在该过程中,在按照您想要的方式处理 WM_MOUSEMOVE 之后,您调用父级的前一个窗口过程。
WM_MOUSEMOVE is not delivered to the button if the cursor is not over it (and is not captured, but you don't want that). So you have to process WM_MOUSEMOVE in the parent dialog. If you want your button to be a self-contained control, you have to subclass the parent window upon button creation.
Subclassing, in this context, means:
- you retrieve and store the parent's window proc address with GetParent()->GetWindowLong(GWL_WNDPROC)
- you set it to your procedure with SetWindowLong()
- in the procedure, you call the parent's previous window proc, after handling WM_MOUSEMOVE the way you want.
WM_MOUSEMOVE 坐标将相对于屏幕,但您可能希望跟踪按钮相对于包含该按钮的窗口的位置。在父窗口上使用ScreenToClient方法进行转换,然后可以比较坐标看看是否接近。然后使用 MoveWindow 来移动按钮。
The WM_MOUSEMOVE coordinates will be relative to the screen, but you'll probably want to track the button position relative to the window that contains it. Use the ScreenToClient method on the parent window to convert, then you can compare the coordinates to see if it's close. Then use MoveWindow to move the button.
如果跟踪鼠标光标位置,您可以确定光标何时接近或进入按钮窗口矩形。然后,您可以使用 SetWindowPos() 函数在父窗口客户区中重新定位按钮窗口。
If you track the mouse cursor position you can determine when the cursor gets close to or enters the button window rect. You can then use the SetWindowPos() function to reposition the button window in the parent window client area.