Win32中的复选框问题 [英] Problem with check box in Win32

查看:151
本文介绍了Win32中的复选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在visual studio 2015中使用win32 API创建了一个复选框。

我的复选框已成功创建,没有任何错误。



但是这不执行所需的任务。



我的对话框包含两个按钮确定并取消。我愿意在选中对话框时添加一个函数调用,当按下OK按钮时,除正常函数外还应执行该函数



 INT_PTR CALLBACK StoreHere(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (消息)
{
// ------------------------------------------- ---

// --------- -------------------------------------
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam)= = IDOK || LOWORD(wParam)== IDCANCEL)
{
EndDialog(hDlg,LOWORD(wParam));
return (INT_PTR)TRUE;
}
break ;
if (IDOK == Optimizer(hDlg,message,wParam,lParam)){
case 1008 :{ // 案例1008是复选框1的ID
bool checked = IsDlgButtonChecked(hDlg,IDC_CHECK1);
if (选中){
CheckDlgButton(hDlg,IDC_CHECK1,BST_UNCHECKED);
DeletePreviousOne();
}
else
{
CheckDlgButton(hDlg,IDC_CHECK1,BST_CHECKED);
返回 - 1 ;
}
}
}
}
return (INT_PTR)FALSE;
}





由于我的代码既没有错误也没有警告,解决这个问题的唯一方法是通过设置断点进行调试,以便我通过设置断点来做到这一点

 ==>> DeletePreviousOne(); 





我发现我的函数从未调用过。如何实现我的愿望?



我的代码没有足够的信息吗?

我错过了什么吗?



请帮我这个先生请



谢谢



UPDATE -2:

对不起先生我试过这个,我的代码表现不同:(即使我按下取消按钮也能正常工作。

  case  WM_COMMAND:
if (LOWORD(wParam)== IDOK || LOWORD(wParam)== IDCANCEL)
{
case 1008 :{
bool 已选中= IsDlgButtonChecked(hDlg,IDC_CHECK1);
if (选中){
CheckDlgButton(hDlg,IDC_CHECK1,BST_UNCHECKED);
DeletePreviousOne( );
}
else
{
CheckDlgButton(hDlg,IDC_CHECK1,BST_CHECKED);
返回 - 1 ;
}
}
EndDialog(hDlg,LOWORD(wParam));
return (INT_PTR)TRUE;
}





在设定断点时,我发现即使按下取消按钮,我的功能也会被调用。 br />


如何解决这个问题先生?

我错过了什么东西吗?



请先帮助我这位先生

谢谢



更新 - 3:



 INT_PTR CALLBACK优化器(HWND hDlg,UINT消息,WPARAM wParam,LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (消息)
{
// ------------------------------------------- ---

// --------- -------------------------------------
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam)= = IDOK || LOWORD(wParam)== IDCANCEL)
{
EndDialog(hDlg,LOWORD(wParam));
return (INT_PTR)TRUE;
if (IDOK == Optimizer(hDlg,message,wParam,lParam)){
case IDC_CHECK1:
{
case BN_CLICKED:
if (SendDlgItemMessage(hDlg,IDC_CHECK1,BM_GETCHECK, 0 0 ))
{
recycler();
}
break ;
}
}

}
break ;
}
return (INT_PTR)FALSE;
}





现在的问题是只有选中复选框才能获取功能,但是当用户点击取消时它也会执行按钮。



请先帮助我。

解决方案

感谢您的澄清。请参阅:按钮消息(Windows) [ ^ ]。



请记住,复选框也是一个按钮。 当用户点击按钮时,其状态会发生变化,按钮会以 WM_COMMAND 消息的形式发送通知代码。查看通知代码的任何页面。你会看到 wParam (32位)包含按钮的控制标识符作为低位字和通知代码作为高位字,并且 lParam 包含按钮的句柄(在您的情况下为复选框)。



您可以随时向复选框发送消息(使用其 HWND ),以检索已检查的状态: BM_GETCHECK消息(Windows) [ ^ ]。



-SA

Sir, I have created a Check box using win32 API in visual studio 2015.
My check box has been created successfully without any errors.

But this does not performs the required task.

My dialog boxes contains two push buttons ok and cancel. I am willing to to add a function call when the dialog box is checked and when the OK button is pressed that function should also be executed in addition to the normal functions

INT_PTR CALLBACK StoreHere(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
		//----------------------------------------------

		//----------------------------------------------
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
		if (IDOK == Optimizer(hDlg, message, wParam, lParam)) {
	case 1008: {//case 1008 is the ID for check box 1
		bool checked = IsDlgButtonChecked(hDlg, IDC_CHECK1);
		if (checked) {
			CheckDlgButton(hDlg, IDC_CHECK1, BST_UNCHECKED);
			DeletePreviousOne();
		}
		else
		{
			CheckDlgButton(hDlg, IDC_CHECK1, BST_CHECKED);
			return -1;
		}
	}
		}
}
	return (INT_PTR)FALSE;
}



Since my code has neither errors nor warnings the only way to solve this is debugging by setting break points so I did this by setting break points here

==>> DeletePreviousOne();



I found that my function gets never called.How to achieve my wish?

Is my code has not sufficient information?
Am I missing anything?

Kindly help me with this sir please

Thank you

UPDATE -2:
Sorry sir I tried this my code behaves differently:(It functions even when I press the cancel button.

case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
	case 1008: {
		bool checked = IsDlgButtonChecked(hDlg, IDC_CHECK1);
		if (checked) {
			CheckDlgButton(hDlg, IDC_CHECK1, BST_UNCHECKED);
			DeletePreviousOne();
		}
		else
		{
			CheckDlgButton(hDlg, IDC_CHECK1, BST_CHECKED);
			return -1;
		}
	}
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}



On setting the break points I came to know that my function gets called even I press the cancel button.

How to solve this problem sir?
Am I missing something or everything?

Kindly help me with this sir please
Thank you

UPDATE - 3:

INT_PTR CALLBACK Optimizer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
		//----------------------------------------------

		//----------------------------------------------
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
			if (IDOK == Optimizer(hDlg, message, wParam, lParam)) {
	case IDC_CHECK1:
	{
	case BN_CLICKED:
		if (SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK, 0, 0))
		{
			recycler();
		}
		break;
			}
			}
			
		}
		break;
}
	return (INT_PTR)FALSE;
}



The problem now is function gets only when the check box is checked but it also executes when the user hits cancel button.

Kindly help me with this sir.

解决方案

Thank you for your clarifications. Please see: Button Messages (Windows)[^].

Remember that a check box is also a button. "When the user clicks a button, its state changes, and the button sends notification codes, in the form of WM_COMMAND messages." Look at any page on notification code. You will see that wParam (32 bits) contains the button's control identifier as the lower word and notification code as the higher word, and lParam contains a handle to a button (check box, in your case).

At any time, you can also send message to the check box (using its HWND), to retrieve the checked state: BM_GETCHECK message (Windows)[^].

—SA


这篇关于Win32中的复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆