Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
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
C++
==>> 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.
C++
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:

C++
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.
Posted
Updated 23-Jan-16 22:49pm
v5
Comments
Sergey Alexandrovich Kryukov 23-Jan-16 15:41pm    
Did you set up windows function for a check box? From your code, it's not clear what you are handling.
Besides, it's really bad to use immediate constants such as 1008. This is the anti-pattern called "magic number".
—SA
[no name] 23-Jan-16 15:52pm    
Sir, My function gets called even when the check box is not clicked
i.e, When I have not clicked the check box and clicked Push (ok or cancel) button my check box is marked by a tick automatically and then started executing by itself.The above is what I did to handle the checkbox.
Thank you
Sergey Alexandrovich Kryukov 23-Jan-16 15:57pm    
You need show some comprehensive context. Are you doing it in a dialog? What is that code? dialog function, window function? of what object?..
—SA
Sergey Alexandrovich Kryukov 23-Jan-16 16:01pm    
Instead of taking all your code, develop a separate debug project, minimized as possible. The main function (entry point) should show one window/dialog, with only one check box, nothing else, and the change of state should be handled, say, showing a message box with the state... If this is not the part of your scenario, please clarify.
—SA
Sergey Alexandrovich Kryukov 23-Jan-16 16:05pm    
Yes, I mean that. By not doing so you would waste too much time on unrelated aspects and miscommunication.
—SA

1 solution

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
 
Share this answer
 
Comments
[no name] 24-Jan-16 4:46am    
Sir, some one has downvoted your question but for me it helped to solve half of my problems.So, My vote of 5. I tried the same using different project but it happens the same.
I have now considered check box as a button and modified my code
Here is what I did as per the link given by you,
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;
}
Now my problem is Function executes even when user presses the cancel button when the check box is checked. But now my code behaves correctly if the check box is not pressed.

Thank you sir for your kind help
Sergey Alexandrovich Kryukov 24-Jan-16 13:35pm    
Could you move this new code fragment to the body of the question using "Improve question", to make it properly formatted? But this is a pure execution flow logic which can be fixed is you use the debugger.
Are you going to accept the answer formally?
—SA
[no name] 25-Jan-16 11:00am    
Sir, I have already added this is my question.Thank you.
"Are you going to accept the answer formally?"
Yes, Sir I will accept your answer.
[no name] 26-Jan-16 12:23pm    
Sir, by all means I have tried every thing
I have set the break points also and found that this if statement gets called [if (SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK, 0, 0))] when I press cancel or even when I terminate my dialog box.

To solve that I have tried specifying only for OK button like this
if (IDOK==SendDlgItemMessage(hDlg, IDC_CHECK1, BM_GETCHECK, 0, 0))
but has no use on this.
Sir, what should I do now?
I have tried the msdn documentation but no use(should be my fault).Could you please give me some links or Is there any advanced topics in debugging to learn?
If yes could you specify something?

Thank you sir for your kind help.
Sergey Alexandrovich Kryukov 26-Jan-16 14:02pm    
As far as I can see, the code fragment for WM_COMMAND case is wrong. It ends with opening bracket '{' under "if", with another case underneath. You have to remove it. Switch statement should come with some cases each ending with "break;". Just re-factor your code. Your case WM_COMMAND can have a block ending with "break". Inside this block, you can have another switch based on, say, lower word of lParam. Better yet, at this point call a separate function which processes WM_COMMAND depending in wParam and lParam you can pass to it.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900