以下代码涉及到了很多消息的处理,有些部分注释掉了,主要看代码
#include <windows.h>
#include<tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <string>
#define IDM_OPEN 102
/*鼠标消息
* 键盘消息
* Onkeydown
* Onkeyup
* //键盘扫描码
* /lParam>>16&0ff
快捷键消息
菜单消息
控件消息
自定义消息
窗口消息
客户区域的概念(Client Aera)
非客户区*/
using namespace std;
string g_Text;
TEXTMETRIC g_tm;//字体信息
VOID showerrormassage()
{LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |/* 分别为FORMAT_MESSAGE_ALLOCATE_BUFFER由函数分配输出缓冲区,FORMAT_MESSAGE_FROM_SYSTEM表示程序将会在系统消息表资源中搜索所需消息,FORMAT_MESSAGE_IGNORE_INSERTS程序将会忽略搜索到消息中的插入序列。 */FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0, NULL);MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION);LocalFree(lpMsgBuf);
}//热键消息
//热键消息需要注册
//RegisterHotKey()
//软件卸载的时候还要卸载这个注册
//所以ip号就是这么回事
//
//UnregisterHotKey注销的函数//LRESULT OnHotkey(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
// return TRUE;
//}LRESULT Onchar(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{TCHAR szBuf[MAXBYTE];if ((char)wParam == '\r'){g_Text += (char)wParam;g_Text += '\n';}else if ((char)wParam == '\b'){if (!g_Text.empty()) {g_Text.pop_back();}}else{g_Text += (char)wParam;}wsprintf(szBuf, _T("Onchar %s\n"), g_Text.data());OutputDebugString(szBuf);//获取窗口HDC,非客户区GetWindowDC/*HDC hdc = GetDC(hwnd);*//*TextOut(hdc, 0, 0, g_Text.data(), g_Text.length());*///这个API不支持回车//释放DC//RECT rc;//GetClientRect(hwnd, &rc);创建白色的刷子//HGDIOBJ hBrushOld;//HBRUSH hBrush= CreateSolidBrush(RGB(255,255,255));选择刷子//hBrushOld= SelectObject(hdc, hBrush);绘制背景//FillRect(hdc, &rc, hBrush);绘制文本//DrawText(hdc, g_Text.data(), g_Text.length(), &rc, DT_LEFT);还原默认刷子//SelectObject(hdc, hBrushOld);释放刷子//DeleteObject(hBrush);释放DC//ReleaseDC(hwnd, hdc);//ShowCaret(hwnd);//SetCaretPos(g_tm.tmAveCharWidth * g_Text.length(), 0);RECT rc;GetClientRect(hwnd, &rc);InvalidateRect(hwnd, NULL, TRUE);//产生WM_PAINT return TRUE;
}
LRESULT OnCreate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {//初始化/* MessageBox(NULL,_T("onCrate"), _T("asm"), MB_OK);*/OutputDebugString(_T("[11syy]WM_CREATE\n"));HDC hdc = GetDC(hwnd);SelectObject(hdc,GetStockObject(SYSTEM_FIXED_FONT));GetTextMetrics(hdc, &g_tm);ReleaseDC(hwnd, hdc);return TRUE;
}//消息处理LRESULT OnClese(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {/*MessageBox(NULL, _T("onClose"), _T("asm"), MB_OK);*/OutputDebugString(_T("[11syy]WM_ClOSE\n"));DestroyWindow(hwnd);return TRUE;
}
//LRESULT OnKeydown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//{
// //获取键盘状态
// BYTE KeyState[256];
// if (!GetKeyboardState(KeyState))
// {
// return TRUE;
// }
//
// //键盘扫描码
// BYTE SanCode = (UINT)lParam >> 16 & 0xff;
// WORD ch;
// ToAscii(wParam, SanCode, KeyState, &ch, 0);
// TCHAR szBuf[MAXBYTE];
// wsprintf(szBuf, _T("[asm] OnKeydown) % c\n"), ch);
// OutputDebugString(szBuf);
// return TRUE;
//}//消息处理LRESULT OnDestroy(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{/* MessageBox(NULL, _T("onDestory"), _T("asm"), MB_OK);*/PostMessage(hwnd, WM_QUIT, 0, NULL);OutputDebugString(_T("[11syy]WM_DESTROY\n"));return TRUE;
}LRESULT OnSetFocus(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{OutputDebugString(_T("[11syy]WM_SETFOCUS\n"));//创建插入符CreateCaret(hwnd, (HBITMAP)NULL,1,g_tm.tmHeight);SetCaretPos(0, 0);ShowCaret(hwnd);return TRUE;
}
LRESULT OnKillFocus(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{OutputDebugString(_T("[11syy] WM_KILLFOCUS\n"));return TRUE;
}LRESULT OnEraseBackgroud(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{OutputDebugString(_T("[11syy]OnEraseBackgroud \n"));return FALSE;
}LRESULT OnPaint(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{OutputDebugString(_T("[11syy]OnPaint \n"));//HDC hdc=GetDC(hwnd);不受无效区域的影响PAINTSTRUCT ps;HDC hdc = BeginPaint(hwnd, &ps);RECT rc;GetClientRect(hwnd, &rc);DrawText(hdc, g_Text.data(), g_Text.length(), &rc, DT_LEFT);/*ReleaseDC(hwnd, hdc);*///wpaint消息一直会来,因为操作系统会一直认为他是无效区域//所以你得告诉操作系统,我们画过的地方应该为有效区域//ValidateRect(hwnd,&rc);EndPaint(hwnd, &ps);//自动将无效区域设置为有效区域return TRUE;
}
LRESULT OnCommand(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{OutputDebugString(_T("[11syy]Oncommnd \n"));WORD WID = LOWORD(wParam);switch (WID){case 102://这里定义宏IDM_OPENMessageBox(NULL, "打开", "asm", MB_OK);break;}return TRUE;
}//LRESULT OnMove(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//{
// TCHAR szBuf[MAXBYTE];
// int xpos = (int)(short)LOWORD(lParam);
// int ypos = (int)(short)HIWORD(lParam);
// wsprintf(szBuf, _T("[11syy]xpos:%d ypos:%d"), xpos, ypos);
// PostMessage(hwnd, WM_QUIT, 0, NULL);
// return TRUE;
//}
//LRESULT OnLButtonnDown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//{
// int xpos = LOWORD(lParam);
// int ypos = HIWORD(lParam);
// TCHAR szBuf[MAXBYTE];
// wsprintf(szBuf, _T("[11syy]xpos:%d ypos:%d\n"), xpos, ypos);
// OutputDebugString(szBuf);
// /*MessageBox(NULL, szBuf, _T("asm"), MB_OK);*/
// return FALSE;
//}
//LRESULT OnLButtonnup(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//{
// int xpos = LOWORD(lParam);
// int ypos = HIWORD(lParam);
// TCHAR szBuf[MAXBYTE];
// wsprintf(szBuf, _T("[11syy]xpos:%d ypos:%d\n"), xpos, ypos);
// OutputDebugString(szBuf);
// //MessageBox(NULL, szBuf, _T("asm"), MB_OK);
// return FALSE;
//}//LRESULT onMouse(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//{
// LRESULT lResult = FALSE;
// switch (uMsg)
// {
// case WM_LBUTTONDOWN:
// lResult= OnLButtonnDown(hwnd, uMsg, wParam, lParam);
// break;
// }
// return FALSE;
//}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {LRESULT lResult = FALSE;switch (uMsg){case WM_CREATE:lResult = OnCreate(hwnd, uMsg, wParam, lParam);break;case WM_CLOSE:lResult = OnClese(hwnd, uMsg, wParam, lParam);break;case WM_DESTROY:lResult = OnDestroy(hwnd, uMsg, wParam, lParam);break;case WM_CHAR:lResult= Onchar(hwnd, uMsg, wParam, lParam);break;case WM_SETFOCUS:lResult = OnSetFocus(hwnd, uMsg, wParam, lParam);break;case WM_KILLFOCUS:lResult = OnKillFocus(hwnd, uMsg, wParam, lParam);break;case WM_ERASEBKGND://刷背景lResult = OnEraseBackgroud(hwnd, uMsg, wParam, lParam);break;case WM_PAINT://绘制消息lResult = OnPaint(hwnd, uMsg, wParam, lParam);break;case WM_COMMAND:lResult = OnCommand(hwnd, uMsg, wParam, lParam);break;}//无效区域if (!lResult) {return DefWindowProc(hwnd, uMsg, wParam, lParam);//默认窗口过程处理}return lResult;
}//图形界面,窗口int WINAPI _tWinMain(HINSTANCE hInstance,//应用程序示例句柄HINSTANCE hPrevInstance,//保留TCHAR* lpCmdline, //命令行参数,LPSTR可能会变成Unicodeint nCmdShow) {//发送消息//SendMessage();//直接调用窗口过程函数同步//PostMessage();//投递消息到消息队列去//HWND hNotepad = FindWindow("Notepad", NULL);//if (hNotepad == NULL)//{// return FALSE;//}//HWND hEdit = GetWindow(hNotepad, GW_CHILD);//HWND hEditchild = GetWindow(hEdit, GW_CHILD);//PostMessage(hEditchild, WM_KEYDOWN, 'A', 0);//HDC hdc = GetDC(hEditchild);//while (true)//{// TextOut(hdc, 0, 0, "SB", 2);//}//ReleaseDC(hEdit, hdc);////窗口显示方式//比如我们启动这个窗口,最大化,最小化/*MessageBoxA(NULL, "hell word ", "asm", MB_YESNO);*/\//int res = MessageBoxW(NULL, L"hell unicode", L"asm", MB_YESNO);////if (res == 0) {///* MessageBoxW(NULL, L"错误", L"asm", MB_OK);*/// showerrormassage();// return 0;//}程序》实例化》进程》多个窗口//1.注册窗口TCHAR szWndclassName[] = { _T("chongmousyy") };WNDCLASSEX wc = { 0 };wc.cbSize = sizeof(WNDCLASSEX);wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;//窗口类型wc.lpfnWndProc = WindowProc;wc.hInstance = hInstance;wc.hIcon = LoadIcon(NULL, IDC_HAND);//图标wc.hCursor = LoadCursor(NULL, IDC_ARROW);//光标FDXXwc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));//窗口背景颜色刷子wc.lpszClassName = szWndclassName;//窗口类名,窗口名字不可以为空wc.lpszMenuName = NULL;//窗口菜单if (RegisterClassEx(&wc) == 0){showerrormassage();return 0;};//2.创建窗口TCHAR szWndName[] = { _T("翀某人") };HWND hwnd = CreateWindowEx(0,szWndclassName,szWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);if (hwnd == 0){showerrormassage();return 0;}HMENU hMenu = CreateMenu();//弹出菜单AppendMenu(hMenu, MF_STRING|MF_POPUP,(UINT_PTR)hMenu,"文件(&f)");AppendMenu(hMenu, MF_STRING|MF_POPUP, (UINT_PTR)hMenu, "编辑(&E)");//添加子菜单HMENU hSubMenu = GetSubMenu(hMenu, 0);AppendMenu(hSubMenu, MF_STRING, IDM_OPEN, "打开(&O)");SetMenu(hwnd, hMenu);RECT rc;GetClientRect(hwnd, &rc);//空键 按钮//HWND hEdit = CreateWindowEx(0,// "EDIT",// NULL,// WS_CHILD | WS_VISIBLE|WS_VSCROLL|ES_MULTILINE,// 0,// 0,// rc.right - rc.left,// rc.bottom - rc.top,// hwnd,// NULL,// hInstance,// NULL//);//3.显示跟新窗口ShowWindow(hwnd, SW_SHOWNORMAL);UpdateWindow(hwnd);//4.消息循环(消息队列)BOOL bRET;MSG msg;while ((bRET = GetMessage(&msg, NULL, 0, 0)) != 0) {if (bRET == -1) {break;}else{//转发消息TranslateMessage(&msg);//转换键盘消息为字符消息DispatchMessage(&msg);//派发消息}}//5.消息处理//资源return 0;
}