
The header and resource files are all exactly like your ones (they have the includes).When creating buttons, you can just add an image (png, svg, etc) to it relying on styles in the CSS, as done here as well. I also tried the codes you gave me for this file in the WM_CREATE case of the windowProcedure function but it didn't make any difference. PostQuitMessage (0) /* send a WM_QUIT to the message queue */ break ĭefault: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam) Switch (message) /* handle the messages */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
#Mfc set icon on button windows
* This function is called by the Windows function DispatchMessage() */ * The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam * Translate virtual-key messages into character messages */ It will run until GetMessage() returns 0 */ while (GetMessage (&messages, NULL, 0, 0)) * Make the window visible on the screen */ SendDlgItemMessage(GetDlgItem(hwnd,IDB_BUTTON),BM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)hMyIcon) HMyIcon=LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, icon_size, icon_size, NULL) HThisInstance, /* Program Instance handler */ HWND_DESKTOP, /* The window is a child-window to desktop */ WS_OVERLAPPEDWINDOW, /* default window */ĬW_USEDEFAULT, /* Windows decides the position */ĬW_USEDEFAULT, /* where the window ends up on the screen */ SzClassName, /* Classname */ "Windows App", /* Title Text */ * The class is registered, let's create the program*/Ġ, /* Extended possibilites for variation */ * Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) Wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND Wincl.cbWndExtra = 0 /* structure or the window instance */ /* Use Windows's default color as the background of the window */

Wincl.cbClsExtra = 0 /* No extra bytes after the window class */ Wincl.hCursor = LoadCursor (NULL, IDC_ARROW) Wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION) Wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION) Wincl.style = CS_DBLCLKS /* Catch double-clicks */ Wincl.lpfnWndProc = WindowProcedure /* This function is called by windows */ WNDCLASSEX wincl /* Data structure for the windowclass */ /* The Window structure */ MSG messages /* Here messages to the application are saved */ HWND hwnd /* This is the handle for our window */ Int WINAPI WinMain (HINSTANCE hThisInstance,

* Make the class name into a global variable */ char szClassName = "WindowsApp" LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM) #include #include "header.h" /* Declare Windows procedure */ If you do make that change, then don't call DestroyIcon() in your cleanup. That is the ONLY change you should make, if you are going to make any changes at all. The ONLY change you might make is the one modoran showed me. Create a clean project and follow the instructions EXACTLY. You are clearlly not following the instructions. Once again, here is an example of an app I've created with button icons. You have to put the appropriate statements I showed you above in the appropriate HEADER file you have to put the appropriate statements in the appropriate RESOURCE file IN THE RIGHT ORDER as I showed you you have to put the appropriate statements in the appropriate CPP file in the appropriate functions as I showed you.Īs I said, I do this ALL THE TIME with the EXACT SAME CODE. You don't have to name the file header.h or resource.rc or foo.cpp, but YOU DO, YOU MUST put the statements EXACTLY as they are written EXACTLY in the appropriate file, in EXACTLY the order I showed you.
#Mfc set icon on button code
You need to just create a clean project with a simple window, and you need to put the EXACT CODE in the EXACT SAME FILES as I showed you above. If you use the code EXACTLY as I have posted it above, then it will work. Sorry for being annoying but I've tried a lot to make it work With all these codes it still doesn't work, and the button doesn't appear at all.Ĭould you please write a code which works in the way I wrote mine, please? note: the two lines above are connected but they wouldn't fit in one line here.

This is the part of my code related to the button:

I don't know what my mistake is, so if you don't mind could you please post a code I could just copy so that it would work? I've done everything you said but it still disappears. I tried what you said but it didn't change anything. Thanks for your help, Lamblion but it didn't work.
