#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include "dialogres.h"
static BOOL CALLBACK InputBoxFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static BOOL CALLBACK PasswordFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static int ReadInput(HWND hwnd);
static int ReadPassword(HWND hwnd);
static void CenterWindow (HWND hWnd);
static HINSTANCE hinst;
static void DoRegisterClass(void)
{
WNDCLASS wc;
memset(&wc,0,sizeof(wc));
wc.lpfnWndProc = DefDlgProc;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hinst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszClassName = "dialog";
RegisterClass(&wc);
}
int WINAPI LibMain(HINSTANCE hDLLInst, DWORD Reason, LPVOID Reserved)
{
switch (Reason)
{
case DLL_PROCESS_ATTACH:
hinst = hDLLInst;
DoRegisterClass();
break;
case DLL_PROCESS_DETACH:
UnregisterClass("dialog",hDLLInst);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
static char inputbuffer[1024];
static char UserIDbuffer[255];
static char Passwordbuffer[255];
static char *bprompt;
int APIENTRY __declspec(dllexport)
InputBox(char *Title, char *prompt, char *destbuffer){
int result;
int buflen = 0;
bprompt = prompt;
result = DialogBoxParam(hinst,
MAKEINTRESOURCE(IDD_INPUTBOXDLG),
NULL,
(DLGPROC) InputBoxFunc,
(int)Title);
if (result == 1) {
strncpy(destbuffer,inputbuffer,strlen(inputbuffer));
buflen = strlen(inputbuffer);
}
return buflen;
}
static int InitializeInputBox(HWND hDlg,WPARAM wParam, LPARAM lParam)
{
HFONT font;
CenterWindow(hDlg);
font = GetStockObject(DEFAULT_GUI_FONT);
SendDlgItemMessage(hDlg,IDENTRYFIELD, WM_SETFONT,(WPARAM)font,0);
SetWindowText(hDlg, (char *)lParam);
SendDlgItemMessage(hDlg,IDPROMPT, WM_SETTEXT,(WPARAM)0,(LPARAM)bprompt);
SendDlgItemMessage(hDlg,IDENTRYFIELD, EM_SETLIMITTEXT,512,0);
SetFocus(GetDlgItem(hDlg,IDENTRYFIELD));
EnableWindow(GetDlgItem(hDlg,IDOK),0);
return 1;
}
static BOOL CALLBACK InputBoxFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
InitializeInputBox(hwndDlg,wParam,lParam);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
if (ReadInput(hwndDlg)) {
EndDialog(hwndDlg,1);
}
return 1;
case IDCANCEL:
EndDialog(hwndDlg,0);
return 1;
case IDENTRYFIELD:
switch (HIWORD(wParam)) {
case EN_CHANGE:
if (GetDlgItemText(hwndDlg,IDENTRYFIELD, inputbuffer,sizeof(inputbuffer)))
{
EnableWindow(GetDlgItem(hwndDlg,IDOK),1);
}
else
EnableWindow(GetDlgItem(hwndDlg,IDOK),0);
break;
}
break;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg,0);
return TRUE;
}
return FALSE;
}
static int ReadInput(HWND hwnd)
{
memset(inputbuffer,0,sizeof(inputbuffer));
if (GetDlgItemText(hwnd,
IDENTRYFIELD,
inputbuffer,
sizeof(inputbuffer))) {
return 1;
}
return 0;
}
int APIENTRY __declspec(dllexport)
GetPassword(char *Title, char *prompt, char *Userbuffer, char *PassBuffer){
int result;
bprompt = prompt;
result = DialogBoxParam(hinst,
MAKEINTRESOURCE(IDD_PASSWORDDLG),
NULL,
(DLGPROC) PasswordFunc,
(int)Title);
if (result == 1) {
strncpy(Userbuffer,UserIDbuffer,strlen(UserIDbuffer));
Userbuffer[strlen(UserIDbuffer)+1] = 0;
strncpy(PassBuffer,Passwordbuffer,strlen(Passwordbuffer));
PassBuffer[strlen(Passwordbuffer)+1] = 0;
}
return result;
}
static int InitializePassWord(HWND hDlg,WPARAM wParam, LPARAM lParam)
{
HFONT font;
CenterWindow(hDlg);
font = GetStockObject(DEFAULT_GUI_FONT);
SendDlgItemMessage(hDlg,IDUSERFIELD, WM_SETFONT,(WPARAM)font,0);
SendDlgItemMessage(hDlg,IDPASSWORDFIELD, WM_SETFONT,(WPARAM)font,0);
SetWindowText(hDlg, (char *)lParam);
SendDlgItemMessage(hDlg,IDPROMPT, WM_SETTEXT,(WPARAM)0,(LPARAM)bprompt);
SendDlgItemMessage(hDlg,IDUSERFIELD, EM_SETLIMITTEXT,32,0);
SendDlgItemMessage(hDlg,IDPASSWORDFIELD, EM_SETLIMITTEXT,32,0);
SetFocus(GetDlgItem(hDlg,IDUSERFIELD));
EnableWindow(GetDlgItem(hDlg,IDOK),0);
return 1;
}
static BOOL CALLBACK PasswordFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
InitializePassWord(hwndDlg,wParam,lParam);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
if (ReadPassword(hwndDlg)) {
EndDialog(hwndDlg,1);
}
return 1;
case IDCANCEL:
EndDialog(hwndDlg,0);
return 1;
case IDUSERFIELD:
switch (HIWORD(wParam)) {
case EN_CHANGE:
if (GetDlgItemText(hwndDlg,IDUSERFIELD, UserIDbuffer,sizeof(UserIDbuffer)))
{
EnableWindow(GetDlgItem(hwndDlg,IDOK),1);
}
else
EnableWindow(GetDlgItem(hwndDlg,IDOK),0);
break;
}
break;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg,0);
return TRUE;
}
return FALSE;
}
static int ReadPassword(HWND hwnd)
{
int result = 0;
memset(UserIDbuffer,0,sizeof(UserIDbuffer));
memset(Passwordbuffer,0,sizeof(Passwordbuffer));
if (GetDlgItemText(hwnd,
IDUSERFIELD,
UserIDbuffer,
sizeof(UserIDbuffer))) {
if (GetDlgItemText(hwnd,
IDPASSWORDFIELD,
Passwordbuffer,
sizeof(Passwordbuffer))) {
result = 1;
}
else {MessageBox(0,"PassWord is necessary","PassWord error",MB_OK);}
}
return result;
}
static void CenterWindow (HWND hWnd)
{
static RECT wRect;
static DWORD x;
static DWORD y;
GetWindowRect(hWnd,&wRect);
x=(GetSystemMetrics(SM_CXSCREEN)-(wRect.right-wRect.left))/2;
y=(GetSystemMetrics(SM_CYSCREEN)-(wRect.bottom-wRect.top+GetSystemMetrics(SM_CYCAPTION)))/2;
SetWindowPos(hWnd,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
}
|
|