How to explicitly set the taskbar icon?

In Visual Studio, I created a simple old Win32 application and shared all resources and generated code so that my application consisted of this:

#include "stdafx.h" #include "IcoTest.h" int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { ::MessageBox( NULL, L"Testing", L"Test", MB_OK ); } 

When I run the application, this is what I see:

screenshot http://i43.tinypic.com/a0c68m.png

So the question is, can I change the default application icon on the taskbar? If so, what code do you need to add for this?

Edit:

That's what I did, and such work, but it is not perfect. The new icon displays well, but the taskbar preview window in Vista does not work, and the system menu does not work, so I just leave it alone.

 HWND CreateDummyWindow(HINSTANCE hInstance, int iconId, LPCTSTR taskbarTitle) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = DefWindowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(iconId)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = 0; wcex.lpszMenuName = 0; wcex.lpszClassName = taskbarTitle, wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(iconId)); ATOM atom = RegisterClassEx(&wcex); HWND wnd = ::CreateWindow( wcex.lpszClassName, taskbarTitle, WS_ICONIC | WS_DISABLED, -1000, -1000, 1, 1, NULL, NULL, hInstance, NULL); return wnd; } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { HWND wnd = CreateDummyWindow(hInstance, IDI_ICON1, _T("Test") ); ::MessageBox( wnd, _T("Testing"), _T("Test"), MB_OK ); ::DestroyWindow( wnd ); } 
+4
source share
6 answers

The icon displayed on the taskbar is taken from the window itself. If the only window is the standard Windows MesssageBox, then you will get some kind of default OS. You must create your own window and give it an icon, then Windows will use it.

+2
source

This looks like sample code. If the real code is an unconfigured Windows application, you can do this:

Give the main application window a taskbar icon by calling SetIcon () . Then, when you call MessageBox (), set the first parameter to the HWND of your application’s main window.

+3
source

In this particular case (one call to MessageBox in the WinMain function), you can connect the message box dialog box and set the icon there. Like this:

 HHOOK g_hMsgBoxHook; HINSTANCE g_hInstance; LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam) { if(nCode == HC_ACTION) { CWPSTRUCT* pcwp = (CWPSTRUCT*)lParam; if(pcwp->message == WM_INITDIALOG) { HICON hIcon = NULL; HICON hIconBig = NULL; // Small icon. hIcon = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0); if(hIcon) { SendMessage(pcwp->hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); } // Big icon. hIconBig = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0); if(hIconBig) { SendMessage(pcwp->hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIconBig); } } } return CallNextHookEx(g_hMsgBoxHook, nCode, wParam, lParam); } int CALLBACK wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow ) { g_hInstance = hInstance; g_hMsgBoxHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, NULL, GetCurrentThreadId()); MessageBoxW(NULL, L"Testing", L"Test", MB_OK); // ... UnhookWindowsHookEx(g_hMsgBoxHook); } 

Where IDI_MYICON is the icon resource identifier.

+2
source
 WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass); // .. wndclass.hIconSm = ExtractIconEx( ... ); RegisterClassEx(&wndclass); HWDN wnd = CreateWindow(...) 
0
source

Why not just add the icon resource to the exe? I'm sure Windows will try this before returning to the "common" icons.

0
source

Create a form, but don’t show it, and then assign an icon to it and use it as the parent of your message box.

This hides the icon:

 using (var f = new Form()) { MessageBox.Show(f,"my message"); } 

This will create an icon:

 using (var f = new Form()) { f.Icon = Resources.IconUpload; f.Location=new Point(-1000,-1000); f.StartPosition = FormStartPosition.Manual; f.Show(); MessageBox.Show(f,"my message"); } 
-3
source

Source: https://habr.com/ru/post/1286430/


All Articles