Guidance
指路人
g.yi.org
software / rapidq / Examples / Win32API & DLL / GetPassword / password.c

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
/*<---------------------------------------------------------------------->*/
/*<---------------------------------------------------------------------->*/
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include "dialogres.h"



/* prototype for the dialog box function. */
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 *bufferlen)
{
	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;
}


/*
You should add your initialization code here. This function will be called
when the dialog box receives the WM_INITDIALOG message.
*/
static int InitializeInputBox(HWND hDlg,WPARAM wParam, LPARAM lParam)
{
	HFONT font;

	CenterWindow(hDlg);
//	font = GetStockObject(ANSI_FIXED_FONT);
	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));
	// Disable the IDOK button at the start.
	EnableWindow(GetDlgItem(hDlg,IDOK),0);
	return 1;
}

/*
This is the main function for the dialog. It handles all messages. Do what your
application needs to do here.
*/
static BOOL CALLBACK InputBoxFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	/* This message means the dialog is started but not yet visible.
	   Do All initializations here
        */
	case WM_INITDIALOG:
		InitializeInputBox(hwndDlg,wParam,lParam);
		return FALSE;
	/* By default, IDOK means close this dialog returning 1, IDCANCEL means
           close this dialog returning zero
        */
	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;
        /* By default, WM_CLOSE is equivalent to CANCEL */
	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 *bufferlen)
{
	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));
	// Disable the IDOK button at the start.
	EnableWindow(GetDlgItem(hDlg,IDOK),0);
	return 1;
}


static BOOL CALLBACK PasswordFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	/* This message means the dialog is started but not yet visible.
	   Do All initializations here
        */
	case WM_INITDIALOG:
		InitializePassWord(hwndDlg,wParam,lParam);
		return FALSE;
	/* By default, IDOK means close this dialog returning 1, IDCANCEL means
           close this dialog returning zero
        */
	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;
        /* By default, WM_CLOSE is equivalent to CANCEL */
	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);
}


掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2021-10-23  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2000-10-19 17:29:20