1. Icon Introduction
has recently begun to touch Windows programming, so it organizes and records some of the fragmented knowledge it touches. This article mainly describes how to change the icon of the Windows dialog window. Here's the first introduction to the Windows icon definition. There are typically two icons on the Windows app we use. One is a large icon file that appears when the app is opened with a small icon file (SMALL icon) in the upper-left corner and when you press ALT + TAB to toggle the window. As an example of the Windows-brought Notepad program, the small icon file looks like 1:
Figure 1 Small ICON for Notepad program
The large Icon style 2 shows:
Figure 2 Notepad program's big ICON
2. Set icon Implementation
After the introduction of the icon definition above, go to the main content of this article--Set the Windows window's icon by programming windows. The entire Setup icon process is divided into the following steps:
2.1 Get the ICO resource file
(1) First can go to Baidu image search "ico" to find a suitable icon file, and save to Local. (Note that this is a GIF or PNG file that needs to be converted to an ICO file online)
(2) Convert the icon file downloaded in the previous step (GIF or PNG, etc.) to ICO (the following address can refer to http://www.img2ico.net/) The example used in this article is shown in the icon 3:
Figure 3 Example icon
2.2 Create a new empty Win32 project, set up a basic dialog box and load the icon resource
(1) Resource Files, Add, Resource, Dialog, New//New dialog window, the effect is as follows
Figure 4 Creating a New dialog box window
(2) Resource files, Add, Resource, Icon, import//Introduction to the ICO file generated in step 1
Figure 5 Introducing the icon resource
(3) Modify the generated dialog window ID is idd_main, the icon file ID is idi_appicon (renamed after the meaning of more explicit, and easy to follow the call memory convenient)
2.3 Program implementation: Set the icon of the window
(1) Design the test window Ctestdlg class, and define the window procedure function and other initialization, close operation. The Ctestdlg window is defined first, and the associated message handler functions Test_proc, Test_oninitdlg, Test_oncommand, and test_onclose are defined.
/************************************************************************//*file: Define a Test window class, use Sit to change window icon's example window * Author:huagang Li * date:2014-8-21 09:42:53 * tips:1. Set the window to a unified class for easy addition Add a new feature * 2. The following ifndef is followed by a sequence of strings generated by the GUID generator, guaranteeing uniqueness **//************************************************************************/#ifndef _e159c66b_2cd4_4e0e_861e_9328e6e99b66_h_#define_e159c66b_2cd4_4e0e_861e_9328e6e99b66_h_#include<Windows.h>classctestdlg{ Public: StaticBOOL WINAPI Test_proc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM);Private: StaticBOOL Test_oninitdlg (HWND hwnd, HWND Hwndfocus, LPARAM LPARAM); StaticBOOL Test_oncommand (HWND hwnd,intID, HWND hwndctl, LPARAM LPARAM); StaticBOOL Test_onclose (HWND hwnd);};#endif //_e159c66b_2cd4_4e0e_861e_9328e6e99b66_h_
The specific interface implementation is as follows:
#include"SetIcon.h"#include"resource.h"#include<WindowsX.h>//procedure handler function for testing windowBOOL WINAPI Ctestdlg::test_proc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {Switch(umsg) {handle_msg (hWnd, Wm_initdialog, Test_oninitdlg); Handle_msg (HWnd, WM_COMMAND, Test_oncommand); Handle_msg (HWnd, Wm_close, test_onclose); } returnFALSE;}//test the initialization function of the window, set the window icon can be done hereBOOL Ctestdlg::test_oninitdlg (HWND hwnd, HWND Hwndfocus, LPARAM LPARAM) {bool BRet=TRUE; Do { //Set the window icon for the downloaded ICOHINSTANCE hinstance =:: GetModuleHandle (NULL); if(NULL = =hinstance) {BRet=FALSE; Break; } hicon Hicon=LoadIcon (hinstance, Makeintresource (Idi_appicon)); if(NULL = =Hicon) {BRet=FALSE; Break; } //set the size of the window icon//Large icon: The icon that corresponds when you press ALT + TAB to toggle the window//Small icon: The icon that corresponds to the upper-left corner of the window:: SendMessage (HWnd, Wm_seticon, Icon_big, (LPARAM) hicon); :: SendMessage (HWnd, Wm_seticon, Icon_small, (LPARAM) hicon); } while(false); returnBRet;}//window Other command response functionBOOL Ctestdlg::test_oncommand (HWND hwnd,intID, HWND hwndctl, LPARAM LPARAM) { returnTRUE;}//Close the test windowBOOL Ctestdlg::test_onclose (hwnd hwnd) {:: EndDialog (hwnd),0); returnTRUE;}
(2) Set icon core code as follows ( set icon's core section )
////////////////////////////////////////////////////////////////// // Core Code Area HINSTANCE hinstance == LoadIcon (hinstance, Makeintresource (Idi_appicon)); // set the size of the window icon // Large icon: The icon that corresponds when you press ALT + TAB to toggle the window // Small icon: The icon that corresponds to the upper-left corner of the window :: SendMessage (hwnd, Wm_seticon, Icon_big, (LPARAM) hicon);:: SendMessage (hwnd, Wm_seticon, Icon_small, (LPARAM) HICON); //////////////////////////////////////////////////////////////////
(3) Write the main function, and instantiate the Test window, run the observation of the actual effect. The main functions are as follows:
/************************************************************************//*file: Entry point of the program * Author:huagang Li * date:2014-8-21 10:01:11 * Tips: Implement the definition of the window in other classes, the main entry only save the window to instantiate **//************************************************************************/#include<windows.h>#include<CommCtrl.h>#include"SetIcon.h"#include"resource.h"//InitCommonControls relies on the library that needs to be added here#pragmaComment (lib, "Comctl32.lib")intWINAPI WinMain (__in hinstance hinstance, __in_opt hinstance hprevinstance, __in LPSTR lpCmdLine, __inintnShowCmd) {InitCommonControls (); ::D Ialogbox (hinstance, Makeintresource (Idd_main), NULL, Ctestdlg::test_proc); returnexit_success;}
Finally, look at the results after the run:
Figure 6 Running results-small ICON
Figure 7 Run result-Large icon
As can be seen from figures 6 and 7, the icon of the window has changed, proving the feasibility of this method.
3. Summary
One sentence summary, the way to set up Windows window icon is to prepare the icon resource and send it to the operating system Wm_seticon message.