簡體   English   中英

更改對話框win32api的背景顏色

[英]change the background color of a dialog box win32api

我正在嘗試更改對話框的背景顏色(win 7,vs2010,c ++)。

我嘗試捕獲WM_CTLCOLORWM_ERASEBKGND並更改顏色。 我設法只捕獲了WM_ERASEBKGND消息,但是通過這種方式,我在調試模式下進行了管理,以查看是否更改了窗口的背景色,但是當dialogBox完成自身的上載時,該顏色被默認的灰色覆蓋了。對話框。

我正在使用CreateDialogParam函數創建DialogBox。

case WM_ERASEBKGND:
{
   HBRUSH brush;
   RECT rect;
   brush = CreateSolidBrush(RGB(255,0,0));
   SelectObject((HDC)wParam,brush);
   GetClientRect(m_hDlg,&rect)//m_hDlg is HWND type
   Rectangle((HDC)wParam,rect.left,rect.top,rect.right,rect.bottom);
   break;
}

我試圖使用該功能:

SetBkMode((HDC)wParam,TRANSPARENTE);

但這沒有幫助。

我該怎么辦?

您需要處理WM_CTLCOLORDLG 您應該返回一個刷柄。 例如,將背景設為白色:

case WM_CTLCOLORDLG:
    return (INT_PTR)GetStockObject(WHITE_BRUSH);

響應WM_ERASEBKGND ,必須return TRUE ,否則默認對話框過程會將對話框的顏色設置為默認系統顏色。

另外,對SelectObject的調用中的第二個參數是錯誤的,它應該是TRANSPARENT ,而不是TRANSPARENTE。

正如arx成員所說,您需要處理WM_CTLCOLORDLG消息才能實現所需的功能。

這是一個小型演示應用程序,您可以將其復制並粘貼到Visual Studio中:

在您的resource.h粘貼:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Dlg bkgnd.rc
//
#define IDD_DIALOG1                     101
#define IDC_BUTTON1                     1001

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1002
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif 

不要忘記上面的空行!

在您的資源文件( .rc擴展名)中粘貼以下內容:

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOG1 DIALOGEX 0, 0, 200, 124
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,52,71,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,111,71,50,14
    PUSHBUTTON      "",IDC_BUTTON1,47,23,73,16
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO 
BEGIN
    IDD_DIALOG1, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 189
        TOPMARGIN, 7
        BOTTOMMARGIN, 117
    END
END
#endif    // APSTUDIO_INVOKED

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

最后, main.cpp

#include "resource.h"
#include <windows.h>

// variable for storing the instance

static HINSTANCE hInst; 

// handle for dialog box

static HWND Dlg;

// dialog procedure

INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    static HBRUSH testBrush;

    switch(Message)
    {
    case WM_INITDIALOG:

          //set text of our static control with the data sent to dialog
          SetWindowText( GetDlgItem( hwnd, IDC_BUTTON1 ), (LPCWSTR)lParam );

          //initialize the brush
          testBrush = CreateSolidBrush( RGB( 255, 0, 0 ) );

          return TRUE;

    case WM_CTLCOLORDLG:
          return (INT_PTR)( testBrush );

    case WM_CLOSE: 
          // if we do not use stock brush we must destroy it
          DeleteObject( testBrush );
          DestroyWindow( hwnd );
          Dlg = NULL;
          return TRUE;
    case WM_COMMAND:
          switch(LOWORD(wParam))
          {
               case IDOK:
               case IDCANCEL:

                    // if we do not use stock brush we must destroy it
                    DeleteObject( testBrush );
                    DestroyWindow( hwnd );
                    Dlg = NULL;
                    break;
          }
          break;
    default:
          return FALSE;
    }
    return TRUE;
}

// WinMain's procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
          {
               Dlg = NULL;

               // create button we can click on 
               HWND btn = CreateWindowEx( 0, L"Button", L"Click me!", 
                                          WS_VISIBLE | WS_CHILD | SS_NOTIFY, 
                                          65, 10, 70, 50, hwnd, 
                                          (HMENU)4000, hInst, 0);
          }
          return (LRESULT)0;

    case WM_COMMAND:

          switch( LOWORD(wParam) )
          {
               case 4000: // activate dialog box
                   {
                        // we will send anything as the parameter to the dialog
                        // this is just an example
                        Dlg = CreateDialogParam( hInst, MAKEINTRESOURCE(IDD_DIALOG1),
                                                 hwnd, DlgProc, (LPARAM)L"Test text" );
                        //show it
                        ShowWindow( Dlg, SW_SHOW );
                   }
                   break;

               default: // pass it to the default window procedure
                   return DefWindowProc(hwnd, msg, wParam, lParam);
          }
          break;

    case WM_PAINT:
         {
               // we do not paint anything
               PAINTSTRUCT ps;
               BeginPaint( hwnd, &ps);
               EndPaint( hwnd, &ps);
         }
         return (LRESULT)0;

    case WM_CLOSE:

         DestroyWindow(hwnd);

         return (LRESULT)0;

    case WM_DESTROY:

         PostQuitMessage(0);

         return (LRESULT)0;

    default:
         return DefWindowProc(hwnd, msg, wParam, lParam);
   }
   return 0;
}

// WinMain

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
               int nCmdShow)
{
   // store hInstance in global variable for later use

   hInst = hInstance;

   WNDCLASSEX wc;
   HWND hwnd;
   MSG Msg;

   // register main window class

   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = 0;
   wc.lpfnWndProc = WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInst;
   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   wc.hCursor = LoadCursor( NULL, IDC_ARROW );
   wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
   wc.lpszMenuName = NULL;
   wc.lpszClassName = L"Main_Window";
   wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
       MessageBox(NULL, L"Window Registration Failed!", L"Error!",
                  MB_ICONEXCLAMATION | MB_OK);

       return 0;
    }

    // create main window

    hwnd = CreateWindowEx( 0,
                           L"Main_Window", 
                           L"test app", 
                           WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT, 
                           CW_USEDEFAULT, 
                           200, 100, NULL, NULL, hInstance, 0 );

    if(hwnd == NULL)
    {
       MessageBox(NULL, L"Window creation failed!", L"Error!", 
                  MB_ICONEXCLAMATION | MB_OK);

       return 0; 
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        // check if message is for dialog or main window
        if(!IsDialogMessage( Dlg, &Msg ) )
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }

    return Msg.wParam;
}

特別要注意消息循環以及我存儲對話框的HWND的方式。

還要注意,您創建的畫筆是不再需要時必須銷毀的畫筆(通常是響應WM_CLOSE )。

如果有什么我可以幫助的,請問我,我會盡力幫助您的。

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM