在Dev-c++里 有创建项目的选项
选择第一个(就是windows窗口) 它会有初始代码
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
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, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
按钮的代码是
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HWND button1;
const int IDbutton1 = 1;
switch(Message) {
case WM_CREATE: {
button1 = CreateWindow ( TEXT("button"), TEXT("Command Button"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0,
50,50,
hwnd, (HMENU)IDbutton1, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
就是这样 加在前几行代码相同的那行里
文本框也一样(窗口风格在C++ 用DEV-C++建一个Windows窗口程序带文本框和命令按钮里有详细的)
哦,对了 前代码
#include <windows.h>
#include <string>
#include <cstring>
注:要把后面
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
删掉 不然会闪退
|