简体   繁体   中英

How to natively add an image to a button in C++ WinAPI (without MFC)?

Okay, I want to add an image to a button ALONG WITH TEXT without making the button in my RC file. Is this even possible, or do I NEED to use an RC file to make the button in able to put an image in it? My image is #define d in "resource.h" and the image is declared in "resources.rc". Both "main.cpp" and "resources.rc" include the "resource.h" header. I really don't want to make a button using resources, but if it s the only way to make a button with an image AND text, then I'll do it. All I need to know is how to put an image into a button in WinAPI.

UPDATE:

  1. Add manifest file to your application, manifest file should be named YourApp.exe.manifest

  2. Add this into your manifest file(more on manifest file here http://msdn.microsoft.com/en-us/library/bb773175%28VS.85%29.aspx ):

     <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="CompanyName.ProductName.YourApplication" type="win32" /> <description>Your application description here.</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> </assembly> 
  3. Link your application with ComCtl32.lib

  4. Add the manifest to your application resource file CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "YourApp.exe.manifest"

  5. Call InitCommonControls() at the beginning of WinMain

END UPDATE

  1. example code for button creation(IMAGE + TEXT), memory leak prone because of LoadBitmap:

     HWND hwnd_button = CreateWindowEx( 0, "BUTTON", //ascii "Button text", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 10, 145, 50, 50, hwnd_parent, NULL, //GetModuleHandle(NULL) (HINSTANCE)GetWindowLong(hwnd_parent, GWL_HINSTANCE), NULL); SendMessage((HWND) m_hWndButton, (UINT) BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1))); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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