Saturday, 7 September 2013

How can i remove these texboxs after successful login? in win32 c++ application

How can i remove these texboxs after successful login? in win32 c++
application

I got a small window with textbox for username and pass, locked textbox
for display output and button to log in. The logging in process works
good.
After a successful login, I need the window to change in that the two text
boxes for username and password disappear, become locked, or deleted for
the time being, whichever is simpler. I also need to change the text on
the button to "logout" instead of login after a successful login. I can
already change what the button does by checking the loggedin bool before
choosing an action from the button, but I dont know how to change the text
on it. or replace it with a different button say. I searched stuff like
toggle visibility, remove textbox/window. I can't seem to find anything.
If i could just remove them and trigger another WM_CREATE instance i could
have a 2nd layout in that case for after logged in, But ya i don't know
how to remove and maybe that's not the normal way of doing it. Answer my
question or suggest a different way of doing this would be ok too. Thank
you
my code:
#include "StdAfx.h"
#include "Resource.h"
#include <windows.h>
#include <string>
#define IDC_MAIN_BUTTON 101 // Button identifier
#define IDC_MAIN_EDIT 102 // Edit box identifier
HWND hEdit, hEdit2, noEdit;
bool loggedin = false;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int
nShowCmd)
{
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,
"Window class creation failed\r\n",
"Window Class Failed",
MB_ICONERROR);
}
HWND hWnd=CreateWindowEx(NULL,
"Window Class",
"Windows application",
WS_OVERLAPPEDWINDOW,
200,
200,
300,
200,
NULL,
NULL,
hInst,
NULL);
if(!hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,
"Window creation failed\r\n",
"Window Creation Failed",
MB_ICONERROR);
}
ShowWindow(hWnd,nShowCmd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
connect();
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
// Create an edit box
hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
70,
200,
25,
hWnd,
(HMENU)IDC_MAIN_EDIT,
GetModuleHandle(NULL),
NULL);
hEdit2=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
95,
200,
25,
hWnd,
(HMENU)IDC_MAIN_EDIT,
GetModuleHandle(NULL),
NULL);
noEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD|WS_VISIBLE|ES_READONLY|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
20,
200,
50,
hWnd,
(HMENU)IDC_MAIN_EDIT,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
SendMessage(hEdit2,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
SendMessage(noEdit,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
SendMessage(hEdit,
WM_SETTEXT,
NULL,
(LPARAM)"Nolan");
SendMessage(hEdit2,
WM_SETTEXT,
NULL,
(LPARAM)"password");
// Create a push button
HWND hWndButton=CreateWindowEx(NULL,
"BUTTON",
"Login",
WS_TABSTOP|WS_VISIBLE|
WS_CHILD|BS_DEFPUSHBUTTON,
50,
120,
100,
24,
hWnd,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL);
SendMessage(hWndButton,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_MAIN_BUTTON:
{
char buffer[256];
SendMessage(hEdit,
WM_GETTEXT,
sizeof(buffer)/sizeof(buffer[0]),
reinterpret_cast<LPARAM>(buffer));
string user(buffer);
SendMessage(hEdit2,
WM_GETTEXT,
sizeof(buffer)/sizeof(buffer[0]),
reinterpret_cast<LPARAM>(buffer));
string pass(buffer);
if(login(user, pass))
{
loggedin = true;
MessageBox(NULL, "You logged in!", "Information",
MB_ICONINFORMATION);
}
else
{
loggedin = false;
MessageBox(NULL, "incorrect username or password",
"Information", MB_ICONINFORMATION);
}
}
break;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

No comments:

Post a Comment