我用API创建了一个窗口,现在想修改其背景色,请问用API该怎么做?谢谢!

解决方案 »

  1.   

    转载自 jennyvenus:调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置为蓝色背景和黄色文本。 
    BOOL CSampleApp : : InitInstance ( ) 

    … 
     
    //use blue dialog with yellow text . 
    SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) ) 
     
    … 

     
    需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说明了创建一个红色背景对话的步骤。 
     
    首先,给对话基类增加一人成员变量 
    CBursh :class CMyFormView : public CFormView 

    … 
     
    private : 
    CBrush m_ brush // background brush 
     
    … 

     
    其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。 
    CMyFormView : : CMyFormView ( ) 

    // Initialize background brush . 
    m_brush .CreateSolidBrush (RGB ( 0, 0, 255) ) 

     
    最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。 
    HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor 

     

    // Determine if drawing a dialog box . If we are, return +handle to 
    //our own background brush . Otherwise let windows handle it . 
    if (nCtlColor = = CTLCOLOR _ DLG ) 
    return (HBRUSH) m_brush.GetSafeHandle ( ) 
    return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor 

    }
      

  2.   

    COLORREF SetBkColor(HDC hdc, COLORREF crColor ); 
      

  3.   

    处理WM_ERASEBKGND,在wParam中包含HDC信息,利用此既可判别窗口
      

  4.   

    Maybe you can write codes to respond to the WM_PAINT message, in this way you can paint any color even a bitmap on the window.
      

  5.   

    写了一个试,但结果却很奇怪,WM_ERASEBKGND中写错了吗?
    #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_APPLICATION) ;
       wndclass.hCursor   = LoadCursor (NULL, IDC_ARROW) ;
      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
       wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))
         {
    MessageBox ( NULL, TEXT ("This program requires Windows NT!"), 
               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
        NULL,         // window menu handle
        hInstance,     // program instance 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 ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
    return 0 ; 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 ; case WM_ERASEBKGND:
    hdc = GetDC (hwnd) ;
    SetBkColor(hdc, RGB ( 0 ,0 , 0 ) );
      ReleaseDC (hwnd, hdc) ;
    return 0;  case WM_DESTROY:
    PostQuitMessage (0) ;
    return 0 ;
         }
       return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
      

  6.   

    Windows向窗口发送一个WM_ERASEBKGND消息通知该窗口擦除背景,可以使用 
    ClassWizard重载该消息的缺省处理程序来擦除背景(实际是画),并返回TRUE以 
    防止Windows擦除窗口。 
    //Paint area that needs to be erased. 
    BOOL CSampleView : : OnEraseBkgnd (CDC* pDC) 
    BOOL CSampleView : : OnEraseBkgnd (CDC* pDC) 

    // Create a pruple brush. 
    CBrush Brush (RGB (128 , 0 , 128) ); 
    // Select the brush into the device context . 
    CBrush* pOldBrush = pDC—>SelcetObject (&brush); 
    // Get the area that needs to be erased . 
    CRect reClip ; 
    pDC—>GetCilpBox (&rcClip); 
    //Paint the area. 
    pDC—> PatBlt (rcClip.left , rcClip.top , 
    rcClip.Width ( ) , rcClip.Height ( ) , PATCOPY ); 
    //Unselect brush out of device context . 
    pDC—>SelectObject (pOldBrush ); 
    // Return nonzero to half fruther processing . 
    return TRUE; 

      

  7.   

    另外,我是用WIN32 APPLICATION建的,且只调用API
      

  8.   

    case WM_ERASEBKGND:
         hdc = GetDC(hWnd);
         RECT rt;
         GetClientRect(hWnd, &rt);
         HBRUSH hBr;
         hBr = CreateSolidBrush(RGB(0,0,0));     FillRect( hdc,&rt ,hBr );
         ReleaseDC (hWnd, hdc) ;
         return 0;
      

  9.   

    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) 
    就是背景色啊
    修改之即可
      

  10.   

    sorry   这是msdn上对   WNDCLASS.hbrBackground的说明
    When this member is NULL, an application must paint its own background whenever it is requested to paint in its client area. To determine whether the background must be painted, an application can either process the WM_ERASEBKGND message or test the fErase member of thePAINTSTRUCT structure filled by theBeginPaint function.