�������� Linux Tags

10. ���ܱ���������Դ

�����Windows��ʽ������һ���Զ���ͼʾ��Windows����ͼʾ��ʾ��Ӧ�ó�ʽ�Ӵ������е����Ͻǡ�����ʽ�����ڡ���ʼ�����ܱ��У�����ʾ��өĻ�ײ��Ĺ������У�������Windows Explorer�У�������Ϊ��ݷ�ʽ��ʾ��������ʱ��WindowsҲ��ʾ�ó�ʽ��ͼʾ����Щ��ʽ���󲿷�����С����һ���ͼ�λ��ƹ��ߣ�Ҳʹ���Զ������α�����ʾ��ʽ�IJ�ͬ��������������Windows��ʽʹ�ù��ܱ��ͶԻ����顣���ܱ����Ի�������Ͼ����У����DZ�׼Windowsʹ���߽�������㡣

ͼʾ���αꡢ���ܱ��ͶԻ����鶼���໥�����ģ�������Windows��ȫ����Դ��̬����Դ�����ϣ����DZ������ڳ�ʽ��.EXE�����У��������Dz���פ���ڳ�ʽ�����������С�Ҳ����˵����Դ���ܴӳ�ʽԭʼ���ж���ı���ֱ�Ӵ�ȡ��Windows�ṩ��ʽֱ�ӻ��ӵذ���������������Ա�ʹ�á������Ѿ����������������ĺ�ʽ����LoadIcon��LoadCursor�����dz����ڷ�����ʽ�������Ӵ����ṹ�������趨�����С����Ǵ�Windows���������λͼʾ���α�ӳ�󣬲����ظ�ͼʾ���α�Ĵ��š��ڱ����У������Ƚ����Լ���ͼʾ������ӳ�ʽ�Լ���.EXE���������롣

�ڱ����У����ǽ�������Щ��Դ��

  • ͼʾ
    ��
  • �α�
    ��
  • �ִ�
    ��
  • �Զ���Դ
    ��
  • ���ܱ�
    ��
  • ���̼��ټ�
    ��
  • �Ի�����
    ��
  • ����ͼ
    ��

ǰ������Դ�ڱ������ۣ��Ի������ڵ�ʮһ�����ۣ�������ͼ�ڵ�ʮ�������ۡ�

ͼʾ���αꡢ�ִ����Զ���Դ
��

ʹ����Դ�ĺô�֮һ����춳�ʽ������Ԫ���ܹ�����������ʽ��.EXE�����С����û����Դ��һ�������ͼʾͼ��֮��Ķ���λ�������ܻ����ڵ����ĵ����У�.EXE����������������ʹ�á�����ͼʾ���ò��ڳ�ʽ����λԪ�����е���ʽ���壨�������޷�����ʵ�ʵ�ͼʾͼ���ˣ�����Ϊ��Դ��ͼʾ�����ڿ����ߵ����Ͽɵ����༭�ĵ����У����ڱ�������б���������.EXE�����С�

��ͼʾ���ӵ���ʽ
��

����Դ���ӵ���ʽ����ҪVisual C++ Developer Studio��һЩ���ӹ��ܡ����ͼʾ��˵������ʹ�á�Image Editor����Ҳ��Ϊ��Graphics Editor����������ͼʾ��ͼ�񡣸�ͼ�񱻴����ڸ�����Ϊ.ICO��ͼʾ�����С�Developer Studio������һ����Դ��������������Ϊ.RC�ĵ�������ʱҲ������Դ���嵵���������г��˳�ʽ��������Դ��һ���ó�ʽ������Դ�ı�ͷ������RESOURCE.H����

��ˣ������Կ�����Щ�µ����������֯��һ��ģ��������Խ�����ΪICONDEMO����ר����ʼ��������һ������Developer Studio�д� File ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ New ï¿½ï¿½È»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ ×¨ï¿½ï¿½ Ò³ï¿½ï¿½ï¿½Ç©ï¿½ï¿½ Win32 Application ï¿½ï¿½ï¿½ï¿½ Project Name ï¿½ï¿½ï¿½Ð¼ï¿½ï¿½ï¿½ ICONDEMO ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ OK ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½Developer Studio���������֧Ԯ��������ר���������������Щ�����������ֵ���ICONDEMO.DSW��ICONDEMO.DSP��ICONDEMO.MAK�����赱���� Tools ï¿½ï¿½ï¿½Ü±ï¿½Ñ¡ï¿½ï¿½ Open ï¿½á£¬ï¿½ï¿½ï¿½ï¿½Ê¾ï¿½ï¿½ Open ï¿½Ô»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð£ï¿½ï¿½ï¿½ Build Ò³ï¿½ï¿½ï¿½Ç©ï¿½ï¿½Ñ¡ï¿½ï¿½ Export makefile when saving project file ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ú£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Í¨ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä½ï¿½ï¿½ï¿½Cԭʼ�뵵������ File ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ New ï¿½ï¿½Ñ¡ï¿½ï¿½ Files Ò³ï¿½ï¿½ï¿½Ç©ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ C++Source File ï¿½ï¿½ï¿½ï¿½ File Name ï¿½ï¿½ï¿½Ð¼ï¿½ï¿½ï¿½ICONDEMO.C������ OK ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½Developer Studio�ͽ�����һ���յ�ICONDEMO.C�����������ʽ10-1�еij�ʽ����ѡ�� Insert ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½È»ï¿½ï¿½Ñ¡ï¿½ï¿½ File As Text Ñ¡ï¿½î£¬ï¿½Ó±ï¿½ï¿½é¸½ï¿½ÏµÄ¹ï¿½ï¿½ï¿½Ð¸ï¿½ï¿½ï¿½Ô­Ê¼ï¿½ë¡£

 ï¿½ï¿½Ê½10-1  ICONDEMO
ICONDEMO.C
/*--------------------------------------------------------------------------
	ICONDEMO.C -- 		Icon Demonstration Program
						(c) Charles Petzold, 1998
--------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     	TCHAR    			szAppName[] 		= TEXT ("IconDemo") ;
     	HWND     			hwnd ;
     	MSG      			msg ;
     	WNDCLASS 		wndclass ;

     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName 		= szAppName ;
     	if (!RegisterClass (&wndclass))
     	{
          		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      							szAppName, MB_ICONERROR) ;
          		return 0 ;
     }
     
   	hwnd = CreateWindow (	szAppName, TEXT ("Icon Demo"),
                   WS_OVERLAPPEDWINDOW,
                   CW_USEDEFAULT, CW_USEDEFAULT,
                   CW_USEDEFAULT, CW_USEDEFAULT,
                   NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
    	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
   	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     	static HICON hIcon ;
     	static int   cxIcon, cyIcon, cxClient, cyClient ;
     	HDC          hdc ;
     	HINSTANCE    hInstance ;
     	PAINTSTRUCT  ps ;
     	int          x, y ;
     
     	switch (message)
     	{
     	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, message, wParam, lParam) ;
}

�������������ó�ʽ����Ϊ�ڳ�ʽ��ͷ���õ�RESOURCE.H�����������ڣ����Ի��������Ȼ����������ֱ�ӽ���RESOURCE.H������������Developer StudioΪ������һ����

������ͨ������Դ���������ӵ�ר������������һ�㡣�ӡ�File�����ܱ���ѡ��New����ѡ��Files��ҳ���ǩ��������Resource Script�����ڡ�File Name�����м��롸ICONDEMO��������OK����ʱ��Developer Studio�Ὠ���������ֵ�����ICONDEMO.RC����Դ����������RESOURCE.H������Cԭʼ�뵵������Դ������������ͬ���Ѷ���ʶ���֣�������ֱ�ӱ༭������������ֻҪ��Developer Studio��ά�����ǾͿ��ԡ��������鿴��Դ��������RESOURCE.H����ϣ����Developer Studio�������ţ������ü��±������ǡ��������������Ķ������а��գ�����Ҫ���׵ظ������ǡ����ס��ֻ�������´���ȷ�IJ�����������±���ר��ʱ��Developer Studio�Żᴢ����Щ�������°汾��

��Դ�����������ֵ�������������Щ��Դ�Ŀ���������ʽ��������������繦�ܱ��ͶԻ����顣��Դ������Ҳ�����Է�������Դ�Ķ���λ���������ã�����ͼʾ���Զ��Ļ����αꡣ

���ڣ��Ѿ�����RESOURCE.H�������������������±���һ��ICONDEMO�����ڻ����һ������ѶϢ��ָ��IDI_ICON��û�����塣���ʶ���ֵ�һ�γ���������������У�

wndclass.hIcon  = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;

�ڱ���ǰ��ij�ʽ�У�������������������������ģ�

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

֮���Ըı�����������Ϊ��ǰ����ΪӦ�ó�ʽʹ�õ��DZ�׼��ͼʾ�����������ǵ�Ŀ����ʹ���Զ�ͼʾ��

��ô�����ǽ���һ��ͼʾ�ɣ���Developer Studio�ġ�File View���Ӵ��У����ῴ������������ICONDEMO.C��ICONDEMO.RC��������CONDEMO.C�ᣬ�Ϳ��Ա༭ԭʼ�롣����ICONDEMO.RC�ᣬ�Ϳ��԰���Դ���ӵ������л�༭�Ѵ��ڵ���Դ��Ҫ����ͼʾ�Ļ�����ӡ� Insert ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Resource ï¿½ï¿½Ñ¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Óµï¿½ï¿½ï¿½Ô´ï¿½ï¿½Ò²ï¿½ï¿½ï¿½ï¿½Í¼Ê¾ï¿½ï¿½È»ï¿½ï¿½ï¿½Ù°ï¿½ï¿½Â¡ï¿½ New ï¿½ï¿½ï¿½ï¿½Å¥ï¿½ï¿½

���ڳ��ֵ���һ���հ׵�32��32ͼ�ص�ͼʾ��������������������ɫ�����ῴ������һ���ͼ���ߺͿ�����ɫ�ĸ��������С�ע����ɫ�������а�����������ɫ�޹ص�ѡ���������ɫѡ����ʱ����Ϊ��өĻ��ɫ��������өĻ��ɫ������һ��ͼ������ɫʱѡ���ˡ�өĻ��ɫ��ʱ����ʵ������͸���ġ�����ͼʾ��ʲô��������ʾ��ͼʾδ��ɫ�IJ��ֻ���ʾ����ɫ���������ǾͿ��Խ����Ǿ��ε�ͼʾ��

˫��Χ��ͼʾ�����򣬻���֡�Icon Properties���Ի����飬�öԻ�����ʹ���ܹ�����ͼʾ��ID�͵������ơ�Developer Studio�����Ѿ���ID�趨ΪIDI_ICON1��������ΪIDI_ICON������ICONDEMO�Ϳ�������ͼʾ������IDI������ͼʾ��ID������ͬ���أ�����������ΪICONDEMO.ICO��

����ѡ��һ������ɫ����ɫ�����ɫ������ͼʾ�ϻ�һ�����B������BIG������ע�ⲻ����ͼ10-1��ô���롣


��

ͼ10-1 ��ʾ��Developer Studio�еı�׼��32��32�� ICONDEMO����

��ʱ��ʽӦ���ܹ����벢ִ�еúܺ��ˡ�Developer Studio����ICONDEMO.RC��Դ�������л�һ�����ߣ���ʾ�����Ǵ���ʶ���֣�IDI_ICON����ͼʾ������ICONDEMO.ICO����RESOURCE.H��ͷ�����л����IDI_ICONʶ���ֵĶ��塣

Developer Studioͨ����Դ������RC.EXE������Դ��������Դ��������ת��Ϊ����λ��ʽ��Ҳ���Ǿ��и�����.RES�ĵ�����Ȼ�ᣬ���ѱ������Դ������ͬ.OBJ��.LIB����һ����LINK�����б�ָ�����ᡣ�������Դ�����ӵ��������������.EXE�����еķ�ʽ��

����ִ��ICONDEMOʱ����ʽͼʾ��ʾ�ڱ����е����ϽǺ͹������С����������ʽ���ӵ�����ʼ�����ܱ��У����������Ϸ��ýݾ�����Ҳ�����Ƕ�������ͼʾ��

ICONDEMOҲ����ʾ����ˮƽ�ʹ�ֱ���ظ���ʾ��ͼʾ����ʽʹ������

hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;

ȡ��ͼʾ�Ĵ��š�ʹ������

cxIcon = GetSystemMetrics (SM_CXICON) ;
cyIcon = GetSystemMetrics (SM_CYICON) ;

ȡ��ͼʾ�Ĵ�С��Ȼ�ᣬ��ʽͨ����κ���

DrawIcon (hdc, x, y, hIcon) ;

��ʾͼʾ������x��y�DZ���ʾͼʾ�����Ͻǵ����ꡣ

��Ŀǰʹ�õĴ������Ѷ��ʾ���ϣ�����SM_CXICON��SM_CYICON������GetSystemMetrics��ر�ͼʾ�Ĵ�СΪ32��32ͼ�ء�����������Developer Studio�н�����ͼʾ��С����Ҳ��ͼʾ�����������Ϻ���ʾ��ICONDEMO��ʽ��ʾ����Ĵ�С��Ȼ���������С������ʾ�ڳ�ʽ�ı����л������е�ͼʾ��С��Сͼʾ�Ĵ�С�����ɴ���SM_CXSMSIZE��SM_CYSMSIZE������GetSystemMetrics��ã���һ��SM��ʾ��system metrics��ϵͳ������������������SM��ʾ��small��С�����������Ŀǰʹ�õĴ������ʾ����˵��Сͼʾ�Ĵ�СΪ16��16ͼ�ء�

���������⡣��Windows��32��32��ͼʾ��СΪ16��16��ͼʾʱ���������ͼ�ص��к��С����������ijЩ�Ƚϸ��ӵ�ͼʾ���ͻ�ʧ�档��ˣ�����Ӧ��Ϊ��Щͼ����С�ͻ���ε�ͼʾ���������16��16ͼ�ص�ͼʾ����Developer Studio��ͼʾͼ��������DZ�ʶΪ��Device��������ʽ�嵥���飬�������ұ���һ����ť�����¸ð�ť�ᵯ����New Icon Image���Ի����飬��ʱѡ��Small��16��16���������������Ի���һ��ͼʾ����ͼ10-2��ʾ����һ����S������ʾ��С������


��

ͼ10-2 ��Developer Studio����ʾ��С��16��16�� ICONDEMO����

�ڸó�ʽ�����������κ����顣�ڶ���ͼʾͼ�񱻴�������ͬ��ICONDEMO.ICO�����У�������ͬ��IDI_ICONʶ�������á����ʵ���ʱ��Windows���Զ�ʹ�øý�С��ͼʾ�������ڱ����л������С�������������ʾ��ݷ�ʽ���Լ���ʽ����DrawIconװ����ʾ����ʱ��Windows��ʹ�ô�ͼʾ��

��������Щ֪ʶ֮�ᣬ�����ǿ�һ��ʹ��ͼʾ����ϸ�����

ȡ��ͼʾ����
��

�������ϸ�Ķ�ICONDEMO.RC��RESOURCE.H�������ῴ����Developer Studio�������ά��������һЩ��ǡ�Ȼ������������Դ������ʱ��ֻ��������������Ҫ�ġ���Щ��ICONDEMO.RC��RESOURCE.H������ժ¼�����Ĺؼ����ֱ����ڳ�ʽ10-2�С�

ICONDEMO.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Icon
IDI_ICON      			ICON    DISCARDABLE     "icondemo.ico"
RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by IconDemo.rc

#define IDI_ICON       101
��ʽ10-2 ICONDEMO.RC��RESOURCE.H������ժ¼

��ʽ10-2����ʾ��ICONDEMO.RC��RESOURCE.H������������ͨ�����ֱ༭�����ֶ������ĺ����ƣ�80�����Windows��ʽд���߾����������ġ�Ψһ��ͬ����AFXRES.H�����Ǹ���ͷ�������������ڽ����ɻ���������MFCר��ʱ��Developer Studioʹ�õij���ʶ���֡��ڱ����У����Dz����õ�AFXRES.H��

ICONDEMO.RC�����

IDI_ICON ICON DISCARDABLE "icondemo.ico"

����Դ��������ICON��������ͼʾ��һ����ֵʶ����IDI_ICON�����101����Developer Studio���ӵ�DISCARDABLE�ؼ���ָ������ҪʱWindows���ԴӼ������ж���ͼʾ���Ի�ö���Ŀռ䡣֮�᲻��Ҫ��ʽ�κ��ض��IJ�����Windows���ܹ���������ͼʾ��DISCARDABLE�������ڶ��ģ�����Ҫָ����ֻ�������ƺ�Ŀ¼·�������ո�ʱ��Developer Studio�Ž��������������š�

����Դ������򽫱������Դ������ICONDEMO.RES�У������������ʽ����Դ���ӵ�ICONDEMO.EXE�����ᣬ����Դ�Ϳ��Ծ���һ����Դ��̬��RT_ICON����һ��ʶ���֣�IDI_ICON��101������ʶ����ʽ����ͨ������LoadIcon��ʽȡ�ô�ͼʾ�Ĵ��ţ�

hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;

��ע��ICONDEMO�������ط����������ʽ��һ���ڶ����Ӵ����ʱ����һ�����Ӵ�ѶϢ������ʽ��ȡ��ͼʾ�Ĵ�����춻��ơ�LoadIcon����HICON��̬��ֵ������ͼʾ�Ĵ��š�

LoadIcon�ĵ�һ����������ָ����Դ�����ĸ�������ִ��ʵ����š�ʹ��hInstance��ʾ�����Գ�ʽ�Լ���.EXE������LoadIcon�ĵڶ�������ʵ���ϱ�����Ϊָ���ִ���ָ�ꡣ���Ὣ�ῴ��������ʹ���ִ�����������ֵʶ���ֱ�ʶ��Դ���޼�MAKEINTRESOURCE��������ת������Դ�ִ�������ָ������ֵ�ָ�꣬������ʾ��

#define MAKEINTRESOURCE(i)  (LPTSTR) ((DWORD) ((WORD) (i)))

LoadIcon֪��������ڶ��������ĸ�����Ϊ0����ô�������Ϊͼʾ����ֵʶ���֡�ͼʾ��ʶ���ֱ���Ϊ16λԪֵ��

����ǰ��ķ�����ʽʹ����Ԥ�ȶ����ͼʾ��

LoadIcon (NULL, IDI_APPLICATION) ;

hInstance�������趨ΪNULL�����Windows֪������Ԥ�ȶ����ͼʾ��IDI_APPLICATIONҲ��WINUSER.H����MAKEINTRESOURCE���壺

#define IDI_APPLICATION MAKEINTRESOURCE(32512)

LoadIcon�ĵڶ�������������һ����Ȥ�����⣺ͼʾ��ʶ�����ܿ���Ϊ�ִ��𣿴��ǿ��ԡ��������£��� Developer Studio ï¿½Ð£ï¿½ï¿½ï¿½ ICONDEMO ×¨ï¿½ï¿½ï¿½Äµï¿½ï¿½ï¿½ï¿½Ð±ï¿½ï¿½Ï£ï¿½Ñ¡ï¿½ï¿½ IDONDEMO.RC ï¿½ï¿½ï¿½ï¿½ï¿½á¿´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Îªï¿½ï¿½IconDemo Resource������״�ṹ��Ȼ������Դ��̬��Icon�����������ǡ�IDI_ICON��������û����Ҽ�����ͼʾʶ���֣����ӹ��ܱ���ѡ�� Properties ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ü¸Ä±ï¿½ID��ʵ���ϣ������԰����Ʒ��������ڽ������Ϊ�ִ����������ַ���ָ����Դ���ƣ����ڱ���������ط�Ҳʹ�ø÷�����

��ϲ��Ϊͼʾ���Լ�һЩ������Դ��ʹ���������ƣ���Ϊ���ƿ����dz�ʽ�����ơ����磬�ٶ�����������ΪMYPROG�������ʹ�á�Icon Properties���Ի����齫ͼʾ��IDָ��Ϊ��MyProg�����������ţ�����Դ����������������������

MYPROG ICON DISCARDABLE myprog.ico

Ȼ������RESOURCE.H�в�û��#define��������ָ��MYPROG����ֵʶ���֡���Դ���������ٶ�MYPROG���ִ�ʶ���֡�

��C��ʽ�У�ʹ��LoadIcon��ʽ��ȡ��ͼʾ���š��������Ѿ����˱�ʾ��ʽ�����ִ���

static TCHAR szAppName [] = TEXT ("MyProg") ;

����ζ����ʽ����ʹ��������

hIcon = LoadIcon (hInstance, szAppName) ;

������ͼʾ����Ⱦ޼�MAKEINTRESOURCE������һЩ��

���������ȷʵ������������������ô�����������ִ���ʶ���ֻ��ִ����ڡ�Icon Properties���Ի������У���ID�����������֡���Դ����������һ�����������ICON������

125 ICON DISCARDABLE myprog.ico

����ʹ�����ַ���֮һ����ͼʾ�������׶��ķ�ʽ�ǣ�

hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (125)) ;

��һ�������Ķ��ķ�ʽ�ǣ�

hIcon = LoadIcon (hInstance, TEXT ("#125")) ;

Windowsʶ���ʼ��Ԫ#��ΪASCII��ʽ����Ԫ��ֵ�Ŀ�ͷ��

�ڳ�ʽ��ʹ��ͼʾ
��

��ȻWindows�Լ��ַ�ʽ��ͼʾ��������ʽ����������Windows��ʽ������WNDCLASS�ṹ��RegisterClass�����Ӵ����ʱָ��һ��ͼʾ���������������ģ��������õúܺã����䵱ͼʾ����������׼�ͽ�С��ͼ���Сʱ��������ˡ�Windows����ʾͼʾͼ��ʱ��������ͼʾ������ѡ������ʵ�ͼ���С��

RegisterClass��һ���Ľ��汾����RegisterClassEx����ʹ����ΪWNDCLASSEX�Ľṹ��WNDCLASSEX���������ӵ���λ��cbSize��hIconSm��cbSize��λָ����WNDCLASSEX�ṹ�Ĵ�С������hIconSm���趨ΪСͼʾ��ͼʾ���š���������WNDCLASSEX�ṹ�У��������趨������ͼʾ������ص�����ͼʾ���ţ�һ����춱�׼ͼʾ��һ�����Сͼʾ��

�����ֱ�Ҫ��û�С��������ǿ����ģ�Windows�Ѿ��ӵ���ͼʾ��������ȡ�˴�С���ʵ�ͼʾͼ��RegisterClassEx�ƺ�û��RegisterClass���������hIconSm��λʹ���˰������ͼ���ͼʾ��������ֻ�е�һ��ͼ���ܱ����á��������DZ�׼��С��ͼʾ��ʹ��ʱ�ű���С��RegisterClassEx�ƺ���Ϊ��ʹ�ö��ͼʾͼ�����Ƶģ�ÿ��ͼ��ֻ����һ��ͼʾ��С����Ϊ���ڿ��Խ����ͼʾ��С������ͬһ��ͼʾ�����У������ҽ���ʹ��WNDCLASS��RegisterClass��

��������ڳ�ʽִ�е�ʱ�򣬶�̬�ظ��ij�ʽ��ͼʾ������ʹ��SetClassLong���ﵽĿ�ġ����磬���������ʶ����IDI_ALTICON��صĵڶ���ͼʾ��������������ʹ�����µ����������л����Ǹ�ͼʾ��

SetClassLong (hwnd, GCL_HICON, 
     LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ALTICON))) ;

������봢���ʽͼʾ�Ĵ��ţ���Ҫʹ��DrawIcon��ʽ�ڱ���ʾ��������ʹ��GetClassLong��ô��š����磺

DrawIcon (hdc, x, y, GetClassLong (hwnd, GCL_HICON)) ;

��Windows�ļ���ijЩ���֣�LoadIcon����Ϊ����ʱ�ġ������Ƽ�ʹ��LoadImage��LoadIcon��/Platform SDK/User Interface Services/Resources/Icons��˵����LoadImage��/Platform SDK/User Interface Services/Resources/Resources��˵��������ȻLoadImage��Ϊ������û��LoadIcon�򵥡�����ע�⵽����ICONDEMO�ж�ͬһ��ͼʾ������LoadIcon���Ρ��ⲻ��������⣬Ҳû��ʹ�ö���ļ����塣LoadIcon��ȡ�ô��ŵ�����Ҫ������ŵ�����������ʽ֮һ��ʵ������һ��DestroyIcon��ʽ��������CreateIcon��CreateIconIndirect��CreateIconFromResource����һ��ʹ�á���Щ��ʽʹ��ʽ�ܹ���̬�ؽ���ͼʾͼ��

ʹ���Զ��α�
��

�ڳ�ʽ��ʹ���Զ��Ļ����α���ʹ���Զ���ͼʾ���ƣ�ֻ�Ǵ������ʽд��������ʹ��Windows�ṩ���αꡣ�Զ��α�һ��Ϊ��ɫ����СΪ32��32ͼ�ء���Developer Studio�н����α��뽨��ͼʾ�ķ�����ͬ���ӡ� Insert ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Resource ï¿½ï¿½ï¿½ï¿½È»ï¿½áµ¥ï¿½ï¿½ï¿½ï¿½ Cursor ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Òªï¿½ï¿½ï¿½Ç¶ï¿½ï¿½ï¿½ï¿½Èµã¡£

�����������������趨�Զ��α꣬����Ϊ��

wndclass.hCursor = LoadCursor (hInstance, MAKEINTRESOURCE (IDC_CURSOR)) ;

����α����������ƶ��壬��Ϊ��

wndclass.hCursor = LoadCursor (hInstance, szCursor) ;

ÿ������λ춸��������������Ӵ���ʱ���ͻ���ʾ��IDC_CURSOR��szCursor���Ӧ�Ļ����αꡣ

���ʹ�������Ӵ�����ô������ϣ���α����������Ӵ��IJ�ͬ���������������ʽΪ��Щ���Ӵ��������Ӵ���𣬾Ϳ�����ÿ���Ӵ�������ʵ����趨hCursor��λ����ÿ���Ӵ����ʹ�ò�ͬ���αꡣ���ʹ����Ԥ�ȶ�������Ӵ�������Ϳ���ʹ�����·����ı��Ӵ�����hCursor��λ��

SetClassLong (hwndChild, GCL_HCURSOR,
	LoadCursor (hInstance, TEXT ("childcursor")) ;

���������ʾ���򻮷�Ϊ��С���߼��������ʹ�����Ӵ����Ϳ���ʹ��SetCursor���ı们���α꣺

SetCursor (hCursor) ;

�ڴ���WM_MOUSEMOVEѶϢ�����ڼ䣬��Ӧ�ú���SetCursor�����򣬵��α��ƶ�ʱ��Windows��ʹ���Ӵ�����ж�����α����ػ��αꡣ�ļ�ָ�������û�иı��α꣬��SetCursor�ٶȽ���ܿ졣

�ִ���Դ
��

���ִ�������Դ�Ĺ���һ��ʼ�������˾��ù��졣��Ϊ������ʹ��ԭʼ���ж���Ϊ������һ���ִ�ʱ����û�������κ����⡣

�ִ���Դ��Ҫ��Ϊ���ó�ʽת������������ʱ��Ϊ���㡣�������������н�������һ�������ܱ��ͶԻ�����Ҳ����Դ��������һ���֡����ʹ���ִ���Դ�����ǽ��ִ�ֱ�ӷ���ԭʼ���У���ô��ʽ��ʹ�õ��������ֽ���ͬһ��������Դ�������С����ת������Դ�������е����֣���ô������ʽ����һ�����԰汾��������һ�о������������ʽ�����ַ�����������֯ԭʼ�밲ȫ�öࣨȻ����������һ��������ʽ�����ڱ����������ʽ�в�ʹ���ִ�����ԭ�����ִ���ʹ��ʽ�뿴������Ϊģ���͸��ӣ���

�������ڡ� Insert ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Resource ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ String Table ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½Ê¾ï¿½ï¿½Ó©Ä»ï¿½Ò±ßµï¿½ï¿½Ð±ï¿½ï¿½Ð¡ï¿½Í¨ï¿½ï¿½Ë«ï¿½ï¿½ï¿½Ö´ï¿½ï¿½Í¿ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ã¿ï¿½ï¿½ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½Ê¶ï¿½ï¿½ï¿½Öºï¿½ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ý¡ï¿½

����Դ�����У��ִ���ʾ��һ�����е������У�������ʾ��

STRINGTABLE DISCARDABLE
BEGIN
	IDS_STRING1, "character string 1"
	IDS_STRING2, "character string 2"
	�����ִ�����
END

������������ڰ汾��Windowsд��ʽ���������ֱ༭�����ֶ���������ִ�������Developer Studio��������µ�Ȼ�����׵ö��ˣ��������������Ҵ���������BEGIN��END������

��Դ�������԰�������ִ���������ÿ��ID����Ψһ��ʾһ���ִ���ÿ���ִ�ռһ�У����4097����Ԫ��\t������Ϊ��λ��Ԫ��\n����Ϊlinefeed��Ԫ�š�DrawText��MessageBox��ʽ�ܹ�ʶ����Щ���Ʒ��š�

���ij�ʽ����ʹ��LoadString���а��ִ����Ƶ���ʽ���϶εĻ������У�

LoadString (hInstance, id, szBuffer, iMaxLength) ;

����id��ID����������Դ��������ÿ���ִ���ǰ�棻szBuffer��ָ������ִ�����Ԫ���е�ָ�ꣻiMaxLength������szBuffer�е������Ԫ������ʽ�����ִ��е���Ԫ����

ÿ���ִ�ǰ���IDһ���Ƕ����ڱ�ͷ�����еľ޼�ʶ���֡�����Windows��ʽд����ʹ������IDS_ ����ʾ�ִ���ID����ʱ���������ƻ�������Ѷ��Ҫ���ִ���ʾʱ���뵽�ִ��С�����������£������Խ�C�ĸ�ʽ����Ԫ�����ִ������������wsprintf����Ϊһ����ʽ���ִ���

������Դ���֣������ִ����е����֣���Unicode��ʽ������.RES������Դ�����Լ����յ�.EXE�����С�LoadStringW��ʽֱ������Unicode���֡�LoadStringA��ʽ������Windows 98����Ч�������Unicode�����������������ת����

����������һ����ʽ����ʹ�������ִ�����ѶϢ��������ʾ����������Ѷ��RESOURCE.H��ͷ����Ϊ��Щ��Ѷ����������ʶ���֣�

#define IDS_FILENOTFOUND 		1
#define IDS_FILETOOBIG 		    2
#define IDS_FILEREADONLY 		3

��Դ���������д��ִ�����

STRINGTABLE
BEGIN
	IDS_FILENOTFOUND,   		    "File %s not found."
	IDS_FILETOOBIG,     			"File %s too large to edit."
	IDS_FILEREADONLY,   			"File %s is read-only."
END

Cԭʼ�뵵��Ҳ���������ͷ��������������һ����ʾѶϢ����ĺ�ʽ���Ҽٶ�szAppName��һ��������ʽ���Ƶ������������

OkMessage (HWND hwnd, int iErrorNumber, TCHAR *szFileName)
{
	TCHAR szFormat [40] ;
	TCHAR szBuffer [60] ;

	LoadString (hInst, iErrorNumber, szFormat, 40) ;
	wsprintf (szBuffer, szFormat, szFilename) ;

	return MessageBox (	hwnd, szBuffer, szAppName,
								MB_OK | MB_ICONEXCLAMATION) ;
}

Ϊ����ʾ������file not found����Ѷ��ѶϢ���飬��ʽ���У�

OkMessage (hwnd, IDS_FILENOTFOUND, szFileName) ;

�Զ�����Դ
��

WindowsҲ�����ˡ��Զ���Դ�������ֳ�Ϊ��ʹ���߶������Դ����ʹ���߾���������ʽд���ߣ��������Ǹ�ʹ������ʽ�������ߣ����Զ���Դ������.EXE�����еĸ������ϸ�Ϊ���㣬��ȡ�ó�ʽ�е�����Ҳ����ˡ����Ͽ���������Ҫ���κθ�ʽ����ʽ��춴�ȡ�Զ���Դ��Windows��ʽ��ʹWindows��������������岢����ָ������ָ�ꡣȻ�����Ϳ��ԶԳ�ʽ���κβ��������ᷢ�ֶ�춴���ʹ�ȡ�����Լ������ϣ���Ҫ�Ȱ����ϴ������ⲿ�����У���ʹ�õ������뺯ʽ��ȡ��Ҫ����öࡣ

���磬����һ����������BINDATA.BIN����������ʽ��Ҫ��ʾ��һЩ���ϡ�������ѡ����������ĸ�ʽ�������MYPROGר������MYPROG.RC��Դ�����������Ϳ�����Developer Studio�дӡ� Insert ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Resource ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ Custom ï¿½ï¿½ï¿½ï¿½Å¥ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ô¶ï¿½ï¿½ï¿½ï¿½ï¿½Ô´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¾ï¿½ï¿½Ô´ï¿½ï¿½ï¿½ï¿½ï¿½Æ£ï¿½ï¿½ï¿½ï¿½ç£¬BINTYPE��Ȼ�ᣬDeveloper Studio��������Դ���ƣ��������������IDR_BINTYPE1������ʾ�����������λ���ϵ��Ӵ�����������������ʲô���û����Ҽ�����IDR_BINTYPE1���ƣ���ѡ�� Properties ï¿½ï¿½È»ï¿½ï¿½Í¿ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ£ï¿½ï¿½ï¿½ï¿½ç£¬BINDATA.BIN��

��Դ�������ͻ�������µ�һ��������

IDR_BINTYPE1 BINTYPE BINDATA.BIN

�������Ǹո����ɵ�BINTYPET��Դ��̬�⣬���������ICONDEMO�е�ICON����һ��������ͼʾ�ᣬ�����Զ���Դ����ʹ�����ֵ����ƣ����������ֵ�ʶ���֡�

�������벢�����ʽ������BINDATA.BIN�����ᱻ����MYPROG.EXE�����С�

�ڳ�ʽ�ij�ʼ�������磬�ڴ���WM_CREATEѶϢʱ���ڼ䣬�����Ի����Դ�Ĵ��ţ�

hResource = 	LoadResource (	hInstance,
			FindResource (	hInstance, TEXT ("BINTYPE"), 
									MAKEINTRESOURCE (IDR_BINTYPE1))) ;

����hResource����ΪHGLOBAL��̬������ָ�����������Ĵ��š���������������ʲô��LoadResource������������Դ��������塣��LoadResource��FindResource��ʽ�����������һ��ʹ�ã���ʵ���Ͼ������LoadIcon��LoadCursor��ʽ����������ʵ�ϣ�LoadIcon��LoadCursor��ʽ���õ���LoadResource��FindResource��ʽ��

������Ҫ��ȡ����ʱ������LockResource��

pData = LockResource (hResource) ;

LockResource����Դ��������壨�����û������Ļ�����Ȼ�����ᴫ��һ��ָ����Դ��ָ�ꡣ����������Դ��ʹ��ʱ�������ԴӼ��������ͷ�����

FreeResource (hResource) ;

�����ij�ʽ��ֹʱ��Ҳ���ͷ���Դ����ʹ��û�к���FreeResource.��

�����ǿ�һ��ʹ��������Դ��һ��ͼʾ��һ���ִ�����һ���Զ�����Դ���ķ�����ʽ����ʽ10-3��ʾ��POEPOEM��ʽ������ʾ������ʾEdgar Allan Poe�ġ�Annabel Lee�����֡��Զ�����Դ�ǵ���POEPOEM.TXT����������һ��ʫ�ģ������ֵ����Է�б�ߣ�\��������

 ï¿½ï¿½Ê½10-3  POEPOEM
POEPOEM.C
/*---------------------------------------------------------------------------
   POEPOEM.C -- Demonstrates Custom Resource
					(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HINSTANCE hInst ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
						PSTR szCmdLine, int iCmdShow)
{
	TCHAR			szAppName [16], szCaption [64], szErrMsg [64] ;
	HWND			hwnd ;
	MSG			msg ;
	WNDCLASS 		wndclass ;
     
	LoadString (	hInstance, IDS_APPNAME, szAppName, 
					sizeof (szAppName) / sizeof (TCHAR)) ;

	LoadString (	hInstance, IDS_CAPTION, szCaption, 
					sizeof (szCaption) / sizeof (TCHAR)) ;
        
	wndclass.style 					= CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   			= WndProc ;
	wndclass.cbClsExtra    			= 0 ;
	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     		= hInstance ;
     	wndclass.hIcon         		= LoadIcon (hInstance, szAppName) ;
     	wndclass.hCursor       		= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          	LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName, 
                                  					sizeof (szAppName)) ;
          	LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg, 
                                  					sizeof (szErrMsg)) ;
          	MessageBoxA (NULL, (char *) szErrMsg, 
                             					(char *) szAppName, MB_ICONERROR) ;
          	return 0 ;
     	}
     
     	hwnd = CreateWindow (	szAppName, szCaption,
                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                     CW_USEDEFAULT, CW_USEDEFAULT,
                     CW_USEDEFAULT, CW_USEDEFAULT,
                     NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     {
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     	static char  			* pText ;
     	static HGLOBAL 		hResource ;
     	static HWND    			hScroll ;
     	static int     				iPosition, cxChar, cyChar, cyClient, iNumLines, xScroll ;
     	HDC            				hdc ;
     	PAINTSTRUCT    				ps ;
     	RECT           				rect ;
     	TEXTMETRIC     				tm ;

     	switch (message)
     	{
     	case WM_CREATE :
          		hdc 			= GetDC (hwnd) ;
          		GetTextMetrics (hdc, &tm) ;
          		cxChar 		= tm.tmAveCharWidth ;
          		cyChar 		= tm.tmHeight + tm.tmExternalLeading ;
          		ReleaseDC (hwnd, hdc) ;
          
          		xScroll 		= GetSystemMetrics (SM_CXVSCROLL) ;
          		hScroll 		= CreateWindow (TEXT ("scrollbar"), NULL,
                                  WS_CHILD | WS_VISIBLE | SBS_VERT,
                                  0, 0, 0, 0,
                hwnd, (HMENU) 1, hInst, NULL) ;
          
          		hResource = 	LoadResource (hInst, 
                FindResource (hInst, 	TEXT ("AnnabelLee"),
                                        TEXT ("TEXT"))) ;
          
          		pText = (char *) LockResource (hResource) ;
          		iNumLines = 0 ;
          
          		while (*pText != '\\' && *pText != '\0')
        		{
               		if (*pText == '\n')
                    				iNumLines ++ ;
               		pText = AnsiNext (pText) ;
          		}
          		*pText = '\0' ;
          
          		SetScrollRange (hScroll, SB_CTL, 0, iNumLines, FALSE) ;
          		SetScrollPos   (hScroll, SB_CTL, 0, FALSE) ;
          		return 0 ;
          
     	case 	WM_SIZE :
          		MoveWindow (hScroll, LOWORD (lParam) - xScroll, 0,
                xScroll, cyClient = HIWORD (lParam), TRUE) ;
          		SetFocus (hwnd) ;
          		return 0 ;
          
     	case 	WM_SETFOCUS :
          		SetFocus (hScroll) ;
          		return 0 ;
          
     	case 	WM_VSCROLL :
          		switch (wParam)
          		{
          		case 	SB_TOP :
               				iPosition = 0 ;
               				break ;
          		case 	SB_BOTTOM :
               				iPosition = iNumLines ;
               				break ;
          		case 	SB_LINEUP :
               				iPosition -= 1 ;
               				break ;
          		case 	SB_LINEDOWN :
               				iPosition += 1 ;
               				break ;
          		case 	SB_PAGEUP :
               				iPosition -= cyClient / cyChar ;
               				break ;
          		case 	SB_PAGEDOWN :
               				iPosition += cyClient / cyChar ;
               				break ;
          		case 	SB_THUMBPOSITION :
               				iPosition = LOWORD (lParam) ;
               				break ;
          		}
          		iPosition = max (0, min (iPosition, iNumLines)) ;
          
          		if (iPosition != GetScrollPos (hScroll, SB_CTL))
          		{
               				SetScrollPos (hScroll, SB_CTL, iPosition, TRUE) ;
               				InvalidateRect (hwnd, NULL, TRUE) ;
         		}
          		return 0 ;
          
     	case 	WM_PAINT :
          		hdc = BeginPaint (hwnd, &ps) ;
               
          		pText = (char *) LockResource (hResource) ;
               
          		GetClientRect (hwnd, &rect) ;
          		rect.left += cxChar ;
          		rect.top  += cyChar * (1 - iPosition) ;
          		DrawTextA (hdc, pText, -1, &rect, DT_EXTERNALLEADING) ;

          		EndPaint (hwnd, &ps) ;
          		return 0 ;
               
     	case 	WM_DESTROY :
          		FreeResource (hResource) ;
          		PostQuitMessage (0) ;
          		return 0 ;
   	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 POEPOEM.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// TEXT
ANNABELLEE              			TEXT    DISCARDABLE     "poepoem.txt"

/////////////////////////////////////////////////////////////////////////////
// Icon
POEPOEM                 			ICON    DISCARDABLE     "poepoem.ico"

/////////////////////////////////////////////////////////////////////////////
// String Table
STRINGTABLE DISCARDABLE 
BEGIN
    	IDS_APPNAME         "PoePoem"
    	IDS_CAPTION         """Annabel Lee"" by Edgar Allan Poe"
    IDS_ERRMSG              "This program requires Windows NT!"
END
 RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by PoePoem.rc

#define IDS_APPNAME   1
#define IDS_CAPTION   2
#define IDS_ERRMSG    3
 POEPOEM.TXT
It was many and many a year ago,
   In a kingdom by the sea,
That a maiden there lived whom you may know
By the name of Annabel Lee;
And this maiden she lived with no other thought
   Than to love and be loved by me.
I was a child and she was a child
   In this kingdom by the sea,
But we loved with a love that was more than love --
   I and my Annabel Lee --
With a love that the winged seraphs of Heaven
   Coveted her and me.
And this was the reason that, long ago,
   In this kingdom by the sea,
A wind blew out of a cloud, chilling
   My beautiful Annabel Lee;
So that her highborn kinsmen came
   And bore her away from me,
To shut her up in a sepulchre
   In this kingdom by the sea.
The angels, not half so happy in Heaven,
   Went envying her and me --
Yes! that was the reason (as all men know,
   In this kingdom by the sea)
That the wind came out of the cloud by night,
   Chilling and killing my Annabel Lee.
But our love it was stronger by far than the love Of those who were older than we -- Of many far wiser than we --
And neither the angels in Heaven above
   Nor the demons down under the sea
Can ever dissever my soul from the soul
   Of the beautiful Annabel Lee:
For the moon never beams, without bringing me dreams
   Of the beautiful Annabel Lee;
And the stars never rise, but I feel the bright eyes
   Of the beautiful Annabel Lee:
And so, all the night-tide, I lie down by the side
Of my darling -- my darling -- my life and my bride,
   In her sepulchre there by the sea --
   In her tomb by the sounding sea.
 
   [May, 1849]
\
POEPOEM.ICO


��

��POEPOEM.RC��Դ�������У�ʹ���߶������Դ������ΪTEXT��̬��ȡ��ΪAnnabelLee��

ANNABELLEE  TEXT  POEPOEM.TXT

��WndProc����WM_CREATEʱ��ʹ��FindResource��LoadResourceȡ����Դ���š�ʹ��LockResource������Դ������ʹ��һ��С��ʽ������ĩβ�ķ�б�ߣ�\������0�������������WM_PAINTѶϢ�����ڼ�ʹ�õ�DrawText��ʽ��

ע�⣬����ʹ�õ������Ӵ��ľ����У��������Ӵ������У�������Ϊ���Ӵ���������һ���Զ��ļ��̽��棬�����POEPOEM��û�д���WM_KEYDOWN��

POEPOEM��ʹ�������ִ������ǵ�ID��RESOURCE.H��ͷ�����ж��塣�ڳ�ʽ�Ŀ�ʼ��IDS_APPNAME��IDS_CAPTIONPOEPOEM�ִ���LoadString��������壺

LoadString (hInstance, IDS_APPNAME, szAppName, 	sizeof (szAppName) / 
						sizeof (TCHAR)) ;

LoadString (hInstance, IDS_CAPTION, szCaption, 	sizeof (szCaption) /
						sizeof (TCHAR)) ;

ע��RegisterClassǰ����������С��������Windows 98��ִ��Unicode�汾��POEPOEM�����������оͶ���ʧ�ܡ���ˣ�LoadStringA��LoadStringWҪ���ӵöࣨLoadStringA���뽫��Դ�ִ���Unicodeת��ΪANSI����LoadStringW����ֱ������������LoadStringW��Windows 98�²���֧Ԯ������ζ����Windows 98�£���RegisterClassW��ʽʧ��ʱ��MessageBoxW��ʽ��Windows 98֧Ԯ���Ͳ���ʹ��LoadStringW�����ʽ���ִ���������ԭ�򣬳�ʽʹ��LoadStringA����IDS_APPNAME��IDS_ERRMSG�ִ�����ʹ��MessageBoxA��ʾ�Զ���ѶϢ���飺

if (!RegisterClass (&wndclass))
{
     	LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName, 
                     sizeof (szAppName)) ;
     	LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg, 
                     sizeof (szErrMsg)) ;
     	MessageBoxA (NULL, (char *) szErrMsg, 
                     (char *) szAppName, MB_ICONERROR) ;
     	return 0 ;
}

ע�⣬TCHAR�ִ�������ָ��char��ָ�ꡣ

��Ȼ�����Ѿ����������POEPOEM�������ִ���Դ����ô�����߽���ʽת��������汾�ͺ������ˡ���Ȼ�����ǽ����ò����롸Annabel Lee��������֣����룬�����һ�����ѵö�Ĺ�����

���ܱ�
��

�����ǵ�Monty Python�й����ҵ����Ĭ�̾����ǹ��������������ģ�һ�������߽����ҵ�����ij�����ҡ���Ȼ������û���������ҡ������������û����һ�����ң�Ȼ��������һ�֣�������һ�֣����ϵ��ʵ����û����һ�����ң������ܹ�����40�ֵ����ң����ش���Ȼ��û�У�û�У�û�У�û�У�û�С�

������ҵ��¼�����ͨ�����ܱ���ʹ�������⡣һ�����ܱ���һ�п��õ�ѡ������߼������ò��ߣ����������ṩ��Щ���񣬲��ң����Windows��ʽ��˵��������ʹ����һ��Ӧ�ó�ʽ�ܹ�ִ����Щ������

���ܱ�������Windows��ʽ�ṩ��һ��ʹ���߽���������Ҫ�IJ��֣��������ij�ʽ�����ӹ��ܱ�����Windows��ʽ�������Լ򵥵IJ��֡�����Developer Studio�ж��幦�ܱ���ÿ����ѡ�Ĺ��ܱ������Ψһ��ID�������Ӵ����ṹ��ָ�����ܱ����ơ���ʹ����ѡ��һ�����ܱ���ʱ��Windows�����ij�ʽ���Ͱ�����ID��WM_COMMANDѶϢ��

�����깦�ܱ��ᣬ�һ������ۼ��̼��ټ���������һЩ������ϣ���Ҫ����������ܱ����ܡ�

���ܱ�����
��

�Ӵ��Ĺ��ܱ��н����ڱ����е��·���ʾ��������ܱ�����ʱ����Ϊ�������ܱ����򡸶��㹦�ܱ��������ڶ��㹦�ܱ�����Ŀͨ��������ʽ���ܱ���Ҳ������ͻ��ʽ���ܱ������ӹ��ܱ�������Ҳ���Զ������Ƕ�׵�ͻ��ʽ���ܱ���Ҳ����˵����ͻ��ʽ���ܱ��ϵ���Ŀ���Դ�ȡ��һ��ͻ��ʽ���ܱ�����ʱͻ��ʽ���ܱ��ϵ���Ŀ���жԻ������Ի�ø������Ѷ���Ի���������һ�½��ܣ����ڱ����е�����ˣ��ุܶ�Ӵ�����ʾ��ʽ��Сͼʾ�����ͼʾ��������ϵͳ���ܱ�����ʵ��������һ��ͻ��ʽ���ܱ���

ͻ��ʽ���ܱ��ĸ�������ǡ���ѡ�еġ�������ζ��Windows�ڹ��ܱ����ֵ������ʾһ��С��ѡ�б�ǣ�ѡ�б����ʹ����֪���ӹ��ܱ���ѡ������Щѡ���Щѡ��֮������ǻ���ģ�Ҳ���Բ����⡣���㹦�ܱ���ܱ�ѡ�С�

���㹦�ܱ���ͻ��ʽ���ܱ�����Ա������á��������á�����Ч���������������͡�����������ʱ�򱻵��������á��͡����á���ͬ��ʡ������û���õĹ��ܱ�����ʹ���߿�����һ���ģ�������Ч���Ĺ��ܱ�����ʹ�û�ɫ��������ʾ�ġ�

��ʹ���ߵĽǶ����������á����ú���Ч���Ĺ��ܱ���ǿ��ԡ�ѡ��ġ�����ѡ��Ĺ��ܱ���Ŀ�ᱻ�Ӹ�������ʾ����Ҳ����˵��ʹ���߿���ʹ�û���ѡ�񱻽��õĹ��ܱ����������ʾ�α����ƶ������õĹ��ܱ����ϣ�����ʹ�ù��ܱ���Ĺؼ���ĸ��ѡ��ù��ܱ��Ȼ�����ӳ�ʽд���ߵĽǶ����������á����ú���Ч�����ܱ���Ĺ����Dz�ͬ�ġ�WindowsֻΪ���õĹ��ܱ������ʽ����WM_COMMANDѶϢ��Ҫ��ѡ������Ч�����԰���Щ���ܱ�����ú���Ч�������������ʹ����֪��ѡ������Ч�ģ���ô��������һ�����ܱ�����Ч����

���ܱ��ṹ
��

����������ı��ʽ�еĹ��ܱ�ʱ���Ѷ��㹦�ܱ���ÿһ��ͻ��ʽ���ܱ�����ɸ��Զ����Ĺ��ܱ������õġ����㹦�ܱ���һ�����ܱ����ţ��ڶ��㹦�ܱ��е�ÿһ��ͻ��ʽ���ܱ�Ҳ�����Լ��Ĺ��ܱ����š�ϵͳ���ܱ���Ҳ��һ��ͻ��ʽ���ܱ���Ҳ�й��ܱ����š�

���ܱ��е�ÿһ����������ԡ���һ�������ǹ��ܱ�����ʾʲô�����������ִ������ͼ���ڶ���������WM_COMMANDѶϢ��Windows���͸���ʽ�Ĺ��ܱ�ID����������ʹ����ѡ���ܱ���ʱWindows��ʾ��ͻ��ʽ���ܱ��Ĵ��š������������ǹ��ܱ�������ԣ������Ƿ񱻽��á���Ч����ѡ�С�

���幦�ܱ�
��

Ҫʹ��Developer Studio������ʽ��Դ���������ӹ��ܱ������Դ� Insert ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Resource ï¿½ï¿½Ñ¡ï¿½ï¿½ Menu ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¾ï¿½Öªï¿½ï¿½ï¿½Ë£ï¿½ï¿½ï¿½È»ï¿½á£¬ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ã½ï¿½Ì¸Ê½ï¿½Ä·ï¿½Ê½ï¿½ï¿½ï¿½å¹¦ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ã¿Ò»ï¿½î¶¼ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½Øµï¿½ Menu Item Properties ï¿½Ô»ï¿½ï¿½ï¿½ï¿½é£¬Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä¿ï¿½ï¿½ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ï¿½ï¿½ Pop-up ï¿½ï¿½È¡ï¿½ï¿½ï¿½é£¬ï¿½ï¿½ï¿½ï¿½Ä¿ï¿½Í»ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ã»ï¿½ï¿½ID�����Ŀ����ϵ�����û��ѡ�� Pop-up ï¿½ï¿½È¡ï¿½ï¿½ï¿½é£¬ï¿½ï¿½ï¿½ï¿½Ä¿ï¿½ï¿½Ñ¡ï¿½ï¿½Ê±ï¿½Í»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ø¶ï¿½ID��WM_COMMANDѶϢ�������๦�ܱ���ֱ��������Դ��������POPUP��MENUITEM������

����Ϊ���ܱ��е���Ŀ��������ʱ�����Լ���һ����&�����ţ�ָ������һ����Ԫ��Windows��ʾ���ܱ�ʱҪ�ӵ��ߡ����ֵ�����Ԫ������ʹ��Alt��ѡ���ܱ���ʱWindowsҪѰ�ҵıȶ���Ԫ������������в�������&�����ţ��Ͳ���ʾ�κε��ߣ�Windows�Ὣ���ܱ������ֵĵ�һ����ĸ���Alt�����ҡ�

����� Menu Items Properties ï¿½Ô»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Grayed Ñ¡ï¿½î£¬ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½Ç²ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç»ï¿½É«ï¿½Ä£ï¿½ï¿½ï¿½ï¿½î²»ï¿½ï¿½ï¿½ï¿½WM_COMMANDѶϢ�����ѡ�� Inactive Ñ¡ï¿½î£¬ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ò²ï¿½Ç²ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä£ï¿½Ò²ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½WM_COMMANDѶϢ����������������ʾ������ Checked Ñ¡ï¿½ï¿½ï¿½Ú¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½Ï·ï¿½ï¿½ï¿½Ò»ï¿½ï¿½Ñ¡ï¿½Ð±ï¿½Ç¡ï¿½ Separator Ñ¡ï¿½ï¿½ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ï²ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Äºï¿½ï¿½ß¡ï¿½

��ͻ��ʽ���ܱ�����Ŀ�ϣ��������ִ���ʹ����λ��Ԫ\t��������\t�����ֱ������ھ���ͻ��ʽ���ܱ��ĵ�һ���ұ��µ�һ���ϡ��ڱ������棬�ῴ����ʹ�ü��̼��ټ�ʱ��������á��ִ��е�\aʹ���������������Ҷ��롣

��ָ����IDֵ��Windows���͸��Ӵ�ѶϢ������ʽ�й��ܱ�ѶϢ�е���ֵ���ڹ��ܱ���IDֵӦ����Ψһ�ġ����չ�������ʹ����IDM����ID for a Menu������ͷ��ʶ���֡�

�ڳ�ʽ�����ù��ܱ�
��

�����WindowsӦ�ó�ʽ����Դ��������ֻ��һ�����ܱ��������Ը����ܱ���һ�����ʽ������ͬ�����ֵ����ơ���ʽд���߾�������ʽ����춹��ܱ����ƣ��Ա���ͬ���ִ���������Ӵ���𡢳�ʽ��ͼʾ���ƺ͹��ܱ����ơ�Ȼ�ᣬ��ʽ���Ӵ��Ķ�����Ϊ���ܱ����ø����ƣ�

wndclass.lpszMenuName = szAppName ;

��Ȼ��ȡ���ܱ���Դ����÷��������Ӵ������ָ�����ܱ�����Ҳ����ʹ������������WindowsӦ�ó�ʽ����ʹ��LoadMenu��ʽ�����ܱ���Դ����������У���ͬLoadIcon��LoadCursor��ʽһ����LoadMenu����һ�����ܱ����š����������Դ��������Ϊ���ܱ�ʹ�������ƣ��������£�

hMenu = LoadMenu (hInstance, TEXT ("MyMenu")) ;

���ʹ������ֵ����ôLoadMenu���в������µ���ʽ��

hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (ID_MENU)) ;

Ȼ�ᣬ�����Խ�������ܱ�������ΪCreateWindow�ĵھŸ�������

hwnd = CreateWindow (	TEXT ("MyClass"), TEXT ("Window Caption"),
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    NULL, hMenu, hInstance, NULL) ;

����������£�CreateWindow������ָ���Ĺ��ܱ����Ը����Ӵ������ָ�����κι��ܱ������CreateWindow�ĵھŸ�������NULL����ô�����԰��Ӵ�����еĹ��ܱ������������Ӵ������Ӵ��ڶ�ʹ�õĹ��ܱ���������������Ϊ����ͬһ�Ӵ�������ļ����Ӵ�ʹ�ò�ͬ�Ĺ��ܱ���

��Ҳ�������Ӵ������ָ��NULL���ܱ���������CreateWindow������Ҳָ��NULL���ܱ���Ȼ�����Ӵ����������ٸ��Ӵ�ָ��һ�����ܱ���

SetMenu (hwnd, hMenu) ;

������ʽʹ�����Զ�̬���޸��Ӵ��Ĺ��ܱ����ڱ��������NOPOPUPS��ʽ�����ǽ��ῴ���ⷽ������ӡ�

���Ӵ������ʱ�����Ӵ���ص����й��ܱ���������������Ӵ�����صĹ��ܱ��ڳ�ʽ����ǰͨ������DestroyMenu���������

���ܱ���ѶϢ
��

��ʹ����ѡ��һ�����ܱ���ʱ��Windowsͨ�����Ӵ�ѶϢ������ʽ���ͼ�����ͬ��ѶϢ���ڴ��������£� ���ij�ʽ���Ժ��Դ󲿷�ѶϢ��ֻ������Ǵ��ݸ�DefWindowProc���ɡ�WM_INITMENU������һ���ѶϢ�����������в�����

wParam�� �����ܱ�����

lParam�� 0

wParamֵ�����������ܱ����ţ���ʹʹ����ѡ�����ϵͳ���ܱ��е���Ŀ��Windows��ʽͨ������WM_INITMENUѶϢ��������ѡ�и���֮ǰ��ѶϢ�Ѿ�����ʽ�ṩ���޸Ĺ��ܱ��Ļ��ᣬ�������Ǿ��ô˿̸ı䶥�㹦�ܱ��ǻ�����ʹ���ߵġ�

��ʽҲ����յ�WM_MENUSELECTѶϢ������ʹ�����ڹ��ܱ������ƶ��α���߻��󣬳�ʽ���յ�����WM_MENUSELECTѶϢ�����ʵ����Щ�����Թ��ܱ��������������״̬���Ǻ��а����ġ�WM_MENUSELECT�IJ���������ʾ��

LOWORD (wParam)����ѡ����Ŀ�����ܱ�ID����ͻ��ʽ���ܱ�����

HIWORD (wParam)��ѡ�����

lParam: ������ѡ����Ŀ�Ĺ��ܱ�����

WM_MENUSELECT��һ�����ܱ�׷��ѶϢ��wParam��ֵ������Ŀǰѡ����ǹ��ܱ��е���һ��Ӹ�������ʾ���Ǹ�����wParam�ĸ������еġ�ѡ����꡹������������Щ������ϣ�MF_GRAYED��MF_DISABLED��MF_CHECKED��MF_BITMAP��MF_POPUP��MF_HELP��MF_SYSMENU��MF_MOUSESELECT���������Ҫ���ݶԹ��ܱ����ѡ�����ı��Ӵ���ʾ��������ݣ���ô������ʹ��WM_MENUSELECTѶϢ�������ʽ�Ѹ�ѶϢ���͸�DefWindowProc��

��Windows׼����ʾһ��ͻ��ʽ���ܱ�ʱ�������Ӵ�ѶϢ������ʽ����һ��WM_INITMENUPOPUPѶϢ���������£�

wParam�� ͻ��ʽ���ܱ�����

LOWORD (lParam)��ͻ��ʽ���ܱ�����

HIWORD (lParam)�� ϵͳ���ܱ�Ϊ1������Ϊ0

�������Ҫ����ʾͻ��ʽ���ܱ�֮ǰ���û��߽��ù��ܱ����ô���ѶϢ�ͺ���Ҫ�����磬�ٶ���ʽʹ��ͻ��ʽ���ܱ��ϵ� Paste ï¿½ï¿½ï¿½ï¿½ï¿½ ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ö£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Õµï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ðµï¿½WM_INITMENUPOPUPѶϢʱ��Ӧȷ�����������Ƿ������ִ��ڡ����û�У���ôӦ��ʹ Paste ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½Ð§ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç½ï¿½ï¿½Ú±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Þ¸Äµï¿½POPPAD��ʽ�п������������ӡ�

����Ҫ�Ĺ��ܱ�ѶϢ��WM_COMMAND������ʾʹ�����Ѿ��ӹ��ܱ���ѡ����һ�������õĹ��ܱ���ڰ����е�WM_COMMANDѶϢҲ���������Ӵ���������������������Ϊ���ܱ������Ӵ�������ʹ��ͬһID�룬��ô������ͨ��lParam��ֵ���������ǣ����ܱ����lParam��ֵΪ0����μ���10-1��

��10-1
  ���ܱ� ������
LOWORD (wParam): ���ܱ�ID ������ID
HIWORD (wParam): 0 ֪ͨ��
lParam: 0 ���Ӵ�����

WM_SYSCOMMANDѶϢ�����WM_COMMANDѶϢ��ֻ��WM_SYSCOMMAND��ʾʹ���ߴ�ϵͳ���ܱ���ѡ��һ�����õĹ��ܱ��

wParam: ���ܱ�ID

lParam: 0

Ȼ�������WM_SYSCOMMANDѶϢ���ɰ����󰴼������ģ�LOWORD��lParam����HIWORD��lParam�������������α�λ�õ�x��yөĻ���ꡣ

���WM_SYSCOMMAND�����ܱ�IDָʾϵͳ���ܱ��е���һ�ѡ�С����Ԥ�ȶ����ϵͳ���ܱ���ϵ͵����ĸ�λԪӦ�ú�0xFFF0����AND���������ֵ������ֵӦ��Ϊ����֮һ��SC_SIZE��SC_MOVE��SC_MINIMIZE��SC_MAXIMIZE��SC_NEXTWINDOW��SC_PREVWINDOW��SC_CLOSE��SC_VSCROLL��SC_HSCROLL��SC_ARRANGE��SC_RESTORE��SC_TASKLIST�����⣬wParam������SC_MOUSEMENU��SC_KEYMENU��

�������ϵͳ���ܱ������ӹ��ܱ����ôwParam�ĵ����齫��������Ĺ��ܱ�ID��Ϊ�˱�����Ԥ�ȶ���Ĺ��ܱ�ID���ͻ��Ӧ�ó�ʽӦ��ʹ��С�0xF000��ֵ�����춽�һ���WM_SYSCOMMANDѶϢ���͸�DefWindowProc�Ǻ���Ҫ�ġ������������������ô��ʵ���Ͼ��ǽ�����������ϵͳ���ܱ����

���ǽ����۵�����һ��ѶϢ��WM_MENUCHAR��ʵ���ϣ����������ǹ��ܱ�ѶϢ���������������֮һ����ʱ��Windows������ѶϢ���͵��Ӵ�ѶϢ������ʽ�����ʹ���߰���Alt��һ���빦�ܱ��ƥ�����Ԫʱ����������ʾͻ��ʽ���ܱ���ʹ���߰���һ����ͻ��ʽ���ܱ������Ŀ��ƥ�����Ԫ��ʱ����WM_MENUCHARѶϢһ���͵IJ���������ʾ��

LOWORD (wParam): ��Ԫ���루ASCII��Unicode��

HIWORD (wParam): ѡ����

lParam: ���ܱ�����

ѡ�����ǣ�

  • 0 ����ʾͻ��ʽ���ܱ�
    ��
  • MF_POPUP ��ʾͻ��ʽ���ܱ�
    ��
  • MF_SYSMENU ��ʾϵͳͻ��ʽ���ܱ�
    ��

Windows��ʽͨ���Ѹ�ѶϢ���ݸ�DefWindowProc����һ���Windows����0�����ʹWindows�����������ڵ�ʮ����GRAFMENU��ʽ�лῴ��WM_MENUCHARѶϢ��ʹ�á�

������ʽ
��

����������һ���򵥵����ӡ���ʽ10-4��ʾ��MENUDEMO��ʽ���������ܱ��������ѡ���File��Edit��Background��Timer��Help��ÿһ���һ��ͻ��ʽ���ܱ�������MENUDEMOֻ�������򵥡���ͨ�õĹ��ܱ�������������������WM_COMMANDѶϢ�ͼ��wParam�ĵ����顣

 ï¿½ï¿½Ê½10-4  MENUDEMO
MENUDEMO.C
/*---------------------------------------------------------------------
   MENUDEMO.C -- 	Menu Demonstration
                 						(c) Charles Petzold, 1998
-----------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

#define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     HWND     hwnd ;
     	MSG      			msg ;
     	WNDCLASS 		wndclass ;
     
     	wndclass.style 					= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName 		= szAppName ;
     	wndclass.lpszClassName 		= szAppName ;
	if (!RegisterClass (&wndclass))
     	{
          	MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      					szAppName, MB_ICONERROR) ;
          	return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("Menu Demonstration"),
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static int idColor [5] = { 	WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
                                	DKGRAY_BRUSH, BLACK_BRUSH } ;
     	static int iSelection = IDM_BKGND_WHITE ;
     	HMENU				hMenu ;
     
     	switch (message)
     	{
     	case 	WM_COMMAND:
          		hMenu = GetMenu (hwnd) ;
          
          		switch (LOWORD (wParam))
          			{
          		case 	IDM_FILE_NEW:
          		case 	IDM_FILE_OPEN:
          		case 	IDM_FILE_SAVE:
          		case 	IDM_FILE_SAVE_AS:
               				MessageBeep (0) ;
               				return 0 ;

          		case 	IDM_APP_EXIT:
               				SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               				return 0 ;
               
          		case IDM_EDIT_UNDO:
          		case IDM_EDIT_CUT:
          		case IDM_EDIT_COPY:
          		case IDM_EDIT_PASTE:
          		case IDM_EDIT_CLEAR:
               				MessageBeep (0) ;
               				return 0 ;
               
          		case 	IDM_BKGND_WHITE:         	// Note: Logic below
          		case 	IDM_BKGND_LTGRAY:        	// assumes that IDM_WHITE
          		case 	IDM_BKGND_GRAY:          	// through IDM_BLACK are
          		case 	IDM_BKGND_DKGRAY:        	// consecutive numbers in
          		case 	IDM_BKGND_BLACK:         	// the order shown here.
               
               		CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
               		iSelection = LOWORD (wParam) ;
               		CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
               
               		SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
                    							GetStockObject 
                    (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
               
               		InvalidateRect (hwnd, NULL, TRUE) ;
               		return 0 ;
               
          		case 	IDM_TIMER_START:
               				if (SetTimer (hwnd, ID_TIMER, 1000, NULL))
               				{
                    	EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
                    	EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;
               				}
               			return 0 ;
               
          		case	IDM_TIMER_STOP:
               				KillTimer (hwnd, ID_TIMER) ;
               				EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
               				EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;
               				return 0 ;
          		case 	IDM_APP_HELP:
               				MessageBox (hwnd, TEXT ("Help not yet 
implemented!"),
                            szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               				return 0 ;
               
          		case 	IDM_APP_ABOUT:
               				MessageBox (hwnd,TEXT ("Menu Demonstration 
Program\n")
                           TEXT ("(c) Charles Petzold, 1998"),
                        szAppName, MB_ICONINFORMATION | MB_OK) ;
               				return 0 ;
          		}
          		break ;
          
          		case 	WM_TIMER:
               				MessageBeep (0) ;
               				return 0 ;
               
          		case 	WM_DESTROY:
               				PostQuitMessage (0) ;
               				return 0 ;
	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 MENUDEMO.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
MENUDEMO MENU DISCARDABLE 
BEGIN
    	POPUP "&File"
    	BEGIN
        		MENUITEM "&New",  				IDM_FILE_NEW
        		MENUITEM "&Open", 				IDM_FILE_OPEN
        		MENUITEM "&Save",     				IDM_FILE_SAVE
        		MENUITEM "Save &As...",  		IDM_FILE_SAVE_AS
        		MENUITEM SEPARATOR
        		MENUITEM "E&xit", 				IDM_APP_EXIT
    	END
    	POPUP "&Edit"
    	BEGIN
        		MENUITEM "&Undo",  				IDM_EDIT_UNDO
        		MENUITEM SEPARATOR					
        		MENUITEM "C&ut",					IDM_EDIT_CUT
        		MENUITEM "&Copy",  				IDM_EDIT_COPY
        		MENUITEM "&Paste",   				IDM_EDIT_PASTE
        		MENUITEM "De&lete",   			IDM_EDIT_CLEAR
    	END
    	POPUP "&Background"
    	BEGIN
        		MENUITEM "&White",      IDM_BKGND_WHITE, CHECKED
        		MENUITEM "&Light Gray", IDM_BKGND_LTGRAY
        		MENUITEM "&Gray",   	  IDM_BKGND_GRAY
        		MENUITEM "&Dark Gray",  IDM_BKGND_DKGRAY
        		MENUITEM "&Black",      IDM_BKGND_BLACK
    	END
    	POPUP "&Timer"
    	BEGIN
        		MENUITEM "&Start",      IDM_TIMER_START
        		MENUITEM "S&top",  	  IDM_TIMER_STOP, GRAYED
    	END
    	POPUP "&Help"
    	BEGIN
        		MENUITEM "&Help...",  			IDM_APP_HELP
        	    MENUITEM "&About MenuDemo...",	IDM_APP_ABOUT
    	END
END
 RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by MenuDemo.rc

#define IDM_FILE_NEW               40001
#define IDM_FILE_OPEN              40002
#define IDM_FILE_SAVE              40003
#define IDM_FILE_SAVE_AS           40004
#define IDM_APP_EXIT               40005
#define IDM_EDIT_UNDO              40006
#define IDM_EDIT_CUT               40007
#define IDM_EDIT_COPY              40008
#define IDM_EDIT_PASTE             40009
#define IDM_EDIT_CLEAR             40010
#define IDM_BKGND_WHITE            40011
#define IDM_BKGND_LTGRAY           40012
#define IDM_BKGND_GRAY             40013
#define IDM_BKGND_DKGRAY           40014
#define IDM_BKGND_BLACK            40015
#define IDM_TIMER_START            40016
#define IDM_TIMER_STOP             40017
#define IDM_APP_HELP               40018
#define IDM_APP_ABOUT              40019

MENUDEMO.RC��Դ���������������幦�ܱ�����ʾ�����ܱ�������Ϊ��MenuDemo�����������Ŀ�е�����ĸ�������˵����������ĸǰ���롺&����MENUITEM SEPARATOR�������ڡ� Menu Item Properties ï¿½ï¿½ï¿½Ô»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½Ð¡ï¿½ Separator ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä¡ï¿½×¢ï¿½â¹¦ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½Ä¿ï¿½ï¿½ï¿½Ð¡ï¿½ Checked ï¿½ï¿½Ñ¡ï¿½î£¬ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½Ð¡ï¿½ Grayed ï¿½ï¿½Ñ¡ï¿½î¡£ï¿½ï¿½ï¿½Ð£ï¿½ï¿½ï¿½ Background ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ðµï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä¿Ó¦ï¿½Ã°ï¿½Ë³ï¿½ï¿½ï¿½ï¿½ï¿½ë£¬È·ï¿½ï¿½Ê¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Öµï¿½ï¿½Ë³ï¿½ò£¬±ï¿½ï¿½ï¿½Ê½ï¿½ï¿½Òªï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½Ê¶ï¿½ï¿½ï¿½Ö¶ï¿½ï¿½ï¿½ï¿½ï¿½RESOURCE.H�С�

���յ�ͻ��ʽ���ܱ��� File ï¿½ï¿½ï¿½Í¡ï¿½ Edit ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¹Øµï¿½WM_COMMANDѶϢʱ��MENUDEMO��ʽֻʹϵͳ������������ Background ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ð³ï¿½MENUDEMO������������ɫ���������л�ˢ����MENUDEMO.RC��Դ�������У��� White ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½î£¨ï¿½ï¿½ï¿½Ü±ï¿½IDΪIDM_BKGND_WHITE�������ԡ� CHECKED ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ú¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½Ô±ï¿½ï¿½è¶¨Ñ¡ï¿½Ð±ï¿½Ç¡ï¿½ï¿½ï¿½MENUDEMO.C�У�iSelection��ֵ����ʼ��ΪIDM_BKGND_WHITE��

�� Background ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ïµï¿½ï¿½ï¿½ï¿½Ö»ï¿½Ë¢ï¿½à»¥ï¿½Å³â¡£ï¿½ï¿½MENUDEMO.C�յ�һ��WM_COMMANDѶϢ������ѶϢ�е�wParam�ǡ� Background ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ïµï¿½ï¿½ï¿½ï¿½ï¿½Ö®Ò»Ê±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç°Ñ¡ï¿½ÐµÄ±ï¿½ï¿½ï¿½ï¿½ï¿½É«ï¿½Ð³ï¿½ï¿½ï¿½Ñ¡ï¿½Ð±ï¿½Ç£ï¿½ï¿½ï¿½ï¿½Ñ±ï¿½Ç¼Óµï¿½ï¿½ÂµÄ±ï¿½ï¿½ï¿½ï¿½ï¿½É«ï¿½Ï¡ï¿½Îªï¿½Ë£ï¿½ï¿½ï¿½ï¿½ï¿½Òªï¿½Ãµï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½Å£ï¿½

hMenu = GetMenu (hwnd) ;

CheckMenuItem��ʽ����ȡ��Ŀǰ��ѡ�е���Ŀ��

CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;

iSelection��ֵ���趨ΪwParam��ֵ���µı�����ɫ��ѡ�У�

iSelection = wParam ;
CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;

�Ӵ�����еı�����ɫ��DZ��滻Ϊ�µı�����ɫ���Ӵ���ʾ�����Ϊ��Ч״̬��Windowsʹ���µı�����ɫ����Ӵ���

Timerͻ��ʽ���ܱ��г�������ѡ���Start���͡�Stop������ʼʱ����Stop��ѡ���Ϊ��ɫ�ģ���������Դ�������еĹ��ܱ�����һ����������ѡ��Start��ѡ��ʱ��MENUDEMO��ͼ����һ����ʱ��������ɹ�������Ч����Start��ѡ������á�Stop��ѡ�

EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;

���յ�һ��WM_COMMANDѶϢ������wParam���IDM_TIMER_STOPʱ��MENUDEMO��ʽ��ֹͣ���������á� Start ï¿½ï¿½ï¿½î£¬È»ï¿½ï¿½ï¿½ï¿½Ð§ï¿½ï¿½ï¿½ï¿½ Stop ï¿½ï¿½Ñ¡ï¿½î£º

EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;

��ע�⣬�ڼ�ʱ��ִ��ʱ��MENUDEMO��ʽ�������յ�wParam���IDM_TIMER_START��WM_COMMANDѶϢ��ͬ���أ��ڼ�ʱ���ر�ʱ��MENUDEMO��ʽҲ�������յ�wParam���IDM_TIMER_STOP��WM_COMMANDѶϢ��

��MENUDEMO�յ�һ��WM_COMMANDѶϢ������ѶϢ�IJ���wParam���IDM_APP_ABOUT��IDM_APP_HELPʱ��MENUDEMO��ʽ��ʾһ��ѶϢ���飨����һ���У����ǽ���ѶϢ�����Ϊ�Ի����飩��

��MENUDEMO��ʽ�յ�һ��WM_COMMANDѶϢ�������wParam���IDM_APP_EXITʱ�������Լ�����һ��WM_CLOSEѶϢ�����ѶϢ��DefWindowProc�յ�WM_SYSCOMMANDѶϢ��wParam���SC_CLOSEʱ���͸��Ӵ�ѶϢ������ʽ��ѶϢ��ͬ�����ǽ��ڱ����������POPPAD2ʱ����ϸ�о�������⡣

���ܱ���ƹ淶
��

��MENUDEMO�еġ� File ï¿½ï¿½ï¿½Í¡ï¿½ Edit ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½Ä¸ï¿½Ê½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Windows��ʽ�еĸ�ʽ�dz����ơ�Windows��Ŀ��֮һ��Ϊʹ�����ṩһ���׶��Ľ��棬����Ҫ��ʹ����Ϊÿ����ʽ����ѧϰ����������ʽ������� File ï¿½ï¿½ï¿½Í¡ï¿½ Edit ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ã¿ï¿½ï¿½Windows��ʽ�п�������һ�������Ҷ�ʹ��ͬ������ĸ��Alt��������ѡ����ô��Ȼ����춼���ʹ���ߵ�ѧϰ������

���ˡ� File ï¿½ï¿½ï¿½Í¡ï¿½ Edit ï¿½ï¿½Í»ï¿½ï¿½Ê½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½â£¬ï¿½ï¿½ï¿½ï¿½ï¿½Windows��ʽ�Ĺ��ܱ����Dz�ͬ�ġ������һ�����ܱ�ʱ����Ӧ�ÿ�һ�����е�Windows��ʽ�Ծ�������һ�¡���Ȼ���������Ϊ��ij�ʽ�Dz��Եģ�����֪����ȷ�ķ�������ôû�����ܹ���ֹ����ͬʱ��ס���޸�һ�����ܱ���ͨ��ֻ��Ҫ�޸���Դ�������������޸����ij�ʽ�롣��ʹ����Ҫ�ı书�ܱ����λ�ã�Ҳ�����ж������⡣

��Ȼ���ij�ʽ���ܱ��ڶ��������MENUITEM�����������Dz��Ϲ淶�ģ���Ϊ����������׵��´����ѡ�������Ҫ����������ô�����ִ������һ����̾�ţ���ʾ���ܱ��������ͻ��ʽ���ܱ���

���ѵ�һ�ֹ��ܱ����巽��
��

�ڳ�ʽ����Դ�������ж��幦�ܱ���ͨ�����������Ӵ������ӹ��ܱ�����򵥷�����������Ψһ�ķ����������û��ʹ����Դ����������ô����ʹ��CreateMenu��AppendMenu������ʽ�ڳ�ʽ�н������ܱ������������깦�ܱ��ᣬ�����Խ����ܱ����ŷ��͸�CreateWindow������ʹ��SetMenu���趨�Ӵ��Ĺ��ܱ���

�����Ǿ����������CreateMenu�򵥵ذ�һ�����Ŵ��ظ��¹��ܱ���

hMenu = CreateMenu () ;

���ܱ�һ��ʼΪ�ա�AppendMenu�����ܱ�����빦�ܱ��С�������Ϊ���㹦�ܱ����ÿһ��ͻ��ʽ���ܱ��ṩ��ͬ�Ĺ��ܱ����š�ͻ��ʽ���ܱ��ǵ������ɵģ�Ȼ�Ὣͻ��ʽ���ܱ����Ų��붥�㹦�ܱ�����ʽ10-5����ʾ�ij�ʽ����������ַ����������ܱ��ģ�ʵ���ϣ�������ܱ���MENUDEMO��ʽ�еĹ��ܱ���ͬ��Ϊ�˼�˵��������ʹ��ASCII�ִ���

 ï¿½ï¿½Ê½10-5  ��ʹ����Դ������������MENUDEMO��ʽ��ͬ���ܱ���C��ʽ��
hMenu = CreateMenu () ;
hMenuPopup = CreateMenu () ;
AppendMenu 		(hMenuPopup, 	MF_STRING, IDM_FILE_NEW, "&New");
AppendMenu 		(hMenuPopup, 	MF_STRING, IDM_FILE_OPEN, "&Open...");
AppendMenu 		(hMenuPopup, 	MF_STRING, IDM_FILE_SAVE, "&Save");
AppendMenu 		(hMenuPopup,	MF_STRING, IDM_FILE_SAVE_AS, "Save &As...");
AppendMenu 		(hMenuPopup, 	MF_SEPARATOR, 0, NULL) ;
AppendMenu 		(hMenuPopup, 	MF_STRING, IDM_APP_EXIT, "E&xit") ;

AppendMenu 		(hMenu, MF_POPUP, hMenuPopup, "&File") ;

hMenuPopup = CreateMenu () ;
AppendMenu 		(hMenuPopup, 	MF_STRING, IDM_EDIT_UNDO,"&Undo") ;
AppendMenu 		(hMenuPopup, 	MF_SEPARATOR, 0, NULL) ;
AppendMenu 		(hMenuPopup, 	MF_STRING,IDM_EDIT_CUT, "Cu&t") ;
AppendMenu 		(hMenuPopup, 	MF_STRING,IDM_EDIT_COPY,"&Copy") ;
AppendMenu 		(hMenuPopup,  MF_STRING,IDM_EDIT_PASTE,"&Paste") ;
AppendMenu 		(hMenuPopup, MF_STRING,IDM_EDIT_CLEAR,"De&lete") ;
AppendMenu 		(hMenu, 	MF_POPUP, 	hMenuPopup, "&Edit") ;

hMenuPopup = CreateMenu () ;
AppendMenu 	(hMenuPopup, 	MF_STRING| MF_CHECKED, 	IDM_BKGND_WHITE, "&White");
AppendMenu 	(hMenuPopup, 	MF_STRING,          IDM_BKGND_LTGRAY, "&Light Gray");
AppendMenu 	(hMenuPopup, 	MF_STRING,			IDM_BKGND_GRAY,   "&Gray") ;
AppendMenu 	(hMenuPopup, 	MF_STRING,          IDM_BKGND_DKGRAY, "&Dark Gray");
AppendMenu (hMenuPopup, 	MF_STRING,          IDM_BKGND_BLACK,  "&Black") ;

AppendMenu (hMenu, MF_POPUP, hMenuPopup, "&Background") ;
hMenuPopup = CreateMenu () ;
AppendMenu 	(hMenuPopup, 	MF_STRING,          IDM_TIMER_START, "&Start") ;
AppendMenu 	(hMenuPopup, 	MF_STRING | MF_GRAYED, IDM_TIMER_STOP, "S&top") ;

AppendMenu 	(hMenu, 	MF_POPUP, hMenuPopup, "&Timer") ;

hMenuPopup 	= 	CreateMenu () ;

AppendMenu 	(hMenuPopup, 	MF_STRING, 	IDM_HELP_HELP,  	"&Help") ;
AppendMenu 	(hMenuPopup, 	MF_STRING, 	IDM_APP_ABOUT,  	"&About MenuDemo...") ;

AppendMenu (hMenu, MF_POPUP, hMenuPopup, "&Help") ;

����Ϊ����ͬ���������۵㣺ʹ����Դ���������ܱ�ģ�����������ܱ���������׶��Ҹ�������Ҳ���������ʹ������ķ������幦�ܱ�����ֻ���ṩ��һ��ʵ�����ܱ��ķ�������Ȼ��������ʹ�ð������й��ܱ����ִ���ID�����ȵĽṹ������ѹ����ʽ���С���������������ô���ˣ���ô������������Windows���幦�ܱ��ĵ����ַ�����LoadMenuIndirect��ʽ����һ��ָ��MENUITEMTEMPLATE��̬�Ľṹָ�꣬�����ع��ܱ��Ĵ��ţ��ú�ʽ��������Դ�������еij��湦�ܱ�ģ���ᣬ��Windows�й��칦�ܱ������߲����Լ�����һ�¡�

����ͻ��ʽ���ܱ�
��

����������û�ж��㹦�ܱ��е������ʹ�ù��ܱ���Ҳ����˵��������ʹͻ��ʽ���ܱ�������өĻ������κ�λ�á�һ�ַ�����ʹ�û����Ҽ�������ͻ��ʽ���ܱ�����ʽ10-6��ʾ��POPMENU˵�������ַ�����

 ï¿½ï¿½Ê½10-6   POPMENU
POPMENU.C
/*----------------------------------------------------------------------
   POPMENU.C -- Popup Menu Demonstration
                					(c) Charles Petzold, 1998
-------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HINSTANCE hInst ;
TCHAR     		szAppName[] = TEXT ("PopMenu") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     	HWND     			hwnd ;
     	MSG      			msg ;
     	WNDCLASS 		wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, szAppName) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
      		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      							szAppName, MB_ICONERROR) ;
         		return 0 ;
     }
     
     	hInst = hInstance ;
     	hwnd = CreateWindow (	szAppName, TEXT ("Popup Menu Demonstration"),
                     WS_OVERLAPPEDWINDOW,
                     CW_USEDEFAULT, CW_USEDEFAULT,
                     CW_USEDEFAULT, CW_USEDEFAULT,
                     NULL, NULL, hInstance, NULL) ;
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          			TranslateMessage (&msg) ;
          			DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static HMENU	hMenu ;
     	static int 		idColor [5] = {	WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH,
                                  		DKGRAY_BRUSH, BLACK_BRUSH } ;
     	static int 		iSelection 	= IDM_BKGND_WHITE ;
     	POINT        				point ;
     
     	switch (message)
     {
     	case 	WM_CREATE:
          		hMenu = LoadMenu (hInst, szAppName) ;
          		hMenu = GetSubMenu (hMenu, 0) ;
          		return 0 ;

     	case 	WM_RBUTTONUP:
          		point.x = LOWORD (lParam) ;
          		point.y = HIWORD (lParam) ;
          		ClientToScreen (hwnd, &point) ;
          
          		TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL) ;
          		return 0 ;
          
     	case 	WM_COMMAND:
          		switch (LOWORD (wParam))
          		{
          		case 	IDM_FILE_NEW:
          		case 	IDM_FILE_OPEN:
          		case 	IDM_FILE_SAVE:
          		case 	IDM_FILE_SAVE_AS:
          		case 	IDM_EDIT_UNDO:
          		case 	IDM_EDIT_CUT:
          		case 	IDM_EDIT_COPY:
          		case 	IDM_EDIT_PASTE:
          		case 	IDM_EDIT_CLEAR:
               				MessageBeep (0) ;
               				return 0 ;
               
          		case 	IDM_BKGND_WHITE:         	// Note: Logic below
          		case 	IDM_BKGND_LTGRAY:        	//   assumes that IDM_WHITE
          		case 	IDM_BKGND_GRAY:          	//   through IDM_BLACK are
          		case 	IDM_BKGND_DKGRAY:        	//   consecutive numbers in
          		case 	IDM_BKGND_BLACK:         	//   the order shown here.
               
               				CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
               				iSelection = LOWORD (wParam) ;
               				CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
               
               				SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
                    							GetStockObject 
                            (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
               
               				InvalidateRect (hwnd, NULL, TRUE) ;
               				return 0 ;
               
          		case 	IDM_APP_ABOUT:
               				MessageBox (hwnd, TEXT ("Popup Menu Demonstration Program\n")
                           TEXT ("(c) Charles Petzold, 1998"),
                           		szAppName, MB_ICONINFORMATION | MB_OK) ;
               			return 0 ;
               
          		case 	IDM_APP_EXIT:
               				SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               				return 0 ;
               
          		case 	IDM_APP_HELP:
               				MessageBox (hwnd, TEXT ("Help not yet implemented!"),
                            szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               				return 0 ;
          		}
          		break ;
          
     	case 	WM_DESTROY:
          		PostQuitMessage (0) ;
          		return 0 ;
	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 POPMENU.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
POPMENU MENU DISCARDABLE 
BEGIN
    	POPUP "MyMenu"
    	BEGIN
        		POPUP "&File"
        		BEGIN
            		MENUITEM "&New", 				IDM_FILE_NEW
            		MENUITEM "&Open",    			IDM_FILE_OPEN
                    MENUITEM "&Save", 			IDM_FILE_SAVE
            		MENUITEM "Save &As",  	    IDM_FILE_SAVE_AS
            		MENUITEM SEPARATOR
            		MENUITEM "E&xit",   			IDM_APP_EXIT
        		END
        		POPUP "&Edit"
        		BEGIN
            			MENUITEM "&Undo", 		IDM_EDIT_UNDO
            			MENUITEM SEPARATOR
            			MENUITEM "Cu&t",   		IDM_EDIT_CUT
            			MENUITEM "&Copy", 		IDM_EDIT_COPY
            			MENUITEM "&Paste",   		IDM_EDIT_PASTE
            			MENUITEM "De&lete",		IDM_EDIT_CLEAR
END
	POPUP "&Background"
BEGIN
    MENUITEM "&White", 			IDM_BKGND_WHITE, CHECKED
MENUITEM "&Light Gray", 	    IDM_BKGND_LTGRAY
MENUITEM "&Gray",				IDM_BKGND_GRAY
MENUITEM "&Dark Gray",		IDM_BKGND_DKGRAY
MENUITEM "&Black",			IDM_BKGND_BLACK
END
	POPUP "&Help"
BEGIN
MENUITEM "&Help...",   		IDM_APP_HELP
	MENUITEM "&About PopMenu...", IDM_APP_ABOUT
END
  	END
END
 RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by PopMenu.rc

#define IDM_FILE_NEW                  40001
#define IDM_FILE_OPEN                 40002
#define IDM_FILE_SAVE                 40003
#define IDM_FILE_SAVE_AS              40004
#define IDM_APP_EXIT                  40005
#define IDM_EDIT_UNDO                 40006
#define IDM_EDIT_CUT                  40007
#define IDM_EDIT_COPY                 40008
#define IDM_EDIT_PASTE                40009
#define IDM_EDIT_CLEAR                40010
#define IDM_BKGND_WHITE               40011
#define IDM_BKGND_LTGRAY              40012
#define IDM_BKGND_GRAY                40013
#define IDM_BKGND_DKGRAY              40014
#define IDM_BKGND_BLACK               40015
#define IDM_APP_HELP                  40016
#define IDM_APP_ABOUT                 40017

��Դ������POPMENU.RC����Ĺ��ܱ���MENUDEMO.RC�еĹ��ܱ��dz����ơ���ͬ���ǣ��ڶ��㹦�ܱ���ֻ����һ�һ��ͻ��ʽ���ܱ���MyMenu���������С�File������Edit������Background���͡�Help��ѡ����ĸ�ѡ�ֱһ�еس�����ͻ��ʽ���ܱ��ϣ�������ˮƽһ�еس����������ܱ��ϡ�

��WndProc�е�WM_CREATE�����ڼ䣬POPMENUȡ�ô�ͻ��ʽ���ܱ��Ĵ��ţ����Ǵ������֡�MyMenu�����Ǹ�ͻ��ʽ���ܱ���

hMenu = LoadMenu (hInst, szAppName) ;
hMenu = GetSubMenu (hMenu, 0) ;

��WM_RBUTTONUPѶϢ�����ڼ䣬POPMENU�ṩ�˻���ָ���λ�ã�����λ��ת��ΪөĻ���꣬�ٽ�����ֵ���ݸ�TrackPopupMenu��

point.x = LOWORD (lParam) ;
point.y = HIWORD (lParam) ;
ClientToScreen (hwnd, &point) ;

TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, point.x, point.y, 
                					0, hwnd, NULL) ;

Ȼ�ᣬWindows��ʾ�����С�File������Edit������Background���͡�Help�����ͻ��ʽ���ܱ���ѡ�������κ�һ�����ʹǶ�׵�ͻ��ʽ���ܱ���ʾ���ұߣ����ܱ���ʽ��һ��Ĺ��ܱ�һ����

���Ҫʹ����ó�ʽ�������ܱ���ͬ�Ĺ��ܱ�������TrackPopupMenu����������һЩ���⣬��Ϊ��ʽ��Ҫͻ��ʽ���ܱ����š��ڡ�Microsoft Knowledge Base������ID Q99806���ṩһЩ��Ѷ��

ʹ��ϵͳ���ܱ�
��

ʹ��WS_SYSMENU��ʽ�����ĸ��Ӵ�����������е������һ��ϵͳ���ܱ���ť�������Ը�⣬�����޸�������ܱ�����Windows��ʽ��Ƶ����ڣ���ʽд����һ��ѡ�About�����ܱ������ϵͳ���ܱ�����Ȼ���ַ����������������޸�ϵͳ���ܱ�������һ���ڶ̳�ʽ�����ӹ��ܱ��Ŀ���͵������������Ψһ�������ǣ���ϵͳ���ܱ������ӵ�������IDֵ����С�0xF000���������ǽ�����Windowsϵͳ���ܱ�������ʹ�õ�IDֵ���ͻ����Ҫ��ס������Ϊ��Щ�¹��ܱ������Ӵ�ѶϢ������ʽ�д���WM_SYSCOMMANDѶϢʱ���������������WM_SYSCOMMANDѶϢ���͸�DefWindowProc�������������������ôʵ�����ǽ�����ϵͳ���ܱ��ϵ���������ѡ�

��ʽ10-7����ʾ��POORMENU������Ʋ����ĸ��˹��ܱ�������ϵͳ���ܱ��м�����һ���ָ����������������һ�����ɾ����Щ���ӵĹ��ܱ��

 ï¿½ï¿½Ê½10-7  POORMENU
POORMENU.C
/*-------------------------------------------------------------------------
   	POORMENU.C -- 		The Poor Person's Menu
                 						(c) Charles Petzold, 1998
--------------------------------------------------------------------------*/

#include <windows.h>
#define IDM_SYS_ABOUT   				1
#define IDM_SYS_HELP    				2
#define IDM_SYS_REMOVE  			    3

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
static TCHAR szAppName[] = TEXT ("PoorMenu") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     	HMENU    				hMenu ;
     	HWND     				hwnd ;
     	MSG      				msg ;
     	WNDCLASS 			wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      							szAppName, MB_ICONERROR) ;
          		return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("The Poor-Person's Menu"),
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL) ;
     
     	hMenu = GetSystemMenu (hwnd, FALSE) ;
     	AppendMenu	(hMenu, MF_SEPARATOR, 0,        				NULL) ;
     	AppendMenu	(hMenu, MF_STRING, IDM_SYS_ABOUT,  	TEXT ("About...")) ;
     	AppendMenu	(hMenu, MF_STRING, IDM_SYS_HELP,   	TEXT ("Help...")) ;
     	AppendMenu 	(hMenu, MF_STRING, IDM_SYS_REMOVE, 	TEXT ("Remove Additions")) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
    	switch (message)
     	{
     	case 	WM_SYSCOMMAND:
          		switch (LOWORD (wParam))
          		{
          		case 	IDM_SYS_ABOUT:
               				MessageBox (	hwnd, 	TEXT ("A Poor-Person's Menu Program\n")
                      TEXT ("(c) Charles Petzold, 1998"),
                      szAppName, MB_OK | MB_ICONINFORMATION) ;
               				return 0 ;
               
          		case 	IDM_SYS_HELP:
               				MessageBox (	hwnd, TEXT ("Help not yet implemented!"),
                      szAppName, MB_OK | MB_ICONEXCLAMATION) ;
               				return 0 ;
               
          		case 	IDM_SYS_REMOVE:
               				GetSystemMenu (hwnd, TRUE) ;
               				return 0 ;
          		}
          		break ;
          
     		case 	WM_DESTROY:
          				PostQuitMessage (0) ;
          				return 0 ;
     	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}

�������ܱ�ID��POORMENU.C�Ŀ�ʼ���ֶ��壺

#define IDM_ABOUT   		1
#define IDM_HELP    		2
#define IDM_REMOVE  		3

�ڳ�ʽ�Ӵ�����֮�ᣬPOORMENU�õ�һ��ϵͳ���ܱ��Ĵ��ţ�

hMenu = GetSystemMenu (hwnd, FALSE) ;

��һ�κ���GetSystemMenuʱ����Ӧ��Ϊ�޸Ĺ��ܱ���׼�������ڶ��������趨ΪFALSE��

ʹ���ĸ�AppendMenu������ʵ���Թ��ܱ����޸ģ�

AppendMenu 	(hMenu, 	MF_SEPARATOR, 0,           						NULL) ;
AppendMenu 	(hMenu, 	MF_STRING, IDM_SYS_ABOUT,  	TEXT ("About...")) ;
AppendMenu 	(hMenu, 	MF_STRING, IDM_SYS_HELP,   		TEXT ("Help...")) ;
AppendMenu 	(hMenu, 	MF_STRING, IDM_SYS_REMOVE, 	TEXT ("Remove Additions"));

��һ��AppendMenu���������ӷָ�����ѡ��Remove Additions�����ܱ��ʹPOORMENUɾ����Щ���ӵĹ��ܱ����ֻҪ�ѵڶ��������趨ΪTRUE���ٴκ���GetSystemMenu���ɣ�

GetSystemMenu (hwnd, TRUE) ;

��׼ϵͳ���ܱ�������ѡ�Restore��Move��Size��Minimize��Maximize��Close�����Dz���wParam�ֱ���SC_RESTORE��SC_MOVE��SC_SIZE��SC_MINIMUM��SC_MAXIMUM��SC_CLOSE��WM_SYSCOMMANDѶϢ������Windows��ʽһ�㲻�������������������Լ�������ЩѶϢ����������������DefWindowProc����Ҳ����ʹ�����������ķ�������ֹ���߳���ϵͳ���ܱ��ı�׼ѡ�Windows�ļ��л�������һЩϵͳ���ܱ��ı�׼������Ŀ����Щ������Ŀʹ��ʶ����SC_NEXTWINDOW��SC_PREVWINDOW��SC_VSCROLL��SC_HSCROLL��SC_ARRANGE����Ҳ���ᷢ�֣���һЩӦ�ó�ʽ�н���Щ�������ϵͳ���ܱ��Ǻ��ʵġ�

�ı书�ܱ�
��

�����Ѿ����������ʹ��AppendMenu��ʽΪ��ʽ���幦�ܱ��Լ������ܱ�����뵽ϵͳ���ܱ��С���Windows 3.0֮ǰ�������ò�����ʹ��ChangeMenu��ʽ��������ֹ�����ChangeMenu��ʽ�кܶ๦�ܣ������ڵ�ʱ������Windows��������ӵĺ�ʽ֮һ�����ڣ����ຯʽ����ChangeMenu��ʽ��Ҫ���ӣ�����ChangeMenu�Ĺ��ܱ��ֽ�Ϊ����µĺ�ʽ��

  •  AppendMenu ï¿½Ú¹ï¿½ï¿½Ü±ï¿½Î²ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ÂµÄ¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ä¿
    ��
  •  DeleteMenu É¾ï¿½ï¿½ï¿½ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ÐµÄ¹ï¿½ï¿½Ü±ï¿½ï¿½î²¢ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä¿
    ��
  •  InsertMenu ï¿½Ú¹ï¿½ï¿½Ü±ï¿½ï¿½Ð²ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä¿
    ��
  •  ModifyMenu ï¿½Þ¸ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ÐµÄ¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ä¿
    ��
  •  RemoveMenu ï¿½Ó¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ä³Ò»ï¿½ï¿½Ä¿
    ��

������ܱ�����һ��ͻ��ʽ���ܱ�����ôDeleteMenu��RemoveMenu֮�������ͺ���Ҫ��DeleteMenu���ͻ��ʽ���ܱ�����RemoveMenu���������

�������ܱ�����
��

��������ʹ�ù��ܱ�ʱһЩ���õĺ�ʽ��

�����ı䶥�㹦�ܱ���ʱ��ֱ��Windows�ػ����ܱ���ʱ����ʾ�����ĸı䡣������ͨ�����к�����ǿ��ִ�й��ܱ����£�

DrawMenuBar (hwnd) ;

ע�⣬DrawMenuBar�IJ������Ӵ����Ŷ����ǹ��ܱ����š�

������ʹ���������������ͻ��ʽ���ܱ��Ĵ��ţ�

hMenuPopup = GetSubMenu (hMenu, iPosition) ;

����iPosition��hMenuָʾ�Ķ��㹦�ܱ���ͻ��ʽ���ܱ������������ʼΪ0����Ȼ����������������ʽ��ʹ��ͻ��ʽ���ܱ����ţ�������AppendMenu��ʽ�У���

������ʹ�����������ö��㹦�ܱ�����ͻ��ʽ���ܱ���Ŀǰ��������

iCount = GetMenuItemCount (hMenu) ;

������ȡ��ͻ��ʽ���ܱ���Ĺ��ܱ�ID��

id = GetMenuItemID (hMenuPopup, iPosition) ;

����iPosition�ǹ��ܱ�����ͻ��ʽ���ܱ��е�λ�ã���0��ʼ����

��MENUDEMO�����Ѿ��������ѡ�С�����ȡ��ѡ��ͻ��ʽ���ܱ��е�ijһ�

CheckMenuItem (hMenu, id, iCheck) ;

��MENUDEMO�У�hMenu�Ƕ��㹦�ܱ��Ĵ��ţ�id�ǹ��ܱ�ID����iCheck��ֵ��MF_CHECKED��MF_UNCHECKED�����hMenu��ͻ��ʽ���ܱ����ţ���ô����id��λ�����������ǹ��ܱ�ID�����ʹ�������������Ļ�����ô�������ڵ����������а���MF_BYPOSITION�����磺

CheckMenuItem (hMenu, iPosition, MF_CHECKED | MF_BYPOSITION) ;

���˵�����������MF_ENABLED��MF_DISABLED��MF_GRAYED�⣬EnableMenuItem��ʽ��CheckMenuItem��ʽ����ɵĹ������ơ�������ھ���ͻ��ʽ���ܱ��Ķ��㹦�ܱ�����ʹ��EnableMenuItem����ô�����ڵ�����������ʹ��MF_BYPOSITIONʶ���֣���Ϊ���ܱ���û�й��ܱ�ID�����ǽ��ڱ���������ʾ��POPPAD2��ʽ�п���EnableMenuItem��һ�����ӡ� HiliteMenuItemҲ�����CheckMenuItem��EnableMenuItem��������ʹ�õ���MF_HILITE��MF_UNHILITE�������ڹ��ܱ���֮���ƶ�ʱ��Windowsʹ�÷�����ʾ��ʽ������ʾ���ܱ����ͨ������Ҫʹ��HiliteMenuItem��

������Ҫ�����Ĺ��ܱ���Щʲô�أ����ǵ������ڹ��ܱ���ʹ������Щ�ִ���������͸������ĺ������ع�һ�£�

iCharCount = GetMenuString (hMenu, id, pString, iMaxCount, iFlag) ;

iFlag������MF_BYCOMMAND������id�ǹ��ܱ�ID����Ҳ������MF_BYPOSITION�����е�id��λ������������ʽ���ִ���iMaxCount��λԪ�鸴�Ƶ�pString�У������ظ��Ƶ�λԪ������

������Ҳ��֪�����ܱ���Ŀǰ��������ʲô��

iFlags = GetMenuState (hMenu, id, iFlag) ;

ͬ���أ�iFlag������MF_BYCOMMAND��MF_BYPOSITION������ֵiFlags��Ŀǰ�������Ե���ϣ�������ͨ����MF_DISABLED��MF_GRAYED��MF_CHECKED��MF_MENUBREAK��MF_MENUBARBREAK��MF_SEPARATORʶ���ֵļ��������Ŀǰ�����ԡ�

Ҳ���������Թ��ܱ�����һЩ�˽⡣��ʱ��������֪���������������Ҫ���ܱ�ʱ��Ӧ����δ�����������ʹ�������������������ܱ���

DestroyMenu (hMenu) ;

�Ӷ�ʹ���ܱ�������Ч��

�������ܱ��ķ���ͳ����
��

������������΢ƫ�����������۵����⡣��������ij�ʽ��û������ʽ���ܱ������ǽ����˶��û��ͻ��ʽ���ܱ��Ķ��㹦�ܱ���������SetMenu�ڶ��㹦�ܱ�֮���л����ǻ���ʲô���������أ�����Lotus 1-2-3����ʽ������ģʽ���ܱ���������ʽ10-8�е�NOPOPUPS��ʽչʾ�˴�������������������ʽ�У���File���͡�Edit������MENUDEMO��ʽ�е����ƣ�����ȴ����һ�ֶ��㹦�ܱ���ʾ������

 ï¿½ï¿½Ê½10-8  NOPOPUPS
NOPOPUPS.C
/*-------------------------------------------------------------------------
   	NOPOPUPS.C -- 		Demonstrates No-Popup Nested Menu
                 						(c) Charles Petzold, 1998
-------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     	static TCHAR 	szAppName[] = TEXT ("NoPopUps") ;
     	HWND         				hwnd ;
     	MSG          				msg ;
     	WNDCLASS     			wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName = szAppName ;
     	if (!RegisterClass (&wndclass))
     	{
          	MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      					szAppName, MB_ICONERROR) ;
          	return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, 
                 TEXT ("No-Popup Nested Menu Demonstration"),
                 WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT, CW_USEDEFAULT,
                 CW_USEDEFAULT, CW_USEDEFAULT,
                 NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static HMENU 	hMenuMain, hMenuEdit, hMenuFile ;
     	HINSTANCE    		hInstance ;
     	switch (message)
     {
     	case 	WM_CREATE:
          		hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
          
          		hMenuMain = LoadMenu (hInstance, TEXT ("MenuMain")) ;
          		hMenuFile = LoadMenu (hInstance, TEXT ("MenuFile")) ;
          		hMenuEdit = LoadMenu (hInstance, TEXT ("MenuEdit")) ;
          
          		SetMenu (hwnd, hMenuMain) ;
          		return 0 ;
          
     	case 	WM_COMMAND:
          		switch (LOWORD (wParam))
          		{
          		case 	IDM_MAIN:
               				SetMenu (hwnd, hMenuMain) ;
               				return 0 ;
               
          		case 	IDM_FILE:
               				SetMenu (hwnd, hMenuFile) ;
               				return 0 ;
               
          		case 	IDM_EDIT:
               				SetMenu (hwnd, hMenuEdit) ;
               				return 0 ;
               
          		case 	IDM_FILE_NEW:
          		case 	IDM_FILE_OPEN:
          		case 	IDM_FILE_SAVE:
          		case 	IDM_FILE_SAVE_AS:
          		case 	IDM_EDIT_UNDO:
          		case 	IDM_EDIT_CUT:
          		case 	IDM_EDIT_COPY:
          		case 	IDM_EDIT_PASTE:
          		case 	IDM_EDIT_CLEAR:
               				MessageBeep (0) ;
               				return 0 ;
          		}
          		break ;
          
     	case	WM_DESTROY:
          		SetMenu (hwnd, hMenuMain) ;
          		DestroyMenu (hMenuFile) ;
          		DestroyMenu (hMenuEdit) ;

          		PostQuitMessage (0) ;
          		return 0 ;
     }
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 NOPOPUPS.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
MENUMAIN MENU DISCARDABLE 
BEGIN
    	MENUITEM "MAIN:",         0, INACTIVE
    	MENUITEM "&File...",  IDM_FILE
    	MENUITEM "&Edit...", 	IDM_EDIT
END
MENUFILE MENU DISCARDABLE 							
BEGIN
    	MENUITEM "FILE:",      	0, INACTIVE
    	MENUITEM "&New",  									IDM_FILE_NEW
    	MENUITEM "&Open...",  					  		IDM_FILE_OPEN
    	MENUITEM "&Save",   				 				IDM_FILE_SAVE
    	MENUITEM "Save &As", 		 					IDM_FILE_SAVE_AS
    	MENUITEM "(&Main)",   	                  						IDM_MAIN
END
MENUEDIT MENU DISCARDABLE 							
BEGIN
    MENUITEM "EDIT:", 				0, INACTIVE
    MENUITEM "&Undo",       							IDM_EDIT_UNDO
    MENUITEM "Cu&t",  			IDM_EDIT_CUT
    MENUITEM "&Copy", 			IDM_EDIT_COPY
    MENUITEM "&Paste",  			IDM_EDIT_PASTE
    MENUITEM "De&lete", 		   	IDM_EDIT_CLEAR
    MENUITEM "(&Main)", 			IDM_MAIN
END
 RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by NoPopups.rc

#define IDM_FILE                   40001
#define IDM_EDIT                   40002
#define IDM_FILE_NEW               40003
#define IDM_FILE_OPEN              40004
#define IDM_FILE_SAVE              40005
#define IDM_FILE_SAVE_AS           40006
#define IDM_MAIN                   40007
#define IDM_EDIT_UNDO              40008
#define IDM_EDIT_CUT               40009
#define IDM_EDIT_COPY              40010
#define IDM_EDIT_PASTE             40011
#define IDM_EDIT_CLEAR             40012

��Microsoft Developer Studio�У����������������ܱ���������һ�����ӡ�Insert����ѡ��Resource�����Σ�ÿ�����ܱ���һ����ͬ�����ơ����Ӵ�ѶϢ������ʽ����WM_CREATEѶϢʱ��Windows��ÿ�����ܱ���Դ��������壺

hMenuMain = LoadMenu (hInstance, TEXT ("MenuMain")) ;
hMenuFile = LoadMenu (hInstance, TEXT ("MenuFile")) ;
hMenuEdit = LoadMenu (hInstance, TEXT ("MenuEdit")) ;

��ʼʱ����ʽֻ��ʾ�����ܱ���

SetMenu (hwnd, hMenuMain) ;

�����ܱ�ʹ���ִ���MAIN:������File...���͡�Edit...���г�������ѡ�Ȼ������MAIN:���ǽ��õģ����������ʹWM_COMMANDѶϢ�����͵��Ӵ�ѶϢ������ʽ����File���͡�Edit�����ܱ����ԡ�FILE:���͡�EDIT:����ʼ����ʾ�������ӹ��ܱ���ÿ�����ܱ�������һ����ִ���(Main)������ʾ���ص������ܱ��������������ܱ�֮������л��Ǻܼ򵥵ģ�

case 	WM_COMMAND :
     	switch (wParam)
     	{
     	case 	IDM_MAIN :
		SetMenu (hwnd, hMenuMain) ;
          		return 0 ;

     	case 	IDM_FILE :
          		SetMenu (hwnd, hMenuFile) ;
          		return 0 ;

     	case 	IDM_EDIT :
          		SetMenu (hwnd, hMenuEdit) ;
          		return 0 ;

 	�����г�ʽ
    	}
     	break ;

��WM_DESTROYѶϢ�����ڼ䣬NOPOPUPS����ʽ�Ĺ��ܱ��趨Ϊ�����ܱ���������DestroyMenu�������File���͡�Edit�����ܱ������Ӵ������ʱ�������ܱ������Զ������

���̼��ټ�
��

���ټ��Dz���WM_COMMANDѶϢ����Щ�������WM_SYSCOMMAND���ļ���ϡ�����ʱ�ò£¬³ï¿½Ê½Ê¹ï¿½Ã¼ï¿½ï¿½Ù¼ï¿½ï¿½ï¿½ï¿½Ø¸ï¿½ï¿½ï¿½ï¿½Ã¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ä¶ï¿½ï¿½ï¿½ï¿½ï¿½È»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ù¼ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ö´ï¿½Ð·Ç¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½ï¿½Ü£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ç£¬ï¿½ï¿½ï¿½ï¿½Windows��ʽ����һ��������Delete����Clear��ѡ��ġ�Edit�����ܱ�����Щ��ʽϰ���϶���Del��ָ��Ϊ��ѡ��ļ��ټ���ʹ���߿���ͨ���� Alt ï¿½ï¿½ï¿½ï¿½ï¿½Ó¹ï¿½ï¿½Ü±ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½ Delete ï¿½ï¿½Ñ¡ï¿½î£¬ï¿½ï¿½ï¿½ï¿½Ö»ï¿½è°´ï¿½Â¼ï¿½ï¿½Ù¼ï¿½ Del ï¿½ï¿½ï¿½ï¿½ï¿½Ó´ï¿½Ñ¶Ï¢ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê½ï¿½Õµï¿½Ò»ï¿½ï¿½WM_COMMANDѶϢʱ��������ȷ��ʹ�õ��ǹ��ܱ����Ǽ��ټ���

ΪʲôҪʹ�ü��ټ�
��

��Ҳ�����ʣ�Ϊʲô��Ӧ��ʹ�ü��ټ�?Ϊʲô����ֱ������WM_KEYDOWN��WM_CHARѶϢ���Լ�ʵ��ͬ���Ĺ��ܱ������أ��ô����������أ����һ�����Ӵ�Ӧ�ó�ʽ������Ȼ�������ؼ���ѶϢ������ʹ�ü��ټ����Եõ�һЩ�ô���������Ҫ�ѹ��ܱ��ͼ��ټ��Ĵ�����ʽ��дһ�顣

����ж���Ӵ��Ͷ���Ӵ�ѶϢ������ʽ��Ӧ�ó�ʽ��˵�����ټ��Ƿdz���Ҫ�ġ����������������ģ�Windows������ѶϢ���͸�Ŀǰ��Ӵ����Ӵ�ѶϢ������ʽ��Ȼ����춼��ټ��� Windows��WM_COMMANDѶϢ���͸��Ӵ�ѶϢ������ʽ�����Ӵ�ѶϢ������ʽ�Ĵ�����Windows��ʽTranslateAccelerator�и�����ͨ���������Ӵ���Ҳ��ӵ�й��ܱ����Ӵ�������ζ������ÿ���Ӵ�ѶϢ������ʽ���Ѽ��ټ��IJ���������ʽ��дһ�顣

����������Ӵ�����ʾ�����У�ʹ���˷�ϵͳģ̬�Ի����飨����һ���л����ۣ��������Ӵ�����ô���ֺô��ͱ�÷dz���Ҫ���������һ���ض��ļ��ټ��Ա��ڲ�ͬ���Ӵ�֮���ƶ�����ô��ֻ��Ҫһ���Ӵ�ѶϢ������ʽ�����������ʽ�����Ӵ��Ͳ����յ����ټ�������WM_COMMANDѶϢ��

���ż��ټ��ļ�������
��

�����ϣ�������ʹ���κ������������Ԫ����ͬShift����Ctrl����Alt����������ټ���Ȼ������Ӧ�þ���ʹӦ�ó�ʽ֮��Э��һ�£����Ҿ����������Windows�ļ���ʹ�á��ڼ��ټ��У�Ӧ�ñ���ʹ��Tab��Enter��Esc��Spacebar������Ϊ��Щ������������ϵͳ���ܡ�

���ټ��������;�Dz�����ʽ�ġ�Edit�����ܱ��еĸ��Ϊ��Щ���ܱ����Ƽ��ļ��ټ���Windows 3.0��Windows 3.1֮�����в�ͬ�����ͨ����Ҫ֧Ԯ�������е��¾����׼��ټ���

��10-2
���� �ɼ��ټ� �¼��ټ�
Undo Alt+Backspace Ctrl+Z
Cut Shift+Del Ctrl+X
Copy Ctrl+Ins Ctrl+C
Paste Shift+Ins Ctrl+V
Delete��Clear Del Del

��һ�ֳ��õ������������������Ѷ�Ĺ��ܼ�F1��Ӧ�ñ���ʹ��F4��F5��F6������Ϊ��Щ�������ڶ����ļ����棨MDI����ʽ�����������Ĺ��ܣ����ڵ�ʮ���������ۣ���

���ټ���
��

��������Developer Studio�ж�����ټ�����Ϊ���ó�ʽ��������ټ�����Ϊ���ף������ͳ�ʽ����ͬ�����ƣ��빦�ܱ���ͼʾ��Ҳ��ͬ����

ÿ�����ټ������� Accel Properties ï¿½Ô»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¶ï¿½ï¿½ï¿½ï¿½ID�Ͱ�����ϡ�������Ѿ������˹��ܱ������ܱ�ID�����������ʽ�嵥�����У���˲���Ҫ�������ǡ�

���ټ��������������ASCII��Ԫ��Shift��Ctrl��Alt������ϡ�����ͨ������ĸǰ���롺^����ָ������Ctrl����ASCII��Ԫ��Ҳ���Դ�����ʽ�嵥������ѡȡ�������

����Ϊ���ܱ������ټ�ʱ��Ӧ�ý�������ϰ��������ܱ���������С���λ��Ԫ��\t������������ټ��ָ�������ټ����ڵڶ��С�Ϊ���ڹ��ܱ���Ϊ���ټ����ϱ�ǣ����������֡�Ctrl������Shift����Alt��֮�����һ����+���ź�һ�����������磬��Shift+F6����Ctrl+F6������

���ټ���������
��

�����ij�ʽ�У���ʹ��LoadAccelerators��ʽ�Ѽ��ټ�����������壬����øñ��Ĵ��š� LoadAccelerators�����dz������LoadIcon��LoadCursor��LoadMenu������

���ȣ��Ѽ��ټ����Ĵ��Ŷ���Ϊ��̬HANDLE��

HANDLE hAccel ;

Ȼ��������ټ�����

hAccel = LoadAccelerators (hInstance, TEXT ("MyAccelerators")) ;

����ͼʾ���α�͹��ܱ�һ����������ʹ��һ����ֵ������ټ��������ƣ�Ȼ����LoadAccelerators�����к�MAKEINTRESOURCE�޼�һ��ʹ�ø���ֵ�����߰�������˫�����ڣ�ǰ�������Ԫ��#����

���̴���ת��
��

�������ǽ����۵��������г�ʽ�룬�ڱ����У�����ĿǰΪֹ����������Windows��ʽ�ж�ʹ�ù����ǡ���Щ��ʽ���DZ�׼��ѶϢ��Ȧ��

while	(GetMessage (&msg, NULL, 0, 0))
{
  	TranslateMessage (&msg) ;
	DispatchMessage (&msg) ;
}

�������ͷ�Ƕγ�ʽ������޸ģ��Ա�ʹ�ü��ټ���

while	 (GetMessage (&msg, NULL, 0, 0))
{
    	if (!TranslateAccelerator (hwnd, hAccel, &msg))
     {
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
}

TranslateAccelerator��ʽȷ�ϴ����msgѶϢ�ṹ�е�ѶϢ�Ƿ�Ϊ����ѶϢ������ǣ��ú�ʽ����Ѱ����ΪhAccel�ļ��ټ���������ҵ���һ�����ϵģ�����д���Ϊhwnd���Ӵ�ѶϢ������ʽ��������ټ�ID��ϵͳ���ܱ��Ĺ��ܱ���һ�£���ѶϢ����WM_SYSCOMMAND������ѶϢΪWM_COMMAND��

��TranslateAccelerator����ʱ�����ѶϢ�Ѿ���ת���������Ѿ������͸��Ӵ�ѶϢ������ʽ������ô����ֵΪ���㣻���򣬴���ֵΪ0�����TranslateAccelerator����һ������ֵ���򲻺���TranslateMessage��DispatchMessage�����Ǿ�����Ȧ�ص�GetMessage�����С�

TranslateMessage�еIJ���hwnd�������е���׸����ΪѶϢ��Ȧ�е�����������ʽ��û��Ҫ��������������⣬ѶϢ�ṹ�������ṹ����msg����һ������hwnd�ij�Ա�������Ӵ����š�

�ú�ʽ��Щ��ͬ��ԭ����춣�msg�ṹ����λ��GetMessage�������롣��GetMessage�ĵڶ�������ΪNULLʱ����ʽ����ѰӦ�ó�ʽ�����Ӵ���ѶϢ����GetMessage����ʱ��msg�ṹ��hwnd�ǽ�Ҫ���ѶϢ֮�Ӵ����Ӵ����š�Ȼ������TranslateAccelerator�Ѽ���ѶϢת��ΪWM_COMMAND��WM_SYSCOMMANDѶϢʱ����ʹ�ú�ʽ�ĵ�һ������ָ�����Ӵ�����hwnd�������Ӵ�����msg.hwnd��Windows�������������м��ټ�ѶϢ���͸�ͬһ�Ӵ�ѶϢ������ʽ�ģ���ʹ��һ��Ӧ���Ӵ�Ŀǰӵ�����뽹�㡣��ϵͳģ̬�Ի��������ѶϢ����ӵ�����뽹��ʱ��TranslateAccelerator����ת������ѶϢ����Ϊ��Щ�Ӵ���ѶϢ�Dz�������ʽ��ѶϢ��Ȧ�ġ�

��ijЩ����£�������ʽ����һ���Ӵ�������һ����ϵͳģ̬�Ի����飩ӵ�����뽹��ʱ����Ҳ������ת�����ټ�����������һ���п�����δ������������

���ռ��ټ�ѶϢ
��

�����ټ���ϵͳ���ܱ��еĹ��ܱ������Ӧʱ��TranslateAccelerator���Ӵ�ѶϢ������ʽ����һ��WM_SYSCOMMANDѶϢ������TranslateAccelerator���Ӵ�ѶϢ������ʽ����һ��WM_COMMANDѶϢ���±���ʾΪ���ֿ��ܽ��յ���WM_COMMANDѶϢ����ЩѶϢ��춼��ټ������ܱ������Լ����Ӵ������

��10-3
  ���ټ� ���ܱ� ������
LOWORD (wParam) ���ټ�ID ���ܱ�ID ������ID
HIWORD (wParam) 1 0 ֪ͨ��
lParam 0 0 ���Ӵ�����

������ټ���һ�����ܱ����Ӧ����ô�Ӵ�ѶϢ������ʽ�����յ�WM_INITMENU��WM_INITMENUPOPUP��WM_MENUSELECTѶϢ���ͺ���ѡ���˹��ܱ�ѡ��һ�����ڴ���WM_INITMENUPOPUPʱ����ʽ�������úͽ���ͻ��ʽ���ܱ��еĹ��ܱ����ˣ���ʹ�ü��ټ�ʱ������Ȼ�ܹ�ʵ�����๦�ܡ�������ټ���һ�����û�����Ч���Ĺ��ܱ������Ӧ����ô��TranslateAccelerator��ʽ�Ͳ������Ӵ�ѶϢ������ʽ����WM_COMMAND��WM_SYSCOMMANDѶϢ��

�����Ӵ��Ѿ�����С������ôTranslateAccelerator��Ϊ�����õ�ϵͳ���ܱ������Ӧ�ļ��ټ����Ӵ�ѶϢ������ʽ����WM_SYSCOMMANDѶϢ��������WM_COMMANDѶϢ��TranslateAcceleratorҲ��Ϊû���κι��ܱ�����֮��Ӧ�ļ��ټ��������Ӵ�ѶϢ������ʽ����WM_COMMANDѶϢ��

���ܱ�����ټ�Ӧ�ó�ʽPOPPAD
��

�ڵھ��£����ǽ�����һ������POPPAD1�ij�ʽ����ʹ�������Ӵ��༭��������ʵ�������ıʼDZ����ܡ�����һ���У����ǽ����롸File���͡�Edit�����ܱ������ƴ˳�ʽΪPOPPAD2����Edit�����ܱ��Ĺ��ܱ���Ĺ���ȫ�����ã����ǽ��ڵ�ʮһ������ɡ�File�����ܣ��ڵ�ʮ��������ɡ�Print�����ܡ�POPPAD2���ʽ10-9��ʾ��

 ï¿½ï¿½Ê½10-9  POPPAD2
POPPAD2.C
/*---------------------------------------------------------------------------
   	POPPAD2.C -- 		Popup Editor Version 2 (includes menu)
                						(c) Charles Petzold, 1998
---------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

#define ID_EDIT     			1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
TCHAR szAppName[] = TEXT ("PopPad2") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    						PSTR szCmdLine, int iCmdShow)
{
     	HACCEL   		hAccel ;
     	HWND     			hwnd ;
     	MSG      			msg ;
     	WNDCLASS 		wndclass ;
     	wndclass.style         				= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   				= WndProc ;
     	wndclass.cbClsExtra    				= 0 ;
     	wndclass.cbWndExtra    				= 0 ;
     	wndclass.hInstance     				= hInstance ;
     	wndclass.hIcon         				= LoadIcon (hInstance, szAppName) ;
     	wndclass.hCursor       				= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 			= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  			= szAppName ;
     	wndclass.lpszClassName 			= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                    szAppName, MB_ICONERROR) ;
          		return 0 ;
     	}
     
     	hwnd = CreateWindow (	szAppName, szAppName,
                       WS_OVERLAPPEDWINDOW,
                     GetSystemMetrics (SM_CXSCREEN) / 4,
                     GetSystemMetrics (SM_CYSCREEN) / 4,
                     GetSystemMetrics (SM_CXSCREEN) / 2,
                     GetSystemMetrics (SM_CYSCREEN) / 2,
                     NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ; 
     
     	hAccel = LoadAccelerators (hInstance, szAppName) ;
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		if (!TranslateAccelerator (hwnd, hAccel, &msg))
          			{
               				TranslateMessage (&msg) ;
               				DispatchMessage (&msg) ;
          		}
     	}
     	return msg.wParam ;
}

AskConfirmation (HWND hwnd)
{
     	return MessageBox (	hwnd, TEXT ("Really want to close PopPad2?"),
                        	szAppName, MB_YESNO | MB_ICONQUESTION) ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static HWND 	hwndEdit ;
     	int         				iSelect, iEnable ;
     
     	switch (message)
     	{
     	case 	WM_CREATE:
          		hwndEdit = CreateWindow (TEXT ("edit"), NULL,
                   WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
                   WS_BORDER | ES_LEFT | ES_MULTILINE |
                   ES_AUTOHSCROLL | ES_AUTOVSCROLL,
                   0, 0, 0, 0, hwnd, (HMENU) ID_EDIT,
                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
          		return 0 ;
          
     	case 	WM_SETFOCUS:
          		SetFocus (hwndEdit) ;
          		return 0 ;
          
     	case 	WM_SIZE: 
          		MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ;
          		return 0 ;
          
     	case 	WM_INITMENUPOPUP:
          		if (lParam == 1)
          		{
               				EnableMenuItem ((HMENU) wParam, IDM_EDIT_UNDO,
                    		SendMessage (hwndEdit, EM_CANUNDO, 0, 0) ?
                            MF_ENABLED : MF_GRAYED) ;
               
               				EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE,
                    		IsClipboardFormatAvailable (CF_TEXT) ?
                            MF_ENABLED : MF_GRAYED) ;
               
               				iSelect = SendMessage (hwndEdit, EM_GETSEL, 0, 0) ;
               
               				if (HIWORD (iSelect) == LOWORD (iSelect))
                    						iEnable = MF_GRAYED ;
               				else
                    						iEnable = MF_ENABLED ;
               
               EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,  iEnable) ;
               EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,  iEnable) ;
               EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, iEnable) ;
               				return 0 ;
          		}
          		break ;
     	case 	WM_COMMAND:
          		if (lParam)
          		{
               			if (LOWORD (lParam) == ID_EDIT &&
                        (HIWORD (wParam) == EN_ERRSPACE ||
                        HIWORD (wParam) == EN_MAXTEXT))
                    	MessageBox (hwnd, TEXT ("Edit control out of space."),
                        szAppName, MB_OK | MB_ICONSTOP) ;
               			return 0 ;
          }
          		else 	switch (LOWORD (wParam))
          		{
          		case 	IDM_FILE_NEW:
          		case 	IDM_FILE_OPEN:
          		case 	IDM_FILE_SAVE:
          		case 	IDM_FILE_SAVE_AS:
          		case 	IDM_FILE_PRINT:
               				MessageBeep (0) ;
               				return 0 ;
          
          		case 	IDM_APP_EXIT:
               				SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               				return 0 ;

          		case 	IDM_EDIT_UNDO:
               				SendMessage (hwndEdit, WM_UNDO, 0, 0) ;
               				return 0 ;
          
          		case 	IDM_EDIT_CUT:
               				SendMessage (hwndEdit, WM_CUT, 0, 0) ;
               				return 0 ;
          
          		case 	IDM_EDIT_COPY:
               				SendMessage (hwndEdit, WM_COPY, 0, 0) ;
               				return 0 ;
          
          		case 	IDM_EDIT_PASTE:
               				SendMessage (hwndEdit, WM_PASTE, 0, 0) ;
               				return 0 ;
          
          		case 	IDM_EDIT_CLEAR:
               				SendMessage (hwndEdit, WM_CLEAR, 0, 0) ;
               				return 0 ;

		case 	IDM_EDIT_SELECT_ALL:
               				SendMessage (hwndEdit, EM_SETSEL, 0, -1) ;
               				return 0 ;
          
          		case 	IDM_HELP_HELP:
               				MessageBox (hwnd, TEXT ("Help not yet implemented!"),
                            szAppName, MB_OK | MB_ICONEXCLAMATION) ;
               				return 0 ;
          
          		case 	IDM_APP_ABOUT:
               				MessageBox (hwnd, TEXT ("POPPAD2 (c) Charles Petzold, 1998"),
                           	szAppName, MB_OK | MB_ICONINFORMATION) ;
               				return 0 ;
          		}
        		break ;
          
 	case	WM_CLOSE:
          		if (IDYES == AskConfirmation (hwnd))
               				DestroyWindow (hwnd) ;
          		return 0 ;
          
     	case 	WM_QUERYENDSESSION:
          		if (IDYES == AskConfirmation (hwnd))
               				return 1 ;
          		else
               				return 0 ;
          
     	case 	WM_DESTROY:
          		PostQuitMessage (0) ;
          		return 0 ;
     }
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 POPPAD2.RC ��ժ¼��
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Icon
POPPAD2      		ICON    DISCARDABLE     "poppad2.ico"
/////////////////////////////////////////////////////////////////////////////
// Menu
POPPAD2 MENU DISCARDABLE 
BEGIN
    	POPUP "&File"						
	BEGIN
     		MENUITEM "&New", 						IDM_FILE_NEW
        		MENUITEM "&Open...",  				IDM_FILE_OPEN
        		MENUITEM "&Save",  					IDM_FILE_SAVE
        		MENUITEM "Save &As...",  			IDM_FILE_SAVE_AS
        		MENUITEM SEPARATOR
        		MENUITEM "&Print",   					IDM_FILE_PRINT
        		MENUITEM SEPARATOR	
        		MENUITEM "E&xit",  				IDM_APP_EXIT
    	END
    	POPUP "&Edit"
    	BEGIN
        		MENUITEM "&Undo\tCtrl+Z",			IDM_EDIT_UNDO
        		MENUITEM SEPARATOR
        		MENUITEM "Cu&t\tCtrl+X",     	 			IDM_EDIT_CUT
        		MENUITEM "&Copy\tCtrl+C", 			IDM_EDIT_COPY
        		MENUITEM "&Paste\tCtrl+V", 			IDM_EDIT_PASTE
        		MENUITEM "De&lete\tDel",  				IDM_EDIT_CLEAR
        		MENUITEM SEPARATOR
        		MENUITEM "&Select All",  				IDM_EDIT_SELECT_ALL
    	END
    	POPUP "&Help"
    	BEGIN
        		MENUITEM "&Help...",     					IDM_HELP_HELP
        		MENUITEM "&About PopPad2...", 		IDM_APP_ABOUT
    	END
END

/////////////////////////////////////////////////////////////////////////////
// Accelerator
POPPAD2 ACCELERATORS DISCARDABLE 
BEGIN
    	VK_BACK,        		IDM_EDIT_UNDO,          VIRTKEY, ALT, NOINVERT
    	VK_DELETE,      		IDM_EDIT_CLEAR,   		VIRTKEY, NOINVERT
    	VK_DELETE,      		IDM_EDIT_CUT,           VIRTKEY, SHIFT, NOINVERT
    	VK_F1,          		IDM_HELP_HELP,          VIRTKEY, NOINVERT
    	VK_INSERT,      		IDM_EDIT_COPY,          VIRTKEY, CONTROL, NOINVERT
    	VK_INSERT,      		IDM_EDIT_PASTE,         VIRTKEY, SHIFT, NOINVERT
    	"^C",         IDM_EDIT_COPY,          ASCII,  NOINVERT
    	"^V",         IDM_EDIT_PASTE,         ASCII,  NOINVERT
    	"^X",         IDM_EDIT_CUT,           ASCII,  NOINVERT
    	"^Z",         IDM_EDIT_UNDO,          ASCII,  NOINVERT
END
 RESOURCE.H ��ժ¼��
// Microsoft Developer Studio generated include file.
// Used by POPPAD2.RC

#define IDM_FILE_NEW                 40001
#define IDM_FILE_OPEN                40002
#define IDM_FILE_SAVE                40003
#define IDM_FILE_SAVE_AS             40004
#define IDM_FILE_PRINT               40005
#define IDM_APP_EXIT                 40006
#define IDM_EDIT_UNDO                40007
#define IDM_EDIT_CUT                 40008
#define IDM_EDIT_COPY                40009
#define IDM_EDIT_PASTE               40010
#define IDM_EDIT_CLEAR               40011
#define IDM_EDIT_SELECT_ALL          40012
#define IDM_HELP_HELP                40013
#define IDM_APP_ABOUT                40014
POPPAD2.ICO

��


��

POPPAD2.RC��Դ�������������ܱ��ͼ��ټ�������ע�⵽�����м��ټ�����ʾ����λ��Ԫ��\t����ġ�Edit��ͻ��ʽ���ܱ����ִ��С�

���ù��ܱ���
��

�Ӵ�ѶϢ������ʽ�Ĺ����������ú���Ч����Edit�����ܱ��е�ѡ�������ڴ���WM_INITMENUPOPUPʱ��ɡ����ȣ���ʽ����Ƿ�Ҫ��ʾ��Edit��ͻ��ʽ���ܱ�����Ϊ���ܱ��Edit����λ����������File����0��ʼ����1��������������ʾ��Edit��ͻ��ʽ���ܱ�����ôlParamӦ�õ��1��

Ϊ��ȷ���Ƿ����á�Undo��ѡ�POPPAD2���༭�������һ��EM_CANUNDOѶϢ������༭�������ܹ�ִ�С�Undo����������ôSendMessage���д��ط���ֵ������������£�ѡ����ã�����ѡ����Ч����

EnableMenuItem (wParam, IDM_UNDO,
     	SendMessage (hwndEdit, EM_CANUNDO, 0, 0) ?
                  					MF_ENABLED : MF_GRAYED) ;

ֻ�е��������а�������ʱ����Paste��ѡ����ܹ������á����ǿ���ʹ��CF_TEXTʶ����ͨ��IsClipboardFormatAvailable������ȷ����һ�㣺

EnableMenuItem (wParam, IDM_PASTE,
     	IsClipboardFormatAvailable (CF_TEXT) ? MF_ENABLED : MF_GRAYED) ;

ֻ��ѡ���˱༭�������е����֣���Cut������Copy���͡�Delete��ѡ����ܹ������á����༭�������һ��EM_GETSELѶϢ�������ذ�������Ѷ��������

iSelect = SendMessage (hwndEdit, EM_GETSEL, 0, 0) ;

iSelect�ĵ�λԪ���ǵ�һ����ѡ����Ԫ��λ�ã�iSelect�ĸ���������һ����ѡ����Ԫ��λ�á��������������ȣ����ʾû��ѡ�����֣�

if (HIWORD (iSelect) == LOWORD (iSelect))
     	iEnable = MF_GRAYED ;
else
     	iEnable = MF_ENABLED ;

Ȼ����Խ�iEnable��ֵ��춡�Cut������Copy���͡�Delete��ѡ�

EnableMenuItem (wParam, IDM_CUT,  iEnable) ;
EnableMenuItem (wParam, IDM_COPY, iEnable) ;
EnableMenuItem (wParam, IDM_DEL,  iEnable) ;

�������ܱ���
��

��Ȼ�����POPPAD2��ʽ��ʹ�����Ӵ��༭�������ô���ǽ�����һЩ���⣬���漰�����ɡ�Edit�����ܱ��еġ�Undo������Cut������Copy������Paste������Clear���͡�Select All��ѡ����DZ༭������ʹ�����ִ���������ף���Ϊ���ÿһ��ѡ������ֻ����༭�������һ��ѶϢ���ɣ�

case 	IDM_UNDO :
     	SendMessage (hwndEdit, WM_UNDO, 0, 0) ;
     	return 0 ;

case 	IDM_CUT :
     	SendMessage (hwndEdit, WM_CUT, 0, 0) ;
     	return 0 ;

case 	IDM_COPY :
     	SendMessage (hwndEdit, WM_COPY, 0, 0) ;
     	return 0 ;
 
case 	IDM_PASTE :
     	SendMessage (hwndEdit, WM_PASTE, 0, 0) ;
     	return 0 ;

case 	IDM_DEL :
     	SendMessage (hwndEdit, WM_DEL, 0, 0) ;
     	return 0 ;
case 	IDM_SELALL :
     	SendMessage (hwndEdit, EM_SETSEL, 0, -1) ;
     	return 0 ;

ע�⣬���ǿ��Ը���һ������Щ������ֻҪʹIDM_UNDO��IDM_CUT�ȵȵ�ֵ������Ӧ���Ӵ�ѶϢWM_UNDO��WM_CUT��ֵ��

��File��ͻ��ʽ���ܱ��ϵġ�About��ѡ������һ���򵥵�ѶϢ���飺

case 	IDM_ABOUT :
     	MessageBox (hwnd, TEXT ("POPPAD2 (c) Charles Petzold, 1998"),
                 					szAppName, MB_OK | MB_ICONINFORMATION) ;
     	return 0 ;

����һ���У����ǽ��������һ���Ի����顣�����ӹ��ܱ���ѡ��Help��ѡ����߰���F1���ټ�ʱ��ͬ����������һ��ѶϢ���顣

��Exit��ѡ�����Ӵ�ѶϢ������ʽ����һ��WM_CLOSEѶϢ��

case 	IDM_EXIT :
     	SendMessage (hwnd, WM_CLOSE, 0, 0) ;
     	return 0 ;

������DefWindowProc�յ�һ��wParam���SC_CLOSE��WM_SYSCOMMANDѶϢʱ����ɵĹ�����

��ǰ�����Щ��ʽ�У�����û�����Ӵ�ѶϢ������ʽ�д���WM_CLOSEѶϢ����ֻ�Ǽ򵥵ذ����͸�DefWindowProc��DefWindowProc��WM_CLOSE�Ĵ����dz��򵥣�����DestroyWindow��ʽ�����Բ���WM_CLOSEѶϢ�͸�DefWindowProc������POPPAD2���������������ʵ��ĿǰΪֹ������Ҫ�������ڵ�ʮһ���е�POPPAD���������༭����ʱ�����ͱ�÷dz���Ҫ�ˡ�

case WM_CLOSE :
     	if (IDYES == AskConfirmation (hwnd))
          		DestroyWindow (hwnd) ;
  	return 0 ;

AskConfirmation��POPPAD2�е�һ����ʽ������ʾһ������ȷ�Ϲرճ�ʽ��ѶϢ���飺

AskConfirmation (HWND hwnd)
{
     	return MessageBox (hwnd, TEXT ("Really want to close Poppad2?"),
                        	szAppName, MB_YESNO | MB_ICONQUESTION) ;
}

���ѡ����Yes��ť�Ļ���ѶϢ���飨�Լ�AskConfirmation��ʽ��������IDYES��ֻ����������ʽ�Ż����DestroyWindow�����򣬳�ʽ���������

���Ҫ�ڳ�ʽ����֮ǰȷ��ʹ�������Ҫ������ʽ����ô�������봦��WM_QUERYENDSESSIONѶϢ����ʹ����Ҫ�ر�Windowsʱ��Windows��ʼ��ÿ���Ӵ�ѶϢ������ʽ����һ��WM_QUERYENDSESSIONѶϢ��������κ�һ���Ӵ�ѶϢ������ʽ�������ѶϢ�ᴫ��0����ôWindows������������������´�����WM_QUERYENDSESSION��

case 	WM_QUERYENDSESSION :
     	if (IDYES == AskConfirmation (hwnd))
          		return 1 ;
     	else
          		return 0 ;

���Ҫ�ڳ�ʽ����֮ǰҪ��ʹ���ߵ�ȷ�ϣ����봦��WM_CLOSE��WM_QUERYENDSESSION������ѶϢ�������Ϊʲô����ʹPOPPAD2�еġ�Exit�����ܱ�ѡ��ֻ���Ӵ�ѶϢ������ʽ����һ��WM_CLOSEѶϢ��ԭ�������������DZ������ڱ𴦽�������ȷ�ϵĶ�����

���Ҫ����WM_QUERYENDSESSIONѶϢ����ô��Ҳ�������WM_ENDSESSIONѶϢ����Ȥ��Windows�����ѶϢ���͸���ǰ�յ�WM_QUERYENDSESSIONѶϢ��ÿ���Ӵ�ѶϢ������ʽ����������һ����ʽ��WM_QUERYENDSESSION������0�����ܽ���Windows��ִ�У���ôWM_ENDSESSION��wParam����Ϊ0��WM_ENDSESSIONѶϢʵ���ϻش���������⣺�Ҹ��߹�Windows���԰��ҽ���������������ı�����������

������POPPAD2�ġ�File�����ܱ����Ҽ����˳����ġ�New������Open������Save���͡�Save As��ѡ������������ڲ��������á�Ҫ������Щ���������Ҫʹ�öԻ����顣���������۶Ի������ʱ����Ҳ����׼��ѧϰ���ǵ�ʱ���ˡ�