使用资源的一个好处是可以把程序的很多组建都绑定到程序的.exe文件中,而作为一种资源,图标在开发者的机器上被存为一个单独的可编辑文件,然后在程序编译过程中被绑定到.exe文件内
绑定ICON文件,使用下面的语句
wndclass.Icon=LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON));
//其中IDI_ICON是你自己定义的ICON的ID
先看看程序先,然后再介绍如何画ICON,编写程序时,千万别忘了#include"resource.h"
#include<windows.h>
#include<windowsx.h>
#include"resource.h"
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("leidemingzi");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON));
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(
szAppName, // registered class name
TEXT("this is title"), // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static HICON hIcon;
static int cxIcon,cyIcon,cxClient,cyClient;
HDC hdc;
HINSTANCE hInstance;
PAINTSTRUCT ps;
int x,y;
switch(uMsg)
{
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON));
cxIcon=GetSystemMetrics(SM_CXICON);
cyIcon=GetSystemMetrics(SM_CYICON);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(y=0;y<cyClient;y+=cyIcon)
{
for(x=0;x<cxClient;x+=cxIcon)
{
DrawIcon(hdc,x,y,hIcon);
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
下面我用两种方式,来展示一下,如何画ICON图标和添加到exe文件中去。
一在VIsual C++6.0环境中
1新建一个win32项目,编好运行程序.cpp(.c)文件,File-->New-->Files-->Resource Script--->命名--->ok
2然后手动添加到相应的文件中去,如右键Resource file --->Add files to Floder---->刚刚命名的文件名.rc,右键Header file-->Add files to Floder--->.h文件
3光标移动到Resource files,然后点击工具栏的Insert--->Resource...--->Icon
4双击32X32位图标周围的空白区域,然后修改该图标的文件名和ID号,这个ID号要对应程序中的ID喔,这个例子是IDC_ICON
5画图,任意画画,想画啥就画啥:
如
运行程序:
找到项目的位置之后,发现exe文件图标变成了如此这般:
虽然难看了点,但必经是自己亲手画的图标。
二在VS2008环境下画ICON图标,模仿超星浏览器图标
1新建Win32项目,编好运用程序cpp文件
2资源文件添加“资源文件(.rc)”,然后资源文件文件夹会冒出个XX.rc,头文件夹会冒出resource.h
3双击XX.rc,然后资源视图会出现XX.rc文件夹,如:
4在XX.rc文件,右键--->添加资源---Icon
5画完图标后,修改文件名和ID号如:
这样画:
效果

下面来看看超新浏览器的图标是怎样的:
左边是真身,右边是山寨,赶紧自娱自乐去吧;
画得有点粗糙,但还是有点雏形的。