欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WIN32 基础控件的使用

程序员文章站 2024-03-01 11:59:04
...

代码模板

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPWSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	//弹出弹窗
	DialogBoxA(hInstance, MAKEINTRESOURCEA(IDD_DIALOG1), NULL, Dlgproc);
	return 0;
}

复选框

WIN32 基础控件的使用

代码寓意:

我们点击显示之后会弹出复选框是否选中

过程函数:

INT_PTR  CALLBACK Dlgproc(
	HWND hwind,
	UINT type,
	WPARAM wPARAM,
	LPARAM lPARAM
) {
	if (type == WM_CLOSE)
	{
		EndDialog(hwind, 0);
		return TRUE;
	}
	if (type == WM_COMMAND)
	{
		WORD wcontrolID = LOWORD(wPARAM);//控件id
		WORD wNotifyID = HIWORD(wPARAM);//控件触发消息类型
		HWND controlWND = HWND(lPARAM);//控件窗口句柄
		if (wNotifyID == BN_CLICKED && wcontrolID == IDC_BUTTON_SHOW_RESULT)
		{
			//LRESULT runingResult = SendDlgItemMessageA(hwind, IDC_FAVORITE_RUNING, BM_GETCHECK, 0, 0);
			//上面代码等于下面的两行
			HWND runningHWND = GetDlgItem(hwind, IDC_FAVORITE_RUNING);
			LRESULT runingResult = SendMessageA(runningHWND, BM_GETCHECK, 0, 0);

			char outStr[100] = {};

			wsprintfA(outStr, "选中 %d", runingResult);

			MessageBoxA(hwind, outStr, "Hello world", MB_OK);
		}
		return TRUE;
	}


	return FALSE;
}

BM_GETCHECK message 文档

单选框

WIN32 基础控件的使用
单选框需要放入RadioBox中才能标识是哪一组单选框。(不然存在多组单选框无法区分)

过程函数:


INT_PTR  CALLBACK Dlgproc(
	HWND hwind,
	UINT type,
	WPARAM wPARAM,
	LPARAM lPARAM
) {
	if (type == WM_CLOSE)
	{
		EndDialog(hwind, 0);
		return TRUE;
	}
	if (type == WM_COMMAND)
	{
		WORD wcontrolID = LOWORD(wPARAM);//控件id
		WORD wNotifyID = HIWORD(wPARAM);//控件触发消息类型
		HWND controlWND = HWND(lPARAM);//控件窗口句柄
		if (wNotifyID == BN_CLICKED && wcontrolID == IDC_BUTTON_SHOW_RESULT)
		{
			
			LRESULT femaleResult = SendDlgItemMessageA(hwind, IDC_RADIO_FEMALE, BM_GETCHECK, 0, 0);
			LRESULT maleResult = SendDlgItemMessageA(hwind, IDC_RADIO2_MALE, BM_GETCHECK, 0, 0);

			char outStr[100] = {}; 

			wsprintfA(outStr, "选中 %s", femaleResult == BST_CHECKED?"女":"男");

			MessageBoxA(hwind, outStr, "Hello world", MB_OK);
		}
		return TRUE;
	}


	return FALSE;
}

相关标签: WIN32