windows下窗口的创建

平台:visual studio 2013 win32

语言:C++

运行结果:


代码:

#include<windows.h>

#define IDC_MAIN_BUTTON 101
#define IDC_MAIN_EDIT 102
HWND hEdit;

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpComLine, int nShowCmd){
	WNDCLASSEX wClass;
	ZeroMemory(&wClass, sizeof(WNDCLASSEX));
	wClass.cbClsExtra = NULL;
	wClass.cbSize = sizeof(WNDCLASSEX);
	wClass.cbWndExtra = NULL;
	wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wClass.hIcon = NULL;
	wClass.hIconSm = NULL;
	wClass.hInstance = hInst;
	wClass.lpfnWndProc = (WNDPROC)WinProc;
	wClass.lpszClassName = "Windows Class";
	wClass.lpszMenuName = NULL;
	wClass.style = CS_HREDRAW | CS_VREDRAW;

	if (!RegisterClassEx(&wClass)){
		int nResult = GetLastError();
		MessageBox(NULL,
			"对不起,窗体注册失败",
			"窗体类错误",
			MB_ICONERROR);
	}

	HWND hWnd = CreateWindowEx(NULL,
		"Windows Class",
		"为窗体添加一个控件...",
		WS_OVERLAPPEDWINDOW,
		200,
		200,
		400,
		400,
		NULL,
		NULL,
		hInst,
		NULL);

	ShowWindow(hWnd, nShowCmd);

	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (GetMessage(&msg, NULL, 0, 0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
	switch (msg){
		case WM_CREATE:{
						   hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
							   "EDIT",
							   "",
							   WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
							   30,
							   30,
							   200,
							   100,
							   hWnd,
							   (HMENU)IDC_MAIN_EDIT,
							   GetModuleHandle(NULL),
							   NULL);

						   HGDIOBJ hfDefault = GetStockObject(DEFAULT_GUI_FONT);
						   SendMessage(hEdit,
							   WM_SETFONT,
							   (WPARAM)hfDefault,
							   MAKELPARAM(false, 0));
						   SendMessage(hEdit,
							   WM_SETTEXT,
							   NULL,
							   (LPARAM)"用户在这里输入文本并编辑...");

						   HWND hWndButton = CreateWindowEx(NULL,
							   "BUTTON",
							   "确定",
							   WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
							   50,
							   150,
							   100,
							   24,
							   hWnd,
							   (HMENU)IDC_MAIN_BUTTON,
							   GetModuleHandle(NULL),
							   NULL);
						   SendMessage(hWndButton,
							   WM_SETFONT,
							   (WPARAM)hfDefault,
							   MAKELPARAM(FALSE, 0));
		}

		break;

		case WM_COMMAND:
			switch (LOWORD(wParam)){
				case IDC_MAIN_BUTTON:{
					char buffer[256];
					SendMessage(hEdit,
						WM_GETTEXT,
						sizeof(buffer) / sizeof(buffer[0]),
						reinterpret_cast<LPARAM>(buffer));
					MessageBox(NULL,
						buffer,
						"信息提示",
						MB_ICONINFORMATION);
				}
				break;
			}
			break;

		case WM_DESTROY:{
			PostQuitMessage(0);
			return 0;

		}
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值