求 C++ WindowsAPI GUI 教程
  • 板块灌水区
  • 楼主_Constructer_
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/19 21:21
  • 上次更新2024/12/20 13:13:18
查看原帖
求 C++ WindowsAPI GUI 教程
1439381
_Constructer_楼主2024/12/19 21:21

rt,附我写的一个随机 shutdown GUI 程序,需要在编译选项内加入 -std=c++14 -mwindows。(有 shutdown /s /t 0,请勿随意尝试)

#include <windows.h>
#include <time.h>
using namespace std;

LRESULT CALLBACK WinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) {
	HWND Button,Button2;
	switch(uMsg) {
		case WM_CREATE:
			srand(time(nullptr));
			Button = CreateWindow("Button","Button",
			WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,35,10,120,60,
			hwnd,(HMENU)1,(HINSTANCE)hwnd,NULL);
			Button = CreateWindow("Button","Button2",
			WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,35,85,120,60,
			hwnd,(HMENU)2,(HINSTANCE)hwnd,NULL);
			break;
		case WM_COMMAND:
			switch(wParam) {
				case 1:
					if(rand()%2 == 0)
						MessageBox(hwnd,"You Press The Button!","Button Hitted",MB_OK);
					else {
						MessageBox(hwnd,"Your Computer Will Be Fucked By Me!","You Will Die!!!",MB_OK);
						system("shutdown /s /t 0");
					} break;
				case 2:
					if(rand()%2 == 0)
						MessageBox(hwnd,"You Press The Button!","Button Hitted",MB_OK);
					else {
						MessageBox(hwnd,"Your Computer Will Be Fucked By Me!","You Will Die!!!",MB_OK);
						system("shutdown /s /t 0");
					} break;
				default: break;
			}; break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hwnd,uMsg,wParam,lParam);
	} return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) {
	WNDCLASS wndCls;
	wndCls.cbClsExtra = 0;
	wndCls.cbWndExtra = 0;
	wndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndCls.hCursor = LoadCursor(NULL,IDC_CROSS);
	wndCls.hIcon = LoadIcon(NULL,IDI_ERROR);
	wndCls.hInstance = hInstance;
	wndCls.lpfnWndProc = WinProc;
	wndCls.lpszClassName = "test";
	wndCls.lpszMenuName = NULL;
	wndCls.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndCls);
	
	HWND hwnd = CreateWindow("test","Russia Bomb",
	WS_OVERLAPPEDWINDOW,800,300,200,210,
	NULL,NULL,hInstance,NULL);

	ShowWindow(hwnd,SW_SHOWNORMAL);
	UpdateWindow(hwnd);
	
	MSG msg;
	while(GetMessage(&msg,NULL,0,0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	} return 0;
}
2024/12/19 21:21
加载中...