change icon of a dialog - c++

i am recently editing an open source project in visual c++ 2010 , i don't know much about vc++ ,(i have only 5 days of experience in c++), with my little knowledge i am changing some user interface of the project
there is only one icon in my project ,the apps shows the main icon as icon , at the same time i want to make that icon to be on the title of a dialog also (the dialog will be shown when a button in main form is clicked),
the dialog is already in the resources/dialogs but i want to change the icon of it ;

You need to find the dialog procedure of the dialog you're interested in, and in the WM_INITDIALOG message handler (you need to add it if it's not already present) use WM_SETICON to set the icon:
// hIcon is your icon handle
SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);

Related

WM_SETICON not working when program launched via shortcut

I am using WM_SETICON to change the icon of an application. This works when the program is run in VisualStudio debugger, or via commandline, but uses the wrong taskbar icon when launched through a desktop shortcut on Windows 7. Right clicking to pin the program to the taskbar and then unpinning it causes the icon to display correctly.
HICON icon = (HICON) LoadImage(NULL, iconStr, IMAGE_ICON, 32, 32, LR_LOADFROMFILE| LR_SHARED);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL2, (LPARAM)icon);
It seems as though something about launching through a shortcut is causing the program to use a stale cached icon and ignore WM_SETICON. Also the small icon next to the program titlebar is updating properly, it is only the icon on the taskbar that has issues.
I really do need to set the icon programatically because it will change based on commandline. Does anyone know of a way to make this work? Thanks.
I did find a workaround. Since the exe referenced by the shortcuts can't change it's icon, it can launch an exe with a different filename that will then be able to change taskbar icons with WM_SETICON.
shortcuts (1-n) each have their own icon and point to IgnoresWMSetIcon.exe.
On startup IgnoresWMSetIcon.exe launches CanChangeIcon.exe then closes. CanChangeIcon.exe is then able to function normally.
Still interested in an explanation of why this is the case, if someone knows.

how to make a clickable button in c++ win32

anyone can tell me how to use a bitmap as a button, actually i can create a static control and could set a picture to it but the thing is that i don't know how to use it as a button, i am using c++ win32.
This is how i create the bitmap
Code:
HWND Profile_Stuff(HWND hWnd, HINSTANCE hInst)
{
HWND Profile_Pic;
Profile_Pic = CreateWindow("STATIC", NULL, SS_BITMAP|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_BORDER, 5,5,33,33, hWnd, NULL, hInst, NULL);
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "camera1.jpg", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hBmp == NULL){
MessageBox(NULL, "Error while loading image", "Error", MB_OK|MB_ICONERROR);
}
SendMessage(Profile_Pic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
return 0;
}
then i call the function in main window wm_create message handler which creates it successfully, now i don't know to use it as a button, like we have a picture of an advertisement at the bottom of bit torrent application.
i am using visual studio c++ with win32 api.
If you want a button control, you should create a button control. The visual representation can be controlled by the application. To do so, specify the BS_OWNERDRAW Button Style. A button control with this style sends a WM_DRAWITEM message to the control parent whenever a visual aspect has changed. The control parent can then render the control as it sees fit.
An introduction to owner-drawn controls is available at Custom Controls. If you wish to retain some parts of the button control (e.g. its border), see Using Visual Styles with Custom and Owner-Drawn Controls for details (or DrawFrameControl if you aren't using Visual Styles).
Fully working sample code for an owner-drawn button control can be found in this answer.
In Windows, the windows belong to a class, a the class defines the windows procedure for all windows of that class, meaning how they react to events.
If you create a STATIC window, it will not react to any click and will not be useable as a button.
You could create a custom class, register it along with a custom windows procedure able to mimic a BUTTON. But unless you have very special requirements just create an owner drawn button as shown in #IInspectable's answer

top-left corner icon on a dialog from resources in C++

thank you in advance.
I'm developing an application in Win32 C++ which its main window comes up from a resource file because it's easier to place controls.
to that I use CreateDialog a statement
The problem is, I don't achieve to put it an Icon such as I could with CreateWindow statement at the WNDCLASSEX sctructure.
does somebody know the way to get a dialog with that top-left-corner Icon?
Thanks
You can enable your application to display an icon in the title bar of a dialog box by adding the WS_SYSMENU and WS_CAPTION styles to the dialog box template and sending the WM_SETICON message from within the dialog box procedure in response to the WM_INITDIALOG message.
CreateDialog returns the window handle of the dialog. Send that window WM_SETICON messages to specify the icon for the window. Or indeed send the messages in response to WM_INITDIALOG.

Setting icon for application VS Express 2012 c++

I have been trying to get an icon added to my application as a resource, so that it is displayed with my application in VS 2012 Express using c++. So far I have picked up the following code to add to my rc file from other questions and forums.
IDI_APP ICON "resources/Icon.ico"
The icon is displayed on the desktop with the exe, and on the taskbar when the program is running. However in certain situations such as on the task manager the icon for the application dies not show up and instead the default program icon is displayed. I was wondering if anyone knows how to alter my code so that the icon is always associated with my program. I have heard that the problem may result from needing different sized icons however I have many sizes of icons within my ico file created with the program IcoFX. I was also wondering if I needed to programmatically set the icon for it to work in any place the application is associated with. I have tried rebuilding and renaming my program to update the icon in the shell. I am using a sfml window instead of the winapi and a HWND window.
Double check that you created a single .ico file with multiple resolutions, usually 16x16, 32x32, 48x48, 96x96.
Load your icon with something like
ICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
Notify with Windows messages of the icon
//Change both icons to the same icon handle.
SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);
//This will ensure that the application icon gets changed too.
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
Finally reset the shell icon cache as described here or by just restarting/logging out.
Additional resources and references:
https://stackoverflow.com/a/19656000/1938163
https://stackoverflow.com/a/2723270/1938163

How to change the icon in taskbar using windows api

I am writing a small windows application.
I want to create a installer using nsis script.
I know how to change the default application icon, start menu icon and desktop shortcut icon by using
Application icon : !define MUI_ICON "${INSTALL_ICON}"
Shortcut on start menu : CreateShortCut "$SMPROGRAMS\$StartMenuFolder\shorcutName.lnk" "$INSTDIR\executableName.exe" "" "$INSTDIR\${INSTALL_ICON}" 0
Shortcut on Desktop : CreateShortCut "$DESKTOP\shorcutName.lnk" "$INSTDIR\executableName.exe" "" "$INSTDIR\${INSTALL_ICON}" 0
But I want to also change the icon shown on the top left of the application window.
And icon shown in task manager and icon shown on task bar. I think should be done using winapi.
Any help would be appreciated.
Thanks in advance
It's important to change all icons, including the application, both small and big:
//Change both icons to the same icon handle.
SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);
//This will ensure that the application icon gets changed too.
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);