Set the program icon in the code

Source: Internet
Author: User

I recently studied the Win32 compilation tutorial of Luo yunbin, and I also learned about the Windows API and message mechanism. I encountered some problems when learning the "icon and cursor" section. Here we will list the main experiment steps and results for future reference.

I. Preparations before the experiment

1. Content of the code file icon. cpp:

#include <windows.h> LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam,   LPARAM lParam){switch(uMsg){case WM_CLOSE:DestroyWindow(hWnd);PostQuitMessage(0);break;default:return DefWindowProc(hWnd,uMsg,wParam,lParam);}return 0;} int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {HWND hWinMain;WNDCLASSEX stWndClass={0};MSG stMsg={0};char szClassName[]="IconTest";stWndClass.hCursor=LoadCursor(0,IDC_ARROW);stWndClass.hInstance = hInstance;stWndClass.cbSize = sizeof(WNDCLASSEX);stWndClass.style = CS_HREDRAW |CS_VREDRAW;stWndClass.lpfnWndProc = WndProc;stWndClass.hbrBackground =(HBRUSH) (COLOR_WINDOW+1);stWndClass.lpszClassName = szClassName;RegisterClassEx(&stWndClass);hWinMain = CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,100,600,400,0,0,hInstance,0);ShowWindow(hWinMain,SW_SHOWNORMAL);UpdateWindow(hWinMain);while(GetMessage(&stMsg,0,0,0)){TranslateMessage(&stMsg);DispatchMessage(&stMsg);}ExitProcess(0);return 0;}

2. Prepare two icon files, big. ICO and small. ICO. The former is 256*256, and the latter is 16*16. You can find these two materials from the attached CD of Luo yunbin.


Ii. Experiment steps

1. Create a blank Win32 program icontest1 in vc6, and add main. cpp to the Project for compilation. In this case, observe the running results of various icons of the program.

(1) The icon at the left of the title bar of the program is the default small icon that comes with VC. 1.

(2) The icon at the left of the program name in the taskbar is also the default small icon that comes with VC. 2.

(3) The left icon of the program name displayed in the resource manager is also the default small icon that comes with VC. 3.

(4) In the program manager displayed by pressing Alt + TAB, the program icon is the default large icon that comes with VC.

(I cannot intercept the chart that appears after pressing Alt + TAB under XP. If you know it, please let me know ^ o ^)

 

2. Create icontest2. add an RC file to the project. The content is

# Define ico_small 0x1001
Ico_small icon "small. ICO"

, The experiment result is

(1) The icon at the left of the title bar of the program is still the default small icon that comes with VC.

(2) The icon at the left of the program name in the taskbar is the added small. ICO file, as shown in

(3) The icon in resource manager is the added small. ICO, as shown in

(4) The program manager icon is the default large icon of VC.

 

3. Add another two sentences to RC.

# Define ico_big 0x1000
Ico_big icon "big. ICO"

Result:

(1) The icon at the left of the title bar of the program is still the default small icon that comes with VC.

(2) The icon in the taskbar is small. ICO.

(3) The resource manager icon is big. ICO, as shown in

(4) The icon in the program manager comes with VC

 

4. Create test3, add main. cpp to the project, and add a sentence to the code.

Stwndclass. hicon = loadicon (hinstance, makeintresource (ico_small ));

RC file content:

Ico_small icon "small. ICO"

At this time, the four icons are small. ICO

 

5. Change the code in 4

Stwndclass. hicon = loadicon (hinstance, makeintresource (ico_big ));

The RC file content remains unchanged, so the results are quite interesting.

(1) The icon at the left of the title bar comes with VC.

(2) The icon in the taskbar is small. ICO.

(3) The icon in resource manager is small. ICO.

(4) The icon in the program manager comes with VC

 

6. Change the code in 4

Stwndclass. hicon = loadicon (hinstance, makeintresource (ico_big ));
Stwndclass. hiconsm = loadicon (hinstance, makeintresource (ico_small ));

The RC file content is

Ico_big icon "big. ICO"
Ico_small icon "small. ICO"

Result:

(1) The icons in the title bar and taskbar are small. ICO.

(2) The resource manager icon is big. ICO.

(3) The icon in the program manager is big. ICO.

 

7. Create test4, the Code is the original code, and the RC file is

Ico_big icon "big. ICO"
Ico_small icon "small. ICO"
In this case, the title bar and the Program Manager icons are provided by VC.

The taskbar and resource manager are big. ICO.

 

8. Add the wndproc code of test4

Case wm_create:
Setclasslong (hwnd, gcl_hicon, (long) hsmall );
Break;

After running, the icons in the title bar, taskbar, and program manager are small. ICO,

The icon in resource manager is big. ICO.

 

Summary: The small icons in API programming refer to the program icons on the title bar and the taskbar. Generally, they are 16*16 pixels ICO files.

The large icon appears in the Resource Manager and Program Manager, generally 256*256 pixels.

Setclasslong changes the related icons when the program is running and does not affect the static icons on the resource manager.

 

Digress:

With the help of xingyu, I finally understood the key points of using the message wm_seticon.

According

The common method for sending wm_seticon messages is

Sendmessage (hwnd, wm_seticon, icon_big, (lparam) (hiconbig ));

Note that the 3rd parameters must be one of the two predefined constants icon_big and icon_small. On my machine, these two constants are

Vs6 installation directory \ vc98 \ include \ winuser. h file.

According to the description of msdn, the two constants mean

If the parameter is icon_big, the icon in the program manager is modified (that is, what appears after pressing Alt + TAB );

If the parameter is icon_small, the icon on the left of the program title bar is modified;

The first parameter is the handle of the icon file.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.