Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "StyleTheme.h"
#define IDM_EXIT 1
#define IDM_TEST 2
LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);
LRESULT CALLBACK AddUserProc(HWND , UINT , WPARAM , LPARAM);
LRESULT APIENTRY ProcUsername(HWND , UINT , WPARAM , LPARAM);
LRESULT APIENTRY ProcPassword(HWND , UINT , WPARAM , LPARAM);
LRESULT APIENTRY ProcButtonLogin(HWND , UINT , WPARAM , LPARAM);
LRESULT APIENTRY ProcButtonForgotPass(HWND , UINT , WPARAM , LPARAM);
WNDPROC wpUsername;
WNDPROC wpPassword;
WNDPROC wpButtonLogin;
WNDPROC wpButtonForgotPassword;
void ShowAddUser();
void AppendWindowText(HWND hWnd , const char * lpString);
char szAppName[] = "Login Program";
HWND hwnd, hwndMain, hwndRegister , hwndAddUser;
HWND hwndButtonLogin;
HWND hwndButtonForgotPassword;
HWND hwndButtonAddUser;
HWND hwndEditUserName , hwndStaticUserName;
HWND hwndEditPassword , hwndStaticPassword;
HWND hwndEditAddUser , hwndButtonSaveUser , hwndButtonCloseUser;
HWND hwndLogedIn;
HWND hwndStaticBanner;
MSG msg;
WNDCLASSEX wc;
HFONT Font1;
HFONT Font2;
HFONT BannerFont;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIntance, LPSTR szCmdLine, int iCmdShow)
{
wc.style = CS_DBLCLKS;
wc.lpfnWndProc = WndProc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL , IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL , IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
wc.lpszClassName = szAppName;
wc.lpszMenuName = NULL;
if(!RegisterClassEx(&wc)) return 0;
Font1 = CreateFont(14, 0, 0, 0, 0, FALSE, FALSE, 0, 0, 0, 0, 0, 0, "Arial");
Font2 = CreateFont(15, 0, 0, 0 ,0, FALSE, FALSE, 0, 0, 0, 0, 0, 0, "Arial");
BannerFont = CreateFont(60, 0, 0, 0 ,0, FALSE, FALSE, 0, 0, 0, 0, 0, 0, "Monotype Corsiva");
hwnd = CreateWindowEx(0,szAppName , "Login." ,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
hwndAddUser = CreateWindowEx(0 , szAppName , "Add User",
WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT , CW_USEDEFAULT ,
400 , 400 ,
NULL , HWND_DESKTOP , hInstance , NULL);
hwndMain = CreateWindowEx(0, szAppName , NULL ,
WS_OVERLAPPED | WS_CHILD | WS_VISIBLE ,
0 , 0 ,
760 , 509 ,
hwnd , NULL , hInstance , NULL);
hwndStaticBanner = CreateWindow("static" , "Management system" ,
WS_VISIBLE | WS_CHILD ,
200, 40, 500,150,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndLogedIn = CreateWindowEx(0 , szAppName , NULL ,
WS_OVERLAPPED | WS_CHILD | WS_BORDER ,
0 , 0 ,
760 , 509 ,
hwnd , NULL , hInstance , NULL);
hwndStaticUserName = CreateWindow("static" , "UserName:" ,
WS_VISIBLE | WS_CHILD ,
300, 170, 75,15,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndEditUserName = CreateWindowEx(WS_EX_STATICEDGE , "edit" , NULL ,
WS_VISIBLE | WS_CHILD ,
380, 170, 120,20,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndEditPassword = CreateWindowEx(WS_EX_STATICEDGE , "edit" , NULL ,
WS_VISIBLE | WS_CHILD ,
380, 200, 120,20,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndStaticPassword = CreateWindow("static" , "Password:",
WS_VISIBLE | WS_CHILD ,
300, 200, 75,20,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndButtonLogin = CreateWindowEx(0, "button", "&Login",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON ,
400, 225, 100, 25,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndButtonForgotPassword = CreateWindowEx(0, "button", "Forgot Password?",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON ,
295, 225, 100, 25,
hwndMain,
NULL,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndButtonAddUser = CreateWindow("button" , "Add User" ,
WS_CHILD | BS_PUSHBUTTON ,
295 , 255 , 206, 25 ,
hwndMain ,
NULL ,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE),
NULL);
hwndEditAddUser = CreateWindowEx(WS_EX_STATICEDGE , "edit" , NULL ,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL ,
5 , 5, 380 , 320 ,
hwndAddUser ,
NULL ,
(HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE) ,
NULL);
hwndButtonSaveUser = CreateWindow("button" , "Save" ,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
50 , 335 , 120 , 25 ,
hwndAddUser,
NULL,
(HINSTANCE)GetWindowLong(hwndAddUser , GWL_HINSTANCE),
NULL);
hwndButtonCloseUser = CreateWindow("button" , "Close" ,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
200 , 335 , 120 , 25 ,
hwndAddUser,
NULL,
(HINSTANCE)GetWindowLong(hwndAddUser , GWL_HINSTANCE),
NULL);
SendMessage(hwndButtonForgotPassword, WM_SETFONT , (WPARAM)Font1,(LPARAM)TRUE);
SendMessage(hwndButtonLogin, WM_SETFONT , (WPARAM)Font2,(LPARAM)TRUE);
SendMessage(hwndButtonAddUser , WM_SETFONT , (WPARAM)Font2 , (LPARAM)TRUE);
SendMessage(hwndButtonSaveUser, WM_SETFONT , (WPARAM)Font2,(LPARAM)TRUE);
SendMessage(hwndButtonCloseUser, WM_SETFONT , (WPARAM)Font2,(LPARAM)TRUE);
SendMessage(hwndEditPassword, WM_SETFONT , (WPARAM)Font1,(LPARAM)TRUE);
SendMessage(hwndEditUserName, WM_SETFONT , (WPARAM)Font1,(LPARAM)TRUE);
SendMessage(hwndStaticUserName, WM_SETFONT , (WPARAM)Font2,(LPARAM)TRUE);
SendMessage(hwndStaticPassword, WM_SETFONT , (WPARAM)Font2,(LPARAM)TRUE);
SendMessage(hwndStaticBanner, WM_SETFONT , (WPARAM)BannerFont,(LPARAM)TRUE);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
SetFocus(hwndEditUserName);
wpUsername = (WNDPROC) SetWindowLong(hwndEditUserName, GWL_WNDPROC, (LONG) ProcUsername);
wpPassword = (WNDPROC) SetWindowLong(hwndEditPassword, GWL_WNDPROC, (LONG) ProcPassword);
wpButtonForgotPassword = (WNDPROC) SetWindowLong(hwndButtonForgotPassword, GWL_WNDPROC, (LONG) ProcButtonForgotPass);
wpButtonLogin = (WNDPROC) SetWindowLong(hwndButtonLogin, GWL_WNDPROC, (LONG) ProcButtonLogin);
while(GetMessage(&msg , NULL , 0 , 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HBRUSH EditColor = CreateSolidBrush(RGB(255,255,255));
HMENU hMenu;
HINSTANCE hInstance;
FILE *newCheck;
switch(message)
{
case WM_CREATE:
hInstance = (HINSTANCE)GetWindowLong(hwnd , GWL_HINSTANCE);
hMenu = LoadMenu(hInstance , "MenuMain");
SetMenu(hwnd , hMenu);
return 0;
case WM_DESTROY:
DestroyWindow(hwnd);
DestroyWindow(hwndAddUser);
PostQuitMessage(0);
return 0;
case SW_SHOW:
newCheck = fopen("110sal29o.as3" , "r");
if(newCheck == NULL){
break;
}else{
ShowWindow(hwndButtonAddUser , SW_SHOW);
}
return 0;
case WM_CTLCOLOREDIT:
SetBkColor((HDC)wParam , RGB(255,255,255));
SetTextColor((HDC)wParam , RGB(0,0,255));
return ((LRESULT)EditColor);
case WM_COMMAND:
switch(LOWORD(wParam)){
case IDM_EXIT:
SendMessage(hwnd , WM_CLOSE , 0 , 0);
return 0;
}
if((HWND)lParam == hwndButtonSaveUser){
FILE *fpUsers;
int Encrypt;
int Lines;
int ac;
int neof = 0;
int i;
char UserDetails[100];
Lines = SendMessage(hwndEditAddUser , EM_GETLINECOUNT , (WPARAM)0 , (LPARAM)0);
if((fpUsers = fopen("Users.dat" , "r+")) == NULL){
MessageBox(0 , "Users.dat not found" , "Error" , 0 | MB_ICONERROR);
SetFocus(hwndEditUserName);
}else{
fflush(fpUsers);
for(neof = 0; neof<Lines; neof++){
*((LPWORD)UserDetails) = sizeof(UserDetails) / sizeof(char);
ac = SendMessage(hwndEditAddUser , EM_GETLINE , (WPARAM)neof , (LPARAM)UserDetails);
UserDetails[ac] = 0;
for(Encrypt = 0; Encrypt<strlen(UserDetails); Encrypt++){
UserDetails[Encrypt] = UserDetails[Encrypt] ^ 2;
}
for(i = 0; i<strlen(UserDetails); i++){
if(UserDetails[i] == '"'){
UserDetails[i] = ' ';
}
}
fflush(fpUsers);
MessageBox(0 , UserDetails , "test" , 0);
fprintf(fpUsers , UserDetails);
fputc('\n' , fpUsers);
}
fclose(fpUsers);
}
}
if((HWND)lParam == hwndButtonCloseUser){
ShowWindow(hwndAddUser , SW_HIDE);
}
if((HWND)lParam == hwndButtonLogin){
FILE *fpLogin;
int DecLogin;
int DecUser;
int DecPass;
char LoginUsername[12];
char LoginPassword[12];
char LoginStatus[12];
char Username[12];
char Password[12];
char IsUser[20];
char IsPass[20];
SendMessage(hwndEditUserName , WM_GETTEXT , (WPARAM)sizeof(IsUser) , (LPARAM)IsUser);
SendMessage(hwndEditPassword , WM_GETTEXT , (WPARAM)sizeof(IsPass) , (LPARAM)IsPass);
if((strcmp(IsUser , "") == 0) && (strcmp(IsPass , "") == 0)){
MessageBox(0 , "Enter a Username & Password" , "Information" , 0 | MB_ICONINFORMATION);
SetFocus(hwndEditUserName);
break;
}else if((strcmp(IsUser , "") == 0)){
MessageBox(0 , "Enter a Username" , "Information" , 0 | MB_ICONINFORMATION);
SetFocus(hwndEditUserName);
break;
}else if((strcmp(IsPass , "") == 0)){
MessageBox(0 , "Enter a Password" , "Information" , 0 | MB_ICONINFORMATION);
SetFocus(hwndEditPassword);
break;
}
fpLogin = fopen("Users.dat" , "r");
if(fpLogin == NULL){
MessageBox(0, "Users.dat not found." , "Error" , 0 | MB_ICONERROR);
}else{
rewind(fpLogin);
while(!feof(fpLogin)){
fscanf(fpLogin , "%s %s %s\n" , LoginStatus , Username , Password);
for(DecLogin = 0; DecLogin<strlen(LoginStatus); DecLogin++){
LoginStatus[DecLogin] = LoginStatus[DecLogin] ^ 2;
}
for(DecUser = 0; DecUser<strlen(Username); DecUser++){
Username[DecUser] = Username[DecUser] ^ 2;
}
for(DecPass = 0; DecPass<strlen(Password); DecPass++){
Password[DecPass] = Password[DecPass] ^ 2;
}
SendMessage(hwndEditUserName , WM_GETTEXT , (WPARAM)sizeof(LoginUsername),(WPARAM)LoginUsername);
SendMessage(hwndEditPassword , WM_GETTEXT , (WPARAM)sizeof(LoginPassword),(WPARAM)LoginPassword);
if((strcmp(LoginUsername,Username) == 0) && (strcmp(LoginPassword,Password) == 0)&& strcmp(LoginStatus , "User") == 0){
MessageBox(0,"Login OK" , "Message" , 0);
break;
}else if((strcmp(LoginUsername,Username) == 0) && (strcmp(LoginPassword,Password) == 0) && strcmp(LoginStatus , "Admin") == 0){
ShowWindow(hwndButtonAddUser , SW_SHOW);
SetFocus(hwndEditAddUser);
}else{
if(feof(fpLogin)){
MessageBox(0 , "Incorrect Username and or Password" , "Error" , 0 | MB_ICONERROR);
SetFocus(hwndEditUserName);
}
}
}
}
}
if((HWND)lParam == hwndButtonAddUser){
ShowAddUser();
}
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK AddUserProc(HWND hwndAddUser , UINT message , WPARAM wParam , LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
ShowWindow(hwnd , SW_SHOW);
ShowWindow(hwndAddUser , SW_HIDE);
return 0;
}
return DefWindowProc(hwndAddUser , message , wParam , lParam);
}
void ShowAddUser()
{
FILE *fp;
int FileSize = 0;
int DecLogin;
int DecUser;
int DecPass;
int strlenUser , strlenPass, strlenLogin;
char UserText[50] = {0};
char PassText[50] = {0};
char LoginText[6] = {0};
char test[10] = {0};
if((fp = fopen("Users.dat" , "r")) == NULL){
MessageBox(0 , "User.dat not found." , "Error" , 0 | MB_ICONERROR);
}else{
rewind(fp);
fflush(fp);
SendMessage(hwndEditAddUser , WM_SETTEXT , (WPARAM)strlen(LoginText) , (LPARAM)LoginText);
ShowWindow(hwndAddUser , SW_SHOW);
while(!feof(fp)){
fscanf(fp , "%s %s %s\n" , LoginText , UserText , PassText);
MessageBox(0 , "break point" , "break point" , 0);
for(DecLogin = 0; DecLogin<strlen(LoginText); DecLogin++){
LoginText[DecLogin] = LoginText[DecLogin] ^ 2;
}
for(DecUser = 0; DecUser<strlen(UserText); DecUser++){
UserText[DecUser] = UserText[DecUser] ^ 2;
}
for(DecPass = 0; DecPass<strlen(PassText); DecPass++){
PassText[DecPass] = PassText[DecPass] ^ 2;
}
strlenUser = strlen(UserText);
strlenPass = strlen(PassText);
strlenLogin = strlen(LoginText);
if((strlenUser == 0) && (strlenPass == 0) && (strlenLogin == 0))
{
break;
}else{
AppendWindowText(hwndEditAddUser , LoginText);
AppendWindowText(hwndEditAddUser , " ");
AppendWindowText(hwndEditAddUser , UserText);
AppendWindowText(hwndEditAddUser , " ");
AppendWindowText(hwndEditAddUser , PassText);
AppendWindowText(hwndEditAddUser , "\n");
}
}
}
}
LRESULT APIENTRY ProcUsername(HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
case WM_CHAR:
if(wParam == VK_TAB){
SetFocus(hwndEditPassword);
}
if (wParam != VK_TAB){
CallWindowProc(wpUsername, hwnd, msg, wParam, lParam);
}
break;
default:
return CallWindowProc(wpUsername, hwnd , msg , wParam , lParam);
}
return 0;
}
LRESULT APIENTRY ProcPassword(HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
case WM_CHAR:
if(wParam == VK_TAB){
SetFocus(hwndButtonForgotPassword);
}
if (wParam != VK_TAB){
CallWindowProc(wpPassword, hwnd, msg, wParam, lParam);
}
break;
default:
return CallWindowProc(wpPassword, hwnd , msg , wParam , lParam);
}
return 0;
}
LRESULT APIENTRY ProcButtonForgotPass(HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
case WM_CHAR:
if(wParam == VK_TAB){
SetFocus(hwndButtonLogin);
}
if (wParam != VK_TAB){
CallWindowProc(wpButtonForgotPassword, hwnd, msg, wParam, lParam);
}
break;
default:
return CallWindowProc(wpButtonForgotPassword, hwnd , msg , wParam , lParam);
}
return 0;
}
LRESULT APIENTRY ProcButtonLogin(HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
case WM_CHAR:
if(wParam == VK_TAB){
SetFocus(hwndEditUserName);
}
if (wParam != VK_TAB){
CallWindowProc(wpButtonLogin, hwnd, msg, wParam, lParam);
}
break;
default:
return CallWindowProc(wpButtonLogin, hwnd , msg , wParam , lParam);
}
return 0;
}
void AppendWindowText(HWND hWnd, const char * lpString)
{
int iLength = GetWindowTextLength(hWnd);
SendMessage(hWnd, EM_SETSEL, iLength, iLength);
SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) lpString);
SendMessage(hWnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
}
and here is the resource file