更改对话框 win32api 的背景颜色 [英] change the background color of a dialog box win32api

查看:214
本文介绍了更改对话框 win32api 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改对话框的背景颜色(win 7、vs2010、c++).

I'm trying to change the background color of a dialog box (win 7, vs2010,c++).

我试图捕捉WM_CTLCOLORWM_ERASEBKGND 并改变颜色.我设法只捕获了 WM_ERASEBKGND 消息,但通过这种方式,我在调试模式下管理以查看我更改了窗口的背景颜色,但是当对话框完成上传时,颜色溢出通过对话框的默认灰色.

I tried to catch WM_CTLCOLOR, WM_ERASEBKGND and change the color. I managed to catch only the WM_ERASEBKGND message but in this way I manage in debug mode to see that I changed the background color of the window, but when the dialogBox is finish to upload itself, the color is overrun by the defualt grey color of the DialogBox.

我正在使用 CreateDialogParam 函数创建对话框.

I'm creating the DialogBox using the CreateDialogParam func.

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_ERASEBKGND 时,你必须return TRUE,否则你的默认对话框程序会设置颜色对话框的默认系统颜色.

When you respond to WM_ERASEBKGND you must return TRUE, otherwise your default dialog procedure will set the color of the dialog to the default system color.

此外,您在调用 SelectObject 时的第二个参数是错误的,它应该是 TRANSPARENT,而不是 TRANSPARENTE.

Also, your second parameter in your call to SelectObject is wrong, it should be TRANSPARENT, not TRANSPARENTE.

正如成员 arx 所说,你需要处理 WM_CTLCOLORDLG 消息来实现你想要的.

As member arx said, you need to handle WM_CTLCOLORDLG message to achieve what you want.

这是一个小型演示应用程序,您可以将其复制并粘贴到 Visual Studio 中:

Here is a small demo app that you can copy and paste into your 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 扩展名)中粘贴:

In your resource file ( .rc extension ) paste this:

// 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的方式.

Especially pay attention to the message loop, and the way I have stored the HWND of the dialog.

另请注意,您创建的画笔是您不再需要时必须销毁的画笔(通常响应WM_CLOSE).

Also note that the brush you create is the brush you must destroy when you no longer need ( usually in response to WM_CLOSE ).

如果还有什么我可以帮忙的,尽管问我,我会尽力帮助你.

If there is anything else I can do to help, ask me and I will try to help you.

最好的问候.

这篇关于更改对话框 win32api 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆