I have the process name and the handle (HWND) of its window. I want now to get the relative icon (if available). Searching through MSDN, I found ExtractIcon() to get the handle to the icon from a given exe name, and GetIconInfo() to get "information" of the icon from the HICON. I don't know if it's the right way to do this, and how to retrieve correct information to show (in a second moment) the icon without the handle to the icon.I have to send this information to another process (through socket) that has to show the icon.In the ICONINFO structure there are HBITMAP fields that contains the bitmap (black&white and with colour). Is it useful?
you can use the API GetClassLong to retrieve the icon associated with your program then use SendMessage API passing the hwnd of of the window you want to change it's icon.
in this Example I extracted the icon from my application then set it to Calculator. my windows calculator is open before sending to it the icon:
case WM_LBUTTONDOWN: // just for explanation so left clicking in your client area and see the result
{
HICON icon = (HICON)GetClassLong(hWnd, GCL_HICON);
HWND hCons = FindWindow(NULL, "Calculator"); // I already opened windows calculator. you can use any other window but be sure to get its valid Handle
if(!hCons)
MessageBox(0, "\"Calculator\" windows is not found!", 0, MB_OK|MB_ICONHAND);
SendMessage(hCons, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)icon); // setting the icon
}
break;
Related
Im trying to open a Dialog box in Windows machine(using windows credential provider), when the user presses a button. i tried the below code but, dialog box is not getting opened. i have a resource "IDD_DIALOG1" and callback method "ChangePasswordProc".
HWND hwndOwner = nullptr;
::DialogBox(HINST_THISDLL, MAKEINTRESOURCE(IDD_DIALOG1), hwndOwner,ChangePasswordProc);
I didn't write in Windows GUI for a looong time, but perhaps try something like this:
HWND dialog = ::DialogBox(HINST_THISDLL, MAKEINTRESOURCE(IDD_DIALOG1), hwndOwner,ChangePasswordProc);
ShowWindow(dialog, SW_SHOW);
I remember, that creating window does not imply showing it - it must be done explicitly.
To create any window from inside of Credential Provider you must first get parent window handle by calling OnCreatingWindow method of ICredentialProviderCredentialEvents interface.
HRESULT OnCreatingWindow([out] HWND* phwndOwner);
Pointer to this interface is supplying to your provider by calling Advise method of its ICredentialProviderCredential interface :
HRESULT Advise([in] ICredentialProviderCredentialEvents* pcpce);
Have a look at this post.
Is there a way to programmatically hide an application on windows? I want to achieve the same thing as the windows+D shortcut, but for a single application. I want to do this from within that application (application consists of several windows, one of those can't be moved, resized, closed or minimized by the user). Application is written in c++ and uses Qt for the UI.
to do so it's so easy:
1- retrieve the handle to that window:
HWND hChild = GetDlgItem(hWnd, ID_MYCHILD);
2- send to it SW_SHOW either using ShowWindow or via SendMessage:
ShowWindow(hChild, SW_HIDE); // hide
ShowWindow(hChild, SW_SHOW); // show
SendMessage(hChild, SW_HIDE, 0, 0); // hide
SendMessage(hChild, SW_SHOW, 0, 0); // show
if the window doesn't belong to your application then:
1 - retrieve the main window with:
HWND hWnd = GetForegroundWindow(void);
2- use the above to hide/show it
ShowWindow(HwndWindow, SW_MINIMIZE);
Here's the MSDN ShowWindow documentation.
In addition you may find EnumChildWindows useful for finding all these windows if their handles aren't readily available to you.
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
With the following code part, I can get the handle of the open file dialog box that is opened. That works fine.
HWND handleForWindow = FindWindow(NULL, "Open");
Now, I need to type the filepath + filename into the textbox area in the same dialog box. For this, I need to get the handle of the TextBox. I have been using Spy++ and it worked great so far. However, it doesn't get the handle of the TextBox. Spy++ says that the class is "Edit" and has no caption.
I tried the following and some other possibilities with the caption and the class name but no luck.
HWND hwndTextBox = FindWindowEx(handleForWindow, 0, "Edit", "Caption");
Actually the TextBox is a part of ComboBox. If I put there "ComboBox", it gets the handle nicely. But in that case if I send a text message to the box, it doesn't put any text there.
SendMessage(hwndTextBox, WM_SETTEXT, 0, (LPARAM)"AnyText");
Since this doesn't work, I had an idea of getting the current active element in the window because the TextBox comes active all the time whenever it is opened. But I couldn't find any Api call for this. Could anyone help me about that?
Thanks.
You can send a CB_GETCOMBOBOXINFO message to a ComboBox to retrieve the HWND of its edit box.
If you are trying to interact with an Open File dialog from the Common Dialog library, you can send CDM_GETFOLDERPATH and CDM_GETFILEPATH messages to the dialog itself to get its current path and filename, and a CDM_SETCONTROLTEXT message to change the filename displayed in the dialog's cmb13 or edt1 field (see Explorer-Style Control Identifiers).
(I'm using Visual Studio 2008, though I remember having similar problems with older versions as well.)
I've tried several different methods (many of them mentioned in this other question), but I am still having some strange issues:
When including an icon as a resource, it does show up as the executable file's icon immediately, but for the icon to show up on the taskbar, I have to restart the computer. Until then, it continues to show up as whatever the previous icon was. Cleaning the solution, restarting VS, doesn't have any effect. Not a really big issue, as it won't affect a released exe, but it would be nice to know where it's keeping the old icon cached and how to get rid of it.
No matter what I do, the icon displayed when alt-tabbing is the default app icon (square and white and generic). This includes embedding the icon in the executable, as well as setting ICON_BIG with WM_SETICON.
As for the second matter, my code looks something like:
hIcon = (HICON)(
LoadImage( NULL, szFilename, IMAGE_ICON, 32, 32, LR_LOADFROMFILE ) );
if( hIcon )
SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
However, after sending WM_SETICON, GetLastError() returns 6, "The handle is invalid.". hWnd is a valid window handle, and hIcon appears to be a valid icon handle. I've tried searching for reasons why WM_SETICON could cause that error, and at the very least, to figure out WHICH handle it thinks is invalid, but no luck yet. I've cleared the error code immediately before calling SendMessage(), so it has to be set somewhere in the processing of the message.
I tried an alternate method, loading the icon from the exe itself, where the ID of the resource is 101 (it's the first and only resource included):
hIcon = (HICON)(
LoadImage( GetModuleHandle( NULL ), MAKEINTRESOURCE( 101 ),
IMAGE_ICON, 48, 48, 0 ) );
if( hIcon )
SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
... but the same thing happens; after calling SendMessage(), GetLastError() gives the same error status.
I've tried different dimensions (such as 48x48, all of which are present in the icon file), but to no different effect. I know it's definitely finding and loading the images, because if I specify a size that doesn't exist or an invalid resource ID or the wrong filename (depending on how I am loading it), it fails out long before SendMessage().
Strangely, if I specify ICON_SMALL instead of ICON_BIG, the call succeeds with no error status, but from the docs, I need to use ICON_BIG to set the icon used while alt-tabbing. Also, if I use ICON_BIG but load the 16x16 icon, I get no error status, but nothing changes.
Any ideas about what could be causing WM_SETICON to fail? Anything terribly wrong with any of the code I've posted (aside from formatting/style/casting issues, as it's simplified to just the basics)?
I've revisited this to see if I can close out my question. I have been unable to get the app's icon to show up in the alt-tab list just through embedding it in the executable; it will show up in the taskbar, as the file's icon in Explorer, and elsewhere just fine.
I figured I'd try something simpler for setting the icon manually, and went with LoadIcon() instead, as the code below shows:
HICON hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1) );
if( hIcon )
{
SendMessage( GetHandle(), WM_SETICON, ICON_BIG, (LPARAM)hIcon );
DestroyIcon( hIcon );
}
// ... Same for ICON_SMALL
This seems to have done the trick. I really don't know why, but so far it's the only change that had any effect. It's really not an issue I should spend any more time on, so I'll just go with this.
OK, this worked a treat for me :
HICON hIconSmall =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,16, 16, 0);
HICON hIconLarge =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,256, 256, 0); // Big for task bar, small loaded otherwise.
SendMessage(yourWindowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall) ;
SendMessage(yourWindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconLarge) ;
I've used a single .ico file with multiple resolutions 16x16, 32x32, 48x48, 96x96 without problems, as application icon. Then windows will pick up the right size.
The windows shell have an icon cache, there is a trick to restart it without rebooting or logout from the current session, or killing explorer.exe from task manager.
For anyone coming to this same difficulty if you are going to change ICON_BIG you must first send WM_SETICON with ICON_SMALL and then proceed to ICON_BIG.
For example:
SetLastError(0);
SendMessage(windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)iconsmallHandle);
[do error handling]
SetLastError(0);
SendMessage(windowHandle, WM_SETICON, ICON_BIG, (LPARAM)iconbigHandle);
[do error handling]
You will need to use SetLastError after the first SendMessage to clear any returned error.
If you are just setting ICON_SMALL you can ignore ICON_BIG. For whatever reason in all of my tests you must set ICON_SMALL regardless if that icon needs to change or not, before attempting to change ICON_BIG, otherwise you will always get error code 0x6 (invalid handle).
If found the solution for me. I created an invisible CFrameWnd application window and then a few other main windows (which are dialog windows). The magic undocumented rule is: You have to change the big icon in the first created CFrameWnd. While each window keeps it's own ICON_BIG instance, it does not use them. So it seems not possible to show different taskbar icons for different windows inside one application.