简体   繁体   English

如何在C ++ Win32 API的对话框中设置图像?

[英]how to set image in dialogbox in c++ win32 API?

i have developing a C++ Api project. 我正在开发一个C ++ Api项目。

i will use dialogboxparam to create a dialogbox... 我将使用dialogboxparam创建一个对话框...

i done to create and set the textbox,labels and buttons... its work fine... 我已经完成创建和设置文本框,标签和按钮的工作了。

now i want to add a image in the top of the dialogbox... 现在我想在对话框顶部添加图像...

i did use this code in WM_INITDIALOG: 我确实在WM_INITDIALOG中使用此代码:

HBITMAP hImage= (HBITMAP)LoadImage(NULL,L"C:\\WINDOWS\\system32\\BMA-Images\\login-header",IMAGE_BITMAP,LR_DEFAULTSIZE ,LR_DEFAULTSIZE ,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
            SendMessage(_hwnd,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hImage);

But it didnt work... Can anyone help to resolve this... Thanks in advance Sonu 但这没有用...谁能帮助解决这个问题...在此先感谢Sonu

The easiest way is to override the WM_PAINT for the window and paint the bitmap at that point (between the BeginPaint and EndPaint) calls 最简单的方法是为窗口覆盖WM_PAINT并在该点(在BeginPaint和EndPaint之间)调用绘制位图

There is an MFC based example here: 这里有一个基于MFC的示例:

http://www.programmersheaven.com/mb/mfc_coding/113034/113034/how-do-i-set-a-background-picture-in-a-dialog-box-/ http://www.programmersheaven.com/mb/mfc_coding/113034/113034/how-do-i-set-a-background-picture-in-a-dialog-box-/

When processing the WM_INITDIALOG message use HWND hImageCtl = GetDlgItem(_hwnd, <image-control-resource-id>) to get the handle of the image-control (this assumes _hwnd is the handle to the dialog itself). 处理WM_INITDIALOG消息时,请使用HWND hImageCtl = GetDlgItem(_hwnd, <image-control-resource-id>)获取图像控件的句柄(假定_hwnd是对话框本身的句柄)。

Then use hImageCtl to send the STM_SETIMAGE message to. 然后使用hImageCtlSTM_SETIMAGE消息发送到。

This works for years, since Windows 98 for me: 自Windows 98对我以来,这已经有效了多年:

//globals
HBRUSH hbr;
PAINTSTRUCT wcd;

LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,
   LPARAM lParam )
{

   switch( msg ) {

   case WM_PAINT:
       if (GetUpdateRect(hWnd,r,0)) {   
           BeginPaint(hWnd,&wcd);
           if (wParam == NULL) FillRect(wcd.hdc,&wcd.rcPaint,hbr);
           EndPaint(hWnd,&wcd);         
       } 
        break;

      case WM_COMMAND:
///your code
   }
}



int  WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int Show)
{  
    ghInstance = hInstance;
    //Prepare brush for background
    hbr=CreatePatternBrush(LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BGROUND)));
///your code
    DialogBox(hInstance,"MAIN", NULL,(DLGPROC)MainWndProc);
///your code
    return(FALSE);
}

IDB_BGROUND - id of image resource, linked in. IDB_BGROUND-链接到的图像资源的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM