2015-02-06 116 views
2

我正在尝试使用编程Windows第五版来学习win32 API。 当我与一些标识符尝试我发现我不能够理解为什么是happening.I`会更具体的东西,这里是我的代码:当将GetStockObject(WHITE_BRUSH)更改为GetStockObject(GREY_BRUSH)时松动鼠标光标

#include<Windows.h> 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 

int 
WINAPI 
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{ 
    static TCHAR szAppName[] = TEXT("HELLOWIN"); 
    HWND hwnd; 
    MSG msg; 
    WNDCLASS wndclass; 

    wndclass.style   = CS_HREDRAW | CS_VREDRAW; 
    wndclass.lpfnWndProc = WndProc; 
    wndclass.cbClsExtra = 0; 
    wndclass.cbWndExtra = 0; 
    wndclass.hInstance  = hInstance; 
    wndclass.hIcon   = LoadIcon(NULL, IDI_SHIELD); 
    wndclass.hCursor  = LoadCursor(NULL, IDC_CROSS); 
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
    wndclass.lpszMenuName = NULL; 
    wndclass.lpszClassName = szAppName; 

    if (!RegisterClass(&wndclass)) 
    { 
     MessageBox(0, TEXT("This Programm Requires WINNT!"), szAppName, MB_ICONERROR); 
     return(0); 
    } 

    hwnd = CreateWindow(szAppName, //window class name 
     TEXT("The Hello Program"), //window caption 
     WS_OVERLAPPEDWINDOW,   //window style 
     CW_USEDEFAULT,    //initial x position 
     CW_USEDEFAULT,    //initial y position 
     CW_USEDEFAULT,    //initial x size 
     CW_USEDEFAULT,    //initial y size 
     NULL,      //parent window handle(we have top-level window) 
     NULL,      //window menu handle 
     hInstance,     //programm instances handle  
     NULL);      //creation parameters       

    ShowWindow(hwnd, iCmdShow); 
    UpdateWindow(hwnd); 

    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return msg.wParam; 


} 

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    HDC hdc; 
    PAINTSTRUCT ps; 
    RECT rect; 

    switch (message) 
    { 

     case WM_CREATE: 
     { 
      PlaySound(TEXT("D:\\mp3\\aywy._&_EphRem_-_Adderall.wav"), NULL, SND_FILENAME | SND_ASYNC); 
      return 0; 
     } break; 

     case WM_PAINT: 
     { 
      hdc = BeginPaint(hwnd, &ps); 
      GetClientRect(hwnd, &rect); 

      DrawText(hdc, TEXT("Hello, Windows 98!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
      EndPaint(hwnd, &ps); 
      return 0; 
     } break; 

     case WM_DESTROY: 
     { 
      PostQuitMessage(0); 
      return 0; 
     } break; 

    } 
    return DefWindowProc(hwnd, message, wParam, lParam); 
} 

有了这个代码一切的伟大工程,为预期但是... 当我改变:

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 

wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); 

光标图标丢失的背景,仅visisbl e在我使用drawText()的小行 中。让我困惑的是当我的背景是白色(WHITE_BRUSH)时,这不会发生。

有人可以解释为什么吗? PS:如果这种行为在本书后面解释(我正在完成第3章当前),只需键入阅读更多,所以我不会浪费你的时间。

预先感谢您。

回答

2

可能发生的情况是,您正在使用的“十字”光标是一个非常细的光标,通过对基础像素进行归类(而不是在其上方绘制)来实现(通过窗口或硬件)。这对于除了0x808080灰色以外的所有颜色都适用,因为否定0x808080仍然给出0x808080,所以光标不可见。尝试使用浅灰色,深灰色或其他不那么薄的光标。

+0

你是对的。当将光标标识符改回IDC_ARROW(默认白色箭头窗口光标)时,我可以再次看到光标。谢谢。 – strax 2015-02-06 11:23:37

+0

很高兴能有所帮助。请接受答案(如果你愿意的话可以加票) – 2015-02-06 11:26:36

+0

我试着去注册,但我需要更多的信誉。一旦我达到15我会回来upvote – strax 2015-02-06 11:27:57