在调用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的消息处理如下代码所示:

  1. LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
  2. {
  3. POINT ptCursor = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  4. RECT rect, rectText;
  5. get_rects(&rect, &rectText);
  6. InvalidateRect(&rectText);
  7. UpdateWindow();
  8. if (::PtInRect(&rect, ptCursor)) {
  9. ::SetCursor(m_cursor);
  10. int dx = (rect.right - rect.left) / 3;
  11. LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
  12. int index;
  13. if (ptCursor.x - rect.left < dx)
  14. index = 0;
  15. else if (ptCursor.x - rect.left < 2 * dx)
  16. index = 1;
  17. else index = 2;
  18. WTL::CString str;
  19. str.Format(_T("Cursor on %s part"), ppsz[index]);
  20. CClientDC dc(m_hWnd);
  21. dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
  22. }
  23. else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
  24. return 0;
  25. }
	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的消息处理中,当然获得光标客户坐标的方式不同,代码如下所示:

  1. LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
  2. {
  3. POINT point;
  4. ::GetCursorPos(&point);
  5. ScreenToClient(&point);
  6. set_cursor(point);
  7. return 0;
  8. }
	LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
POINT point;
::GetCursorPos(&point);
ScreenToClient(&point);
set_cursor(point);
return 0;
}

而代码中的set_cursor私有方法其实就是上面的判断逻辑,即:

  1. // ptCursor: in client coordinate
  2. void set_cursor(POINT& ptCursor) throw()
  3. {
  4. RECT rect, rectText;
  5. get_rects(&rect, &rectText);
  6. InvalidateRect(&rectText);
  7. UpdateWindow();
  8. if (::PtInRect(&rect, ptCursor)) {
  9. ::SetCursor(m_cursor);
  10. int dx = (rect.right - rect.left) / 3;
  11. LPTSTR ppsz[] = { _T("Red"), _T("Green"), _T("Blue") };
  12. int index;
  13. if (ptCursor.x - rect.left < dx)
  14. index = 0;
  15. else if (ptCursor.x - rect.left < 2 * dx)
  16. index = 1;
  17. else index = 2;
  18. WTL::CString str;
  19. str.Format(_T("Cursor on %s part"), ppsz[index]);
  20. CClientDC dc(m_hWnd);
  21. dc.DrawText(str, -1, &rectText, DT_CENTER | DT_VCENTER);
  22. }
  23. else ::SetCursor(CCursor().LoadSysCursor(IDC_ARROW));
  24. }
	// 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的用法的更多相关文章

  1. VC++大数据量绘图时无闪烁刷屏技术实现(我的理解是,在内存上作画,然后手动显示,而不再直接需要经过WM_PAINT来处理了)

    http://hantayi.blog.51cto.com/1100843/383578 引言 当我们需要在用户区显示一些图形时,先把图形在客户区画上,虽然已经画好但此时我们还无法看到,还要通过 程序 ...

  2. 【ios bug解决】 输入框聚焦时光标不显示

    解决办法:重写user-select样式 css:   user-select: text;-webkit-user-select:text;

  3. C#中的WinForm问题——使用滚动条时页面闪烁及重影问题

    当使用鼠标进行滚动查看页面时,由于页面会频繁刷新,如果页面中控件较多会导致页面出现闪烁.重影等问题,如下图所示: 在网上搜索过该问题,大部分都说使用双缓冲可以解决此类问题,即通过设置DoubleBuf ...

  4. VC/MFC 当鼠标移到控件上时显示提示信息

    VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...

  5. 通过特殊处理 Resize 事件解决 WinForm 加载时闪烁问题的一个方法

    WinForm 上放置的控件多了或者有大背景图,窗体加载时就会闪烁,对于一般的闪烁,设置 DoubleBuffer=True或许有一点改善,要立竿见影的解决可以重载 CreateParams 使用 W ...

  6. ListView用法及加载数据时的闪烁问题和加载数据过慢问题

    ListView介绍及添加数据时的闪烁问题 1.     ListView类 1.1 ListView常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示 ...

  7. 使用mx:Repeater在删除和添加item时列表闪烁

    使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...

  8. 防止vuejs在解析时出现闪烁

    ---## 防止vuejs在解析时出现闪烁 ## 原因: 在使用vuejs.angularjs开发时,经常会遇见在如Chrome这类能够快速解析的浏览器上出现表达式({{ express }} ),或 ...

  9. jQuery的鼠标悬停时放大图片的效果

    这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...

随机推荐

  1. oracle之synonym小结

    oracle中的同义词可以认为是对表.视图.序列.存储过程.函数.程序包或者其他同义词的一个别名,也就是用一个别名来映射的作用. oracle中的同义词可以分为私有和公有两种,私有同义词(privat ...

  2. JQGrid 学习1

    这几天一直在学习基于MVC的JQGrid. 记得刚毕业时候做web最头疼的就是GridView,各种分页查询删除,后来学习了Ajax,使用的jqury UI框架ligerui给公司做ERP系统,再后来 ...

  3. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  4. hdu 5382 GCD?LCM!

    先考虑化简f函数 发现,f函数可以写成一个递归式,化简后可以先递推求出所有f函数的值, 所以可以先求出所有S函数的值,对于询问,O(1)回答 代码: //File Name: hdu5382.cpp ...

  5. iis网站发布相关问题

    最近在公司的服务器上发布了一个简单的web应用,整个做下来到上线用了将近2天时间,期间出现了各种问题,现在发出来供大家参考: 1.iis上发布后出现访问网站,出现“IIS服务器被配置为不列出此目录的内 ...

  6. unix&linux大学教程 目录

    第1章unix简介 第2章什么是linux?什么是unix 第3章unix连接 第4章开始使用unix 第5章gui:图形用户界面 第6章unix工作环境 第7章unix键盘使用 第8章能够立即使用的 ...

  7. eclipse 添加 hibernate 插件

    eclipse helios(3.6)版 1.启动eclipse 2.选择Help > Install New Software...> 3.添加如下地址:http://download. ...

  8. 外壳exe通过反射调用dll时

    外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...

  9. 7、IMS - DNS & ENUM

    1.相关基础SBC:http://blog.sina.com.cn/s/blog_7a6f76080100vp9r.html 2.ENUM/DNS查询过程:http://blog.sina.com.c ...

  10. 关于handler

    1. 一个Handler只有一个队列;2. 在调用Handler.post(Runnable runnable)方法时,会将runnable封装成一个Message; 3. 在队列执行时,会判断当前的 ...