免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10285 | 回复: 1
打印 上一主题 下一主题

[C++] 取颜色点。一直返回0 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2019-07-26 22:19 |只看该作者 |倒序浏览
        HDC DCEquipmentor = GetDC(0); //获取某一个窗口的DC上下文

        WORD beforMouseX = 0;
        WORD beforMouseY = 0;

        int BGRpackgeSize = 100;
                       
        CHAR*BGRpackage = (CHAR*)malloc(BGRpackgeSize);
        ZeroMemory(BGRpackage,BGRpackgeSize);
        BGRpackage[0] = '{';
        while (true)
        {               

                //mouse 移动到我们需要取色的位置,按下某个键,然后判断取色
                if (-32767 == GetAsyncKeyState(VK_TAB))
                        //cout << "按下了TAB键" << endl;
                        POINT mouse;               
                        //SetCursorPos(1002, 816);
                        //Sleep(10);
                        int x=GetCursorPos(&mouse);//获取当前mouse位置
                        //cout << "x="<<mouse.x<<" "<<mouse.y<< endl;
                        if (mouse.x-beforMouseX!=0||mouse.y-beforMouseY!=0)
                        {
                                //cout <<"DCEquipmentor="<<DCEquipmentor << endl;
                                //cout << "x=" << mouse.x << " " << mouse.y << endl;
                                int BGR = GetPixel(DCEquipmentor, mouse.x, mouse.y);    //这里取不到颜色,返回值 为0,
                                //cout << BGR << endl;//鼠标,X Y值 。HDC 返回正常。 BGR 一直为0值,
                                CHAR SingleBGR[100];//单个BGR
                                /*
-----------------------------------------------------------------------------------------
                                int BGR = GetPixel(DCEquipmentor, mouse.x, mouse.y);    //这里取不到颜色,返回值 为0,
                                //cout << BGR << endl;//鼠标,X Y值 。HDC 返回正常。 BGR 一直为0
                        这里设置后,就可以返回正常值 SetCursorPos(1002,816);

按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0
按下了TAB键
0



论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2019-07-29 08:44 |只看该作者
a. 代码要干净,不相干的内容要删掉
b. 代码要完整,别人拿到手就可以调试

例如:
  1. #include <windows.h>
  2. #include <cstdio>

  3. #pragma comment( lib, "Gdi32.lib" )
  4. #pragma comment( lib, "User32.lib" )

  5. int main( void )
  6. {
  7.     HDC hDC = GetDC( NULL );

  8.     POINT oldpt = { -1, -1 };
  9.     for( ; ; )
  10.     {
  11.         POINT mouse;
  12.         if( GetCursorPos(&mouse) && (mouse.x!=oldpt.x || mouse.y!=oldpt.y) )
  13.         {
  14.             oldpt = mouse;
  15.             COLORREF rgb = GetPixel( hDC, mouse.x, mouse.y );
  16.             printf( "Point(%ld,%ld), RGB(%3hhu,%3hhu,%3hhu)\n", mouse.x, mouse.y, GetRValue(rgb), GetRValue(rgb), GetRValue(rgb) );
  17.         }
  18.     }
  19. }
复制代码


经过我测试,你的代码代码没问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP