C++窗体windows.h--2

在简单的窗体基础上加了按钮

加了Button的代码

#include <windows.h>

int num = 1;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//button
	switch (message) {
	case WM_CREATE: {
		CreateWindow(
			TEXT("button"), TEXT("按钮"),
			WS_CHILD | WS_VISIBLE,
			0, 0, 100, 50,
			hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
		num = 1;
	}
	case WM_COMMAND: {
		if (num == 0)MessageBox(NULL, "点击成功1", "", MB_OK);
		num = 0;
		break;
	}
	case WM_DESTROY: {
		PostQuitMessage(0);
		break;
	}
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
}

BOOL RegisterWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wnd;
	wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wnd.lpfnWndProc = WndProc;
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	wnd.lpszClassName = TEXT("WindowClass");
	wnd.hInstance = hInstance;
	wnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wnd.cbSize = sizeof(WNDCLASSEX);
	wnd.lpszMenuName = nullptr;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	return RegisterClassEx(&wnd);
}
BOOL DisplayWindow(HWND& hwnd, HINSTANCE hInstance, HMENU hMenu, int nCmdShow)
{
	hwnd = CreateWindow(TEXT("WindowClass"), TEXT("TweeChaice"), WS_OVERLAPPEDWINDOW, 0, 0, 400, 300, nullptr, nullptr, hInstance, nullptr);
	ShowWindow(hwnd, nCmdShow);
	return hwnd ? TRUE : FALSE;
}

HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!RegisterWindow(hInstance))
	{
		MessageBox(nullptr, TEXT("Error register!"), TEXT("Error"), MB_ICONERROR | MB_OK);
		return -1;
	}
	if (!DisplayWindow(hwnd, hInstance, nullptr, nCmdShow))
	{
		MessageBox(nullptr, TEXT("Error creating window!"), TEXT("Error"), MB_ICONERROR | MB_OK);
		return -1;
	}
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)>0){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

还有另一个简化的代码

#include <windows.h>
int num = 1;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch (Message) {
	case WM_CREATE: {
			CreateWindow(
				TEXT("button"),TEXT("按钮"),
				WS_CHILD | WS_VISIBLE,
				0, 0,100, 50,
				hwnd,(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
		num = 1;
	}
	case WM_COMMAND: {
		if (num == 0)MessageBox(NULL, "点击成功1", "", MB_OK);
		num = 0;
		break;
	}
	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", "Button Style", 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;
}

button的相关内容资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用以下代码来创建一个标题为"Form"的空白窗体: ```c++ #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Register the window class const char CLASS_NAME[] = "My Window Class"; WNDCLASS wc = { }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window HWND hwnd = CreateWindowEx( 0, // Optional window styles CLASS_NAME, // Window class "Form", // Window text WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data ); if (hwnd == NULL) { return 0; } // Show the window ShowWindow(hwnd, nCmdShow); // Run the message loop MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ``` 这段代码将创建一个名为"My Window Class"的窗口类,并使用CreateWindowEx函数创建一个窗口,然后ShowWindow函数将窗口显示在屏幕上。在WndProc函数中,我们定义了窗口的行为。在本例中,当用户关闭窗口时,我们使用PostQuitMessage函数向消息队列发送一个退出消息,以结束应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只秋暝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值