ChokeK
March 5th, 2006, 05:26 PM
Hello, this is my first post on the forums.
I'm definitely new to C++ and Win32, and programming in general. In fact, I just started self-teaching myself C++ and Win32 programming about a week ago.
I'm just trying to make a program for fun that presents a password window upon the WM_CLOSE message. I made my dialog box with CreateWindowEx(), here's the code for that...
//This is in the main WndProc...
case WM_CLOSE:
hQuitBox = CreateWindowEx(0, //bring up the password window
szQBName,
"Exit Attempt",
WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE,
400,
400,
200,
150,
hWnd,
NULL,
hInst,
NULL
);
ShowWindow(FindWindow("hQuitBox", 0), SW_SHOW);
UpdateWindow(FindWindow("hQuitBox", 0));
break;
//END
So hQuitBox is the HWND of the password input window. Now, the WNDCLASSEX szQBName uses the DlgProc...and this is the code for that....
LRESULT CALLBACK DlgProc(HWND hQuitBox, UINT messg2, WPARAM wParam,
LPARAM lParam)
{
HDC hdc; //handle to device context
PAINTSTRUCT pstruct2; //struct for the call to beginpaint
HWND pwEdit;
switch(messg2)
{
case WM_CREATE:
CreateWindowEx( 0,
TEXT("STATIC"),
TEXT(message4),
WS_CHILD|WS_VISIBLE|SS_LEFT,
5,
5,
125,
30,
hQuitBox,
(HMENU)IDC_STATICTXT2,
hInst,
NULL
);
pwEdit = CreateWindowEx( WS_EX_CLIENTEDGE,
"EDIT",
NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_PASSWORD,
0,
20,
150,
25,
hQuitBox,
(HMENU)IDC_PWBOX,
hInst,
NULL
);
CreateWindowEx( 0,
"BUTTON",
"&OK",
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
100,
85,
75,
25,
hQuitBox,
(HMENU)IDOK,
hInst,
NULL
);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case IDOK:
{
//Get the length of the text user typed into the edit
long Len1 = GetWindowTextLength(GetDlgItem(hQuitBox,
IDC_PWBOX));
//set the password needed to "asdf" and init szPasswordEntered
char szPasswordNeeded[] = "asdf";
char szPasswordEntered[Len1+1];
//Get the text the user typed into the edit, store it in
szPasswordEntered
GetDlgItemText(hQuitBox, IDC_PWBOX, szPasswordEntered, sizeof
(szPasswordEntered));
//If szPasswordEntered is longer than the password, do this...
if(sizeof(szPasswordEntered) > 5){
MessageBox(hQuitBox, "Invalid Password!",
"Incorrect Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
//If the edit is empty, do this...
if(sizeof(szPasswordEntered) == 1){
MessageBox(hQuitBox, "Null Entry", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
//If the user put the password in correctly, do this...
else if(szPasswordEntered == szPasswordNeeded){
MessageBox(hQuitBox, "Correct Password Entered!", "Goodbye!",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
SendMessage(FindWindowEx(NULL, NULL, szProgName,
szProgName),
WM_DESTROY, 0, 0);
}
//If the user put the password in incorrectly, do this...
else if(szPasswordEntered != szPasswordNeeded){
MessageBox(hQuitBox, szPasswordEntered, "Incorrect
Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(pwEdit, WM_SETTEXT, 0, 0);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
}
break;
}
break;
szProgName is the WNDCLASSEX of the main app window. As you can see, I have it set up so that if the user puts the wrong password in, a message window pops up displaying the text the user entered. And the password needed in this sample code is "asdf".......but when I type "asdf" into the password box, I get the message box telling me it's incorrect, but it does display "asdf" as the password I entered. Why is this? Am I doing something wrong with the char variables? I don't think I am, because everything displays correctly, and if I take out the "+1" in the line
"char szPasswordEntered[Len1+1];" then it displays "asd" as the incorrect password entered.
Help!!!
Let me know if you need the whole source, I'll repost with it. Thanks much, in advance!!
-Kurt G.
I'm definitely new to C++ and Win32, and programming in general. In fact, I just started self-teaching myself C++ and Win32 programming about a week ago.
I'm just trying to make a program for fun that presents a password window upon the WM_CLOSE message. I made my dialog box with CreateWindowEx(), here's the code for that...
//This is in the main WndProc...
case WM_CLOSE:
hQuitBox = CreateWindowEx(0, //bring up the password window
szQBName,
"Exit Attempt",
WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE,
400,
400,
200,
150,
hWnd,
NULL,
hInst,
NULL
);
ShowWindow(FindWindow("hQuitBox", 0), SW_SHOW);
UpdateWindow(FindWindow("hQuitBox", 0));
break;
//END
So hQuitBox is the HWND of the password input window. Now, the WNDCLASSEX szQBName uses the DlgProc...and this is the code for that....
LRESULT CALLBACK DlgProc(HWND hQuitBox, UINT messg2, WPARAM wParam,
LPARAM lParam)
{
HDC hdc; //handle to device context
PAINTSTRUCT pstruct2; //struct for the call to beginpaint
HWND pwEdit;
switch(messg2)
{
case WM_CREATE:
CreateWindowEx( 0,
TEXT("STATIC"),
TEXT(message4),
WS_CHILD|WS_VISIBLE|SS_LEFT,
5,
5,
125,
30,
hQuitBox,
(HMENU)IDC_STATICTXT2,
hInst,
NULL
);
pwEdit = CreateWindowEx( WS_EX_CLIENTEDGE,
"EDIT",
NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_PASSWORD,
0,
20,
150,
25,
hQuitBox,
(HMENU)IDC_PWBOX,
hInst,
NULL
);
CreateWindowEx( 0,
"BUTTON",
"&OK",
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
100,
85,
75,
25,
hQuitBox,
(HMENU)IDOK,
hInst,
NULL
);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case IDOK:
{
//Get the length of the text user typed into the edit
long Len1 = GetWindowTextLength(GetDlgItem(hQuitBox,
IDC_PWBOX));
//set the password needed to "asdf" and init szPasswordEntered
char szPasswordNeeded[] = "asdf";
char szPasswordEntered[Len1+1];
//Get the text the user typed into the edit, store it in
szPasswordEntered
GetDlgItemText(hQuitBox, IDC_PWBOX, szPasswordEntered, sizeof
(szPasswordEntered));
//If szPasswordEntered is longer than the password, do this...
if(sizeof(szPasswordEntered) > 5){
MessageBox(hQuitBox, "Invalid Password!",
"Incorrect Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
//If the edit is empty, do this...
if(sizeof(szPasswordEntered) == 1){
MessageBox(hQuitBox, "Null Entry", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
//If the user put the password in correctly, do this...
else if(szPasswordEntered == szPasswordNeeded){
MessageBox(hQuitBox, "Correct Password Entered!", "Goodbye!",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
SendMessage(FindWindowEx(NULL, NULL, szProgName,
szProgName),
WM_DESTROY, 0, 0);
}
//If the user put the password in incorrectly, do this...
else if(szPasswordEntered != szPasswordNeeded){
MessageBox(hQuitBox, szPasswordEntered, "Incorrect
Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(pwEdit, WM_SETTEXT, 0, 0);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
}
break;
}
break;
szProgName is the WNDCLASSEX of the main app window. As you can see, I have it set up so that if the user puts the wrong password in, a message window pops up displaying the text the user entered. And the password needed in this sample code is "asdf".......but when I type "asdf" into the password box, I get the message box telling me it's incorrect, but it does display "asdf" as the password I entered. Why is this? Am I doing something wrong with the char variables? I don't think I am, because everything displays correctly, and if I take out the "+1" in the line
"char szPasswordEntered[Len1+1];" then it displays "asd" as the incorrect password entered.
Help!!!
Let me know if you need the whole source, I'll repost with it. Thanks much, in advance!!
-Kurt G.