我自己创建了一个画刷,比如是蓝色的,然后随便画了一条蓝色线段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);
请问我应该怎样准确获得该线段上像素点的颜色值?多谢!

解决方案 »

  1.   

    你的代码很正确。没问题。
    请问你的断点是设置在哪里的,尝试设置在SelectObject(hdc,old_hpen);这里试试,前面的断点都去掉。
      

  2.   

    1.点必须在clip区域
    2.是否需要lptodp?
      

  3.   

    everandforever:
       我的断点是设在SelectObject(hdc,old_hpen),可是得到的color1的值却是4292967295,即0xffffffff。
    bobob:
       GetPixel()和LineTo()都使用逻辑坐标,就不需要转换了。
    请大伙再帮我想一想,这是怎么回事,怎样解决?非常感谢!
      

  4.   

    昨天有事出去了,否则一直趴在网上等候大侠们指点迷津。
    Help!
      

  5.   

    不能用调试,可以用Trace就是对的
      

  6.   

    vcmute:为什么不能用调试。TRACE你又是怎么用的?
      

  7.   

    这是完整代码,请帮我看看。
    // 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
      

  8.   

    vcmute:我查了MSDN,用TRACE是在MFC下进行调试。而我建立的工程是WIN32 application。你的方法行不通哟
      

  9.   

    已经说了,把绘制代码放到WM_PAINT消息处理过程中去。
      

  10.   

    COLORREF 得结构定义事 0x00rrggbb  ,16进制COLORREF color1=GetPixel(hdc,50,70);
    红色 unsigned char red=0x00ff0000 & color1;
    蓝色 unsigned char blue=0x0000ff00 & color1;
    绿色 unsigned char green=0x000000ff & color1;
      

  11.   

    sboom:
        你那做法是获取颜色值的R、G、B分量。跟我的问题没关系。
    Mackz:
       我不放在WM_PAINT消息处理中一样可以绘制一条线段。问题不是出在绘制的地方 。
    上面我给出了完整代码,好心人就帮我调试找找症结所在!
      

  12.   

    我照Mackz的做法改了,放在WM_PAINT进行画线,不过仍有问题。