VC++ 解决在鼠标移动时,光标闪烁的问题。其实本质是 ON_SETCURSOR的用法
在调用Windows API函数SetCursor设置光标时,可能会碰到闪烁的问题:移动鼠标,光标在Class Cursor(即注册窗口类时指定的Cursor)与预设Cursor之间闪烁。
在MSDN上有关SetCursor函数的备注中强调,如果Class Cursor非空,那么每当鼠标移动,系统都会把光标恢复为Class Cursor。为了避免光标闪烁这一问题,必须处理WM_SETCURSOR消息。(MSDN说明)
下面是一个例子:程序在主窗口视图的中间位置绘制RGB条带,当鼠标移动在条带范围就将光标设置成为Cross,此外根据光标的位置,在RGB条带上方30px处显示所处条带的颜色。程序运行起来像这样:
如果在WM_MOUSEMOVE的消息处理中判断光标的位置并设置光标的话,就会碰到所说的光标闪烁问题。WM_MOUSEMOVE的消息处理如下代码所示:
.LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
.{
. POINT ptCursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
. RECT rect, rectText;
. get_rects(&rect, &rectText);
. InvalidateRect(&rectText);
. UpdateWindow();
. if (::PtInRect(&rect, ptCursor)) {
. ::SetCursor(m_cursor);
. int dx = (rect.right - rect.left) / ;
. LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
. int index;
. if (ptCursor.x - rect.left < dx)
. index = ;
. else if (ptCursor.x - rect.left < * dx)
. index = ;
. else index = ;
. WTL::CString str;
. str.Format(_T("Cursor on %s part"), ppsz[index]);
. CClientDC dc(m_hWnd);
. dc.DrawText(str, -, &rectText, DT_CENTER | DT_VCENTER);
. }
. else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
. return ;
.}
闪烁产生的原因在于每次进入OnMouseMove之前,系统都会先将光标恢复成Arrow,进入OnMouseMove之后,如果光标处在RGB条带范围内则立即被设置成Cross。
解决办法就是将上面的判断逻辑放在WM_SETCURSOR的消息处理中,当然获得光标客户坐标的方式不同,代码如下所示:
[cpp] view plaincopyprint?
.LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
.{
. POINT point;
. ::GetCursorPos(&point);
. ScreenToClient(&point);
. set_cursor(point);
. return ;
.}
LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
POINT point;
::GetCursorPos(&point);
ScreenToClient(&point);
set_cursor(point);
return 0;
}
而代码中的set_cursor私有方法其实就是上面的判断逻辑,即:
[cpp] view plaincopyprint?
.// ptCursor: in client coordinate
.void set_cursor(POINT& ptCursor) throw()
.{
. RECT rect, rectText;
. get_rects(&rect, &rectText);
. InvalidateRect(&rectText);
. UpdateWindow();
. if (::PtInRect(&rect, ptCursor)) {
. ::SetCursor(m_cursor);
. int dx = (rect.right - rect.left) / ;
. LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
. int index;
. if (ptCursor.x - rect.left < dx)
. index = ;
. else if (ptCursor.x - rect.left < * dx)
. index = ;
. else index = ;
. WTL::CString str;
. str.Format(_T("Cursor on %s part"), ppsz[index]);
. CClientDC dc(m_hWnd);
. dc.DrawText(str, -, &rectText, DT_CENTER | DT_VCENTER);
. }
. else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
.}
// ptCursor: in client coordinate
void set_cursor(POINT& ptCursor) throw()
{
RECT rect, rectText;
get_rects(&rect, &rectText);
InvalidateRect(&rectText);
UpdateWindow();
if (::PtInRect(&rect, ptCursor)) {
::SetCursor(m_cursor);
int dx = (rect.right - rect.left) / 3;
LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
int index;
if (ptCursor.x - rect.left < dx)
index = 0;
else if (ptCursor.x - rect.left < 2 * dx)
index = 1;
else index = 2;
WTL::CString str;
str.Format(_T("Cursor on %s part"), ppsz[index]);
CClientDC dc(m_hWnd);
dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
}
else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
}
这样就解决了光标闪烁的问题。
在调用Windows API函数SetCursor设置光标时,可能会碰到闪烁的问题:移动鼠标,光标在Class Cursor(即注册窗口类时指定的Cursor)与预设Cursor之间闪烁。
在MSDN上有关SetCursor函数的备注中强调,如果Class Cursor非空,那么每当鼠标移动,系统都会把光标恢复为Class Cursor。为了避免光标闪烁这一问题,必须处理WM_SETCURSOR消息。(MSDN说明)
下面是一个例子:程序在主窗口视图的中间位置绘制RGB条带,当鼠标移动在条带范围就将光标设置成为Cross,此外根据光标的位置,在RGB条带上方30px处显示所处条带的颜色。程序运行起来像这样:
如果在WM_MOUSEMOVE的消息处理中判断光标的位置并设置光标的话,就会碰到所说的光标闪烁问题。WM_MOUSEMOVE的消息处理如下代码所示:
- LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
- {
- POINT ptCursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- RECT rect, rectText;
- get_rects(&rect, &rectText);
- InvalidateRect(&rectText);
- UpdateWindow();
- if (::PtInRect(&rect, ptCursor)) {
- ::SetCursor(m_cursor);
- int dx = (rect.right - rect.left) / 3;
- LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
- int index;
- if (ptCursor.x - rect.left < dx)
- index = 0;
- else if (ptCursor.x - rect.left < 2 * dx)
- index = 1;
- else index = 2;
- WTL::CString str;
- str.Format(_T("Cursor on %s part"), ppsz[index]);
- CClientDC dc(m_hWnd);
- dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
- }
- else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
- return 0;
- }
LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
POINT ptCursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
RECT rect, rectText;
get_rects(&rect, &rectText);
InvalidateRect(&rectText);
UpdateWindow();
if (::PtInRect(&rect, ptCursor)) {
::SetCursor(m_cursor);
int dx = (rect.right - rect.left) / 3;
LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
int index;
if (ptCursor.x - rect.left < dx)
index = 0;
else if (ptCursor.x - rect.left < 2 * dx)
index = 1;
else index = 2;
WTL::CString str;
str.Format(_T("Cursor on %s part"), ppsz[index]);
CClientDC dc(m_hWnd);
dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
}
else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
return 0;
}
闪烁产生的原因在于每次进入OnMouseMove之前,系统都会先将光标恢复成Arrow,进入OnMouseMove之后,如果光标处在RGB条带范围内则立即被设置成Cross。
解决办法就是将上面的判断逻辑放在WM_SETCURSOR的消息处理中,当然获得光标客户坐标的方式不同,代码如下所示:
- LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
- {
- POINT point;
- ::GetCursorPos(&point);
- ScreenToClient(&point);
- set_cursor(point);
- return 0;
- }
LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
POINT point;
::GetCursorPos(&point);
ScreenToClient(&point);
set_cursor(point);
return 0;
}
而代码中的set_cursor私有方法其实就是上面的判断逻辑,即:
- // ptCursor: in client coordinate
- void set_cursor(POINT& ptCursor) throw()
- {
- RECT rect, rectText;
- get_rects(&rect, &rectText);
- InvalidateRect(&rectText);
- UpdateWindow();
- if (::PtInRect(&rect, ptCursor)) {
- ::SetCursor(m_cursor);
- int dx = (rect.right - rect.left) / 3;
- LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
- int index;
- if (ptCursor.x - rect.left < dx)
- index = 0;
- else if (ptCursor.x - rect.left < 2 * dx)
- index = 1;
- else index = 2;
- WTL::CString str;
- str.Format(_T("Cursor on %s part"), ppsz[index]);
- CClientDC dc(m_hWnd);
- dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
- }
- else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
- }
// ptCursor: in client coordinate
void set_cursor(POINT& ptCursor) throw()
{
RECT rect, rectText;
get_rects(&rect, &rectText);
InvalidateRect(&rectText);
UpdateWindow();
if (::PtInRect(&rect, ptCursor)) {
::SetCursor(m_cursor);
int dx = (rect.right - rect.left) / 3;
LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
int index;
if (ptCursor.x - rect.left < dx)
index = 0;
else if (ptCursor.x - rect.left < 2 * dx)
index = 1;
else index = 2;
WTL::CString str;
str.Format(_T("Cursor on %s part"), ppsz[index]);
CClientDC dc(m_hWnd);
dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
}
else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
}
这样就解决了光标闪烁的问题。
VC++ 解决在鼠标移动时,光标闪烁的问题。其实本质是 ON_SETCURSOR的用法的更多相关文章
- VC++大数据量绘图时无闪烁刷屏技术实现(我的理解是,在内存上作画,然后手动显示,而不再直接需要经过WM_PAINT来处理了)
http://hantayi.blog.51cto.com/1100843/383578 引言 当我们需要在用户区显示一些图形时,先把图形在客户区画上,虽然已经画好但此时我们还无法看到,还要通过 程序 ...
- 【ios bug解决】 输入框聚焦时光标不显示
解决办法:重写user-select样式 css: user-select: text;-webkit-user-select:text;
- C#中的WinForm问题——使用滚动条时页面闪烁及重影问题
当使用鼠标进行滚动查看页面时,由于页面会频繁刷新,如果页面中控件较多会导致页面出现闪烁.重影等问题,如下图所示: 在网上搜索过该问题,大部分都说使用双缓冲可以解决此类问题,即通过设置DoubleBuf ...
- VC/MFC 当鼠标移到控件上时显示提示信息
VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...
- 通过特殊处理 Resize 事件解决 WinForm 加载时闪烁问题的一个方法
WinForm 上放置的控件多了或者有大背景图,窗体加载时就会闪烁,对于一般的闪烁,设置 DoubleBuffer=True或许有一点改善,要立竿见影的解决可以重载 CreateParams 使用 W ...
- ListView用法及加载数据时的闪烁问题和加载数据过慢问题
ListView介绍及添加数据时的闪烁问题 1. ListView类 1.1 ListView常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示 ...
- 使用mx:Repeater在删除和添加item时列表闪烁
使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...
- 防止vuejs在解析时出现闪烁
---## 防止vuejs在解析时出现闪烁 ## 原因: 在使用vuejs.angularjs开发时,经常会遇见在如Chrome这类能够快速解析的浏览器上出现表达式({{ express }} ),或 ...
- jQuery的鼠标悬停时放大图片的效果
这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...
随机推荐
- sqlserver2008 创建定时任务
SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...
- windows下CMake使用图文手册 Part 1
维基百科介绍“CMake是个开源的跨平台自动化建构系统,它用配置文件控制建构过程(build process)的方式和Unix的Make相似,只是CMake的配置文件取名为CMakeLists.txt ...
- Grunt的使用
在Node环境下.需要预先安装好Node. 1.安装grunt-cli [root@Luxh-01 ~]# npm install -g grunt-cli 2.创建一个目录test [root@Lu ...
- 使用delphi+intraweb进行微信开发5—准备实现微信API,先从获取AccessToken开始
在前4讲中我们已经使iw开发的应用成功和微信进行了对接,再接下来的章节中我们开始逐一尝试和实现微信的各个API,开始前先来点准备工作 首先需要明确的是,微信的API都是通过https调用实现的,分为p ...
- Starling项目启动时报错This application is not correctly embedded (wrong wmode value)
这个其实是Flash报的“ErrorEvent:. text=Error #3702: Context3D 不可用”错误被Starling变了一下. 解决方法:找到项目文件夹中的html-templa ...
- lombok
参考http://blog.csdn.net/mlinge/article/details/51340362
- android .apk安装时遇到NSTALL_FAILED_CONTAINER_ERROR错误
在一台酷派上装apk时遇到问题: android Installation error: INSTALL_FAILED_CONTAINER_ERROR 遇到问题后baidu google修改为andr ...
- SDRAM控制器的Verilog建模之一
前言:作为经典存储器的三剑客中的flash和sram已经建模测试过了,虽然现在都已经ddr2,ddr3,667MHZ.1333MHZ的天下了,但是接下这周来准备写一下sdram的controller. ...
- c++学习一:指针基础
1.指针优势,当数据量比较大时,通过指针直接访问数据量所在内存.处理更 加复杂的数据结构.例如:链表.二叉树.图等.2.指针本质是一种表示内存地址的数据类型,它和整型int和浮点数float一样 只是 ...
- java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using
在使用spring-data-redis时使用junit测试报错: java.lang.IllegalArgumentException: template not initialized; call ...