获取像素点颜色值GetPixel(hdc,x,y)?

liuuaju 2005-04-09 10:40:25
我自己创建了一个画刷,比如是蓝色的,然后随便画了一条蓝色线段m1(50,60)m2(50,100),为什么我使用GetPixel(hdc,50,70)获取该线段上的颜色值本应是255,为什么却是4294967295。
部分代码如下:
hdc = GetDC(hwnd);
HPEN hpen = CreatePen(PS_SOLID,1,RGB(0,0,255));
HPEN old_hpen = (HPEN)SelectObject(hdc,hpen);

// move to a postion
MoveToEx(hdc, 50,60, NULL);

// draw a line
LineTo(hdc,50, 100);
COLORREF color1=GetPixel(hdc,50,70);//调试时得不到正确结果
// now delete the pen
SelectObject(hdc,old_hpen);
DeleteObject(hpen);

// release the device context
ReleaseDC(hwnd,hdc);
请问我应该怎样准确获得该线段上像素点的颜色值?多谢!
...全文
2467 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
我的断点设置没问题。
请能人帮我瞧瞧。
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
16711680,就是0x00ff0000
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
知道了就是断点的问题,你将断点设在GetPixel之后就对了,一定要在之后
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
4294967295是oxffffffff,不解。
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
4294967295是0xffffffff。我用计算器算过,正好是2的32次方减1。
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
swimmingfish2004:
引自MSDN
The COLORREF value is a 32-bit value used to specify an RGB color.

Remarks
When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00bbggrr

swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
0x00ff0000是不是就是4294967295。
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
“准确的COLORREFR的值0x00bbggrr”楼主是怎么算的?
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
我把完整的代码发上来了,请大家帮我看看。
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <stdio.h>
#include <stdlib.h>
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 300

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
HDC hdc; // handle to a device context

// what is the message
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
//HDC hdc; // graphics device context

// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global
hinstance_app = hinstance;

// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Drawing Line Demo", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
WINDOW_WIDTH, // initial width
WINDOW_HEIGHT,// initial height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);

// save main window handle
main_window_handle = hwnd;
while(GetMessage(&msg,NULL,0,0))
{
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
// get the graphics device context
HDC hdc = GetDC(hwnd);
HPEN hpen = CreatePen(PS_SOLID,1,RGB(0,0,255));
HPEN old_hpen = (HPEN)SelectObject(hdc,hpen);
// move to a postion
MoveToEx(hdc, 50,60, NULL);
// draw a line
LineTo(hdc,50, 100);
COLORREF color1=GetPixel(hdc,50,70);//调试时得不到正确结果
// now delete the pen
SelectObject(hdc,old_hpen);
DeleteObject(hpen);
// release the device context
ReleaseDC(hwnd,hdc);

} // end while
// return to Windows like this
return(msg.wParam);
} // end WinMain
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
颜色值4294967295, 即0xffffffff,采用GetBValue(color1),当然得到的是蓝色分量了。我现在要得到的不是三元色分量,而是准确的COLORREFR的值0x00bbggrr。
我的断点是设在SelectObject(hdc,old_hpen),GetPixel()和LineTo()都使用逻辑坐标。
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
GetBValue(color1);得到蓝色
higherlin 2005-04-09
  • 打赏
  • 举报
回复
很简单啊!
有三个宏可以得到颜色值:
BYTE GetGValue(DWORD rgb):取得绿色值;
BYTE GetRValue(DWORD rgb):取得红色值;
BYTE GetBValue(DWORD rgb):取得蓝色值;
其中rgb就你调用GetPixel的反回值。
swimmingfish2004 2005-04-09
  • 打赏
  • 举报
回复
你试着把断点设置在GetPixel之后,因为如果是在之前,窗口变了,你指定的位置的像素就不是原来的了,所以颜色不对了。
sboom 2005-04-09
  • 打赏
  • 举报
回复
COLORREF 得结构定义事 0x00rrggbb ,16进制

COLORREF color1=GetPixel(hdc,50,70);
红色 unsigned char red=0x00ff0000 & color1;
蓝色 unsigned char blue=0x0000ff00 & color1;
绿色 unsigned char green=0x000000ff & color1;
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
Mackz:
依你上面所写的代码,添加进去后,窗口中出现正确结果ff0000。
但我不明白两个问题:1)为什么程序中断时,不能得到正确值?
2)输出的结果ff0000值总在闪烁,程序在循环执行WM_PAINT消息吗?
呵呵,今天是周末,小菜在这里多谢大家了!
菜牛 2005-04-09
  • 打赏
  • 举报
回复
你是在调试时候看的?那个时候程序中断,可能得不到正确的值,用TRACE或者MessageBox看看结果。

color1=GetPixel(hdc,50,70);//调试时得不到正确结果
char *pText = new char[20];
sprintf(pText, "%x", color1);
TextOut(hdc, 0, 0, pText, lstrlen(pText));
delete []pText;
liuuaju 2005-04-09
  • 打赏
  • 举报
回复
Mackz:
你就别笑我了。
放在循环里面是有问题。窗口是黑色背景,则每重新绘制一次得到的也不是白色,而是蓝色。我依你所说,重新改了代码,把绘制部分放入WM_PAINT消息中,可得到的结果仍然是先是color1=0(正确),然后是=4294967295(误)。
改后的代码如下:
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <stdio.h>
#include <stdlib.h>
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 300

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
HDC hdc; // handle to a device context
HPEN hpen;
HPEN old_hpen;
COLORREF color1;

// what is the message
switch(msg)
{
case WM_PAINT:
// get the graphics device context
hdc = GetDC(hwnd);
hpen = CreatePen(PS_SOLID,1,RGB(0,0,255));
old_hpen = (HPEN)SelectObject(hdc,hpen);
color1=GetPixel(hdc,50,70);
// move to a postion
MoveToEx(hdc, 50,60, NULL);
// draw a line
LineTo(hdc,50, 100);
color1=GetPixel(hdc,50,70);//调试时得不到正确结果
// now delete the pen
SelectObject(hdc,old_hpen);
DeleteObject(hpen);
// release the device context
ReleaseDC(hwnd,hdc);
return (0);
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
//HDC hdc; // graphics device context

// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global
hinstance_app = hinstance;

// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Drawing Line Demo", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
WINDOW_WIDTH, // initial width
WINDOW_HEIGHT,// initial height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);

// save main window handle
main_window_handle = hwnd;
while(GetMessage(&msg,NULL,0,0))
{
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);


} // end while
// return to Windows like this
return(msg.wParam);
} // end WinMain
surstar 2005-04-09
  • 打赏
  • 举报
回复
好说得还有正确 swimmingfish2004(something)

如 Mackz(在相互) 说:
绘制界面-》WM_PAINT

菜牛 2005-04-09
  • 打赏
  • 举报
回复
楼主真强!

第一次看到在“while(GetMessage(&msg,NULL,0,0))”循环里绘制界面的。开了眼界了。

你不能把这点代码放到WM_PAINT消息里吗?
加载更多回复(3)

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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