#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_VREDRAW|CS_HREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hInstance=hInstance;
//第一个参数不能是NULL,否则会出一些意想不到结果
wndclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.lpszClassName=szAppName;
//这个地方也不可以是NULL,不然不会有上面的菜单
wndclass.lpszMenuName=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))
{
/*TranslateAccelerator函数确定保存在msg消息结构体中的消息是否是键盘消息。
如果是,该函数在加速键表中寻找句柄为hAccel的匹配值。如果找到匹配值,它会调用句柄为hwnd的
窗口过程,就是AskConfirmation(HWND hwnd)。如果键盘加速键ID对应系统菜单的一个菜单项,
则相应消息为WM_SYSCOMMAND;否则为WM_COMMAND。
当TranslateAccelerator返回时,如果消息被翻译过(并且已被发送给窗口过程),则返回值为非零值,否则返回零*/
if(!TranslateAccelerator(hwnd,hAccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
int 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:
/*WS_CHILD子窗口;WS_VISIBLE显示;WS_HSCROLL、WS_VSCROLL两个滚动条;WS_BORDER有边界;ES_LEFT左对齐;ES_MULILINE多行
ES_AUTOHSCROLL、ES_AUTOVSCROLL自动滚动条*/
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;
//应该是当下拉菜单变为active前(可以理解为是弹出前)触发。
case WM_INITMENUPOPUP:
//按本书意思应该是说,当菜单Edit要显示时,lParam为1
if(lParam==1)
{
//SendMessage确定返回值是否能够撤销,可以的话,就返回TRUE
EnableMenuItem((HMENU)wParam,IDM_EDIT_UNDO,SendMessage(hwndEdit,EM_CANUNDO,0,0)?MF_ENABLED:MF_GRAYED);
//IsClipboardFormatAvailable判断剪贴版中是否有相应的数据,有则返回非0
EnableMenuItem((HMENU)wParam,IDM_EDIT_PASTE,IsClipboardFormatAvailable(CF_TEXT)?MF_ENABLED:MF_GRAYED);
//获取选定的
iSelect=SendMessage(hwndEdit,EM_GETSEL,0,0);
//iSelect的低位是第一个被选中的位置,高位字是紧随选中文本后面的第一个字符的位置,如果相等,表明没有文本被选中
//这个地方第一次接触
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:
//判断这个WM_COMMAND消息是由哪个地方产生的,菜单的话,lParam为0;
//子窗口,按键为1
if(lParam)
{
//字符数量太多,超出范围
if(LOWORD(wParam)==ID_EDIT&&(HIWORD(wParam==EN_ERRSPACE)))
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,2012"),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);
}
这次代码是所有的代码,留给自己以后看吧,里面一些东西,很有用的。