#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
2. C++ 最简单的窗口程序模板(使用Win32 API):
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
// 注册窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"BasicWindow";
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0,
L"BasicWindow",
L"我的窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nCmdShow);
// 消息循环
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
第二个模板是最简单的Windows窗口程序,使用Win32 API创建一个基本窗口。这个模板会创建一个800x600像素的标准窗口,包含最小化、最大化和关闭按钮。