一定要的:
#include<windows.h>
剩下的:
过程:
LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam){ switch(Message){ case WM_CREATE:{ /*略*/ //HWND 名称=CreateWindow(类型,显示的字,WS_CHILD|WS_VISIBLE,x,y,宽,高,hwnd,(HMENU)编号,hInst,NULL); /* 类型有: "button" "static" "COMBOBOX" "EDIT" 分别为: 按钮 文本 编辑框 无边框编辑框 */ } case WM_COMMAND:{ switch(wParam){ /*略*/ /*case 编号:{ . . . break; }*/ (表示如果点击编号则执行) } } default:{ DefWindowProc(hwnd,Message,wParam,lParam); break; } } }
创建窗口:
HINSTANCE hInstance; HINSTANCE hPrevInstance; LPSTR lpCmdLine; int nCmdShow; hInst=hInstance; WNDCLASSEX wc; HWND hwnd; MSG msg; memset(&wc,0,sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "窗口注册失败!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","窗口名称",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, /* x */ CW_USEDEFAULT, /* y */ 320, /* 宽 */ 200, /* 高 */ NULL,NULL,hInstance,NULL); if(hwnd == NULL) { MessageBox(NULL, "窗口创建失败!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; }
消息循环:
while(GetMessage(&msg, NULL, 0, 0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); }
示例代码:
#include<windows.h> #include<bits/stdc++.h> using namespace std; int shuai=3; HINSTANCE hInst; LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam){ switch(Message){ case WM_CREATE:{ HWND MBOK=CreateWindow("button","提交",WS_CHILD|WS_VISIBLE, 10,60,75,20,hwnd,(HMENU)1,hInst,NULL); HWND MBNO=CreateWindow("button","退出",WS_CHILD|WS_VISIBLE, 95,60,75,20,hwnd,(HMENU)2,hInst,NULL); HWND tishi=CreateWindow("static","作者帅不帅?",WS_CHILD|WS_VISIBLE, 10,0,300,30,hwnd,(HMENU)3,hInst,NULL); HWND xuan1=CreateWindow("button","帅",WS_CHILD|WS_VISIBLE, 10,20,130,30,hwnd,(HMENU)4,hInst,NULL); HWND xuan2=CreateWindow("button","不帅",WS_CHILD|WS_VISIBLE, 160,20,130,30,hwnd,(HMENU)5,hInst,NULL); break; } case WM_COMMAND:{ switch(wParam){ case 1:{ if(shuai==1){ MessageBox(NULL, "Yes!","提示",MB_OK); exit(0); }else if(shuai==0){ MessageBox(NULL, "不能撒谎!","Error!",MB_ICONEXCLAMATION|MB_OK); }else{ MessageBox(NULL, "请先回答!","Error!",MB_ICONEXCLAMATION|MB_OK); } break; } case 2:{ exit(0); break; } case 4:{ shuai=1; break; } case 5:{ shuai=0; break; } } break; } default:{ DefWindowProc(hwnd,Message,wParam,lParam); break; } } } int main(){ HINSTANCE hInstance; HINSTANCE hPrevInstance; LPSTR lpCmdLine; int nCmdShow; hInst=hInstance; WNDCLASSEX wc; HWND hwnd; MSG msg; memset(&wc,0,sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszClassName = "WindowClass"; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "窗口注册失败!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","问题",WS_VISIBLE|WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, /* x */ CW_USEDEFAULT, /* y */ 320, /* 宽 */ 135, /* 高 */ NULL,NULL,hInstance,NULL); if(hwnd == NULL) { MessageBox(NULL, "窗口创建失败!","Error!",MB_ICONEXCLAMATION|MB_OK); return 0; } while(GetMessage(&msg, NULL, 0, 0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }