Checkbox/Registry Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I am new to C++ & would like to create a MFC application with a checkbox
control on it

On program startup I want it to test a specific DWORD then check/uncheck the
checkbox

On click I want the registry entry to changed to either 1 or 0 depending if
the checkbox was checked or unchecked

How can I do this?
 
I am new to C++ & would like to create a MFC application with a checkbox
control on it

In the future, try posting in microsoft.public.vc.mfc for mfc
questions, this is a .Net vc group.

It's hard to tell you precisely what to do because you are new to
VC/MFC and don't know the basics. I suggest that you find and work
through the MFC Scribble tutorial in order to get a better idea.

To access the registry, MFC has the CRegKey class.
On program startup I want it to test a specific DWORD then check/uncheck the
checkbox

To check a check box, you use the CButton GetCheck SetCheck methods (a
check box is a button control).
On click I want the registry entry to changed to either 1 or 0 depending if
the checkbox was checked or unchecked

Handle the button's ON_BN_CLICKED event - the Scribble tutorial will
show you how to do all these key concepts.

Dave
 
David,

Thank you for replying

I am a VB programmer & have been since version 3 but I want to make the
application unmanaged so not reliant on any .NET Framework. In VB.NET, I
have written the code but have no idea in C++. Using Visual Studio 2008
Professional I want to create a Win32 C++ application. Here is some of my
VB.NET code:

Imports Microsoft.Win32

Private Function GetStartupValue() As Integer
Dim intTemp As Integer = 0
Try
reg = Registry.LocalMachine.OpenSubKey(strKey, False)
If Not (reg Is Nothing) Then
intTemp = reg.GetValue(strValue)
reg.Close()
End If
Return intTemp
Catch ex As Exception
MessageBox.Show("Unable to read registry setting", Me.Text)
End Try
End Function

Private Sub frmDisable_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If GetStartupValue() = 1 Then
chkDisable.Checked = False
Else
chkDisable.Checked = True
End If
End Sub

Private Sub chkDisable_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles chkDisable.CheckedChanged
Try
reg = Registry.LocalMachine.OpenSubKey(strKey, True)
If chkDisable.Checked = True Then
reg.SetValue(strValue, 0)
Else
reg.SetValue(strValue, 1)
End If
If Not (reg Is Nothing) Then reg.Close()
Catch ex As Exception
MessageBox.Show("Unable to change reg setting", Me.Text)
End Try
End Sub

The value I am getting is a DWORD which is either 0 or 1

----------------------

I created a simple MFC EXE application, added a checkbox, right-clicked it &
added a variable IsDisabled then used that variable to handle the checkbox
click:

void CUACDlg::OnBnClickedchkdisable()
{
HKEY hkey;
DWORD dwDisposition;
DWORD data;
BOOL IsDisabled=false;
int size = sizeof (DWORD);
if(IsDisabled)
{
IsDisabled=1;
RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Some\\Key\\Here"), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwDisposition);
RegSetValueEx(hkey, TEXT("Disabled"), 0, REG_DWORD, (LPBYTE)&data, size);
//RegCloseKey(hkey);
}
else{
IsDisabled=0;
RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Some\\Key\\Here"), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dwDisposition);
RegSetValueEx(hkey, TEXT("Disabled"), 0, REG_DWORD, (LPBYTE)&data, size);
//RegCloseKey(hkey);
}
RegCloseKey(hkey);
}

Scribble Tutorial:

http://msdn.microsoft.com/en-us/library/aa716527(VS.60).aspx

Any ideas?
 
David,

The conversion of the VB.NET code into unmanaged C++

You saw my attempt at C++. What am I doing wrong?
 
The conversion of the VB.NET code into unmanaged C++
You saw my attempt at C++. What am I doing wrong?

One thing that I can see straightaway...
I created a simple MFC EXE application, added a checkbox,
right-clicked it &
added a variable IsDisabled then used that variable to handle the
checkbox
click:

void CUACDlg::OnBnClickedchkdisable()
{
HKEY hkey;
DWORD dwDisposition;
DWORD data;
BOOL IsDisabled=false;
int size = sizeof (DWORD);
if(IsDisabled)
{
<

Your IsDisabled is a local variable so it's always going to be false,
so that first conditional if is never going to be true.

Presumably you want it to toggle depending on the current state of the
check box - so use GetCheck to get the current state of the check box.

A lot of what VB does fairly eleganly behind the scenes for you, you
have to do yourself in VC/MFC.

Have a go at the Scribble tutorial, it will show you the key aspects.

Dave
 
Back
Top