how to set image in dialogbox in c++ win32 API?

泪湿孤枕 提交于 2021-02-04 21:10:31

问题


i have developing a C++ Api project.

i will use dialogboxparam to create a dialogbox...

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:

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


回答1:


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

There is an MFC based example here:

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




回答2:


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).

Then use hImageCtl to send the STM_SETIMAGE message to.




回答3:


This works for years, since Windows 98 for me:

//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.



来源:https://stackoverflow.com/questions/12858841/how-to-set-image-in-dialogbox-in-c-win32-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!