win32 sdk inputbox 输入框

来源:互联网 发布:python培训视频 编辑:程序博客网 时间:2024/05/16 15:59
//InputDialog.cpp #include "StdAfx.h"#include ".\inputdialog.h"CInputDialog::CInputDialog(void){}CInputDialog::~CInputDialog(void){} LRESULT CALLBACK InputBoxWndProc(HWND WndHandle, UINT Message, WPARAM wParam, LPARAM lParam){static InputBoxStringType *Results = 0;static HWND InputLabelHandle = 0;static HWND InputHandle = 0;switch(Message){case WM_NCCREATE:{Results = (InputBoxStringType*)((CREATESTRUCT*)lParam)->lpCreateParams;break;}case WM_CLOSE:case WM_DESTROY:{PostQuitMessage(0);break;}case WM_COMMAND:{int NotifyCode = HIWORD(wParam);int ControlId = LOWORD(wParam);HWND ControlHandle = (HWND)lParam;switch(ControlId){case 1001:{InputHandle = ControlHandle;break;}case 1002:{if(InputHandle == 0 || !Results){PostQuitMessage(0);break;}InputBoxCharPointerType Buffer = new InputBoxCharType[256]; GetWindowText(InputHandle, Buffer, 256);*Results = Buffer;PostQuitMessage(0);break;}case 1003:{if(InputHandle == 0 || !Results){PostQuitMessage(0);break;}*Results = InputBoxString("");PostQuitMessage(0);break;}}break;}}return DefWindowProc(WndHandle, Message, wParam, lParam);}InputBoxStringType CInputDialog::ShowInputBox(HWND OwnerWindowHandle, const InputBoxStringType &InputLabel, const InputBoxStringType &InputText, const InputBoxStringType &Title){//WNDCLASS WndClass;HWND DialogHandle = 0;HWND InputLabelHandle = 0;HWND InputHandle = 0;HWND OkButtonHandle = 0, CancelButtonHandle = 0;InputBoxStringType Result = InputBoxString("");//ZeroMemory(&WndClass, sizeof(WNDCLASS));//WndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);//WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);//WndClass.hCursor = LoadCursor(0, IDC_ARROW);//WndClass.hInstance = GetModuleHandle(0);//WndClass.lpszClassName = InputBoxString("InputBox");//WndClass.lpfnWndProc = InputBoxWndProc;WNDCLASSEX stdWndClass;RtlZeroMemory(&stdWndClass, sizeof(stdWndClass));stdWndClass.hCursor = LoadCursor(0,IDC_ARROW);stdWndClass.cbSize = sizeof(stdWndClass);stdWndClass.style = CS_HREDRAW|CS_VREDRAW;stdWndClass.lpfnWndProc = InputBoxWndProc;stdWndClass.hbrBackground = (HBRUSH)COLOR_WINDOWFRAME;stdWndClass.lpszClassName = InputBoxString("InputBox");stdWndClass.hInstance = GetModuleHandle(0);RegisterClassEx(&stdWndClass);//RegisterClass(&WndClass);int X = 0, Y = 0;int Width = 0, Height = 0;RECT TempRect;GetWindowRect(GetDesktopWindow(), &TempRect);Width = (int)((float)(TempRect.right - TempRect.left) * 0.25f);Height = 150;X = ((TempRect.right - TempRect.left) / 2) - (Width / 2);Y = ((TempRect.bottom - TempRect.top) / 2) - (Height / 2);DialogHandle = CreateWindowEx(0, InputBoxString("InputBox"), Title.c_str(), WS_OVERLAPPEDWINDOW  , X, Y, Width, Height, OwnerWindowHandle, 0, GetModuleHandle(0), (LPVOID)&Result);int LabelX = 5, LabelY = 5;int LabelWidth = InputLabel.length()* 20, LabelHeight = 24;InputLabelHandle = CreateWindowEx(0, InputBoxString("STATIC"), InputLabel.c_str(), WS_CHILD | WS_VISIBLE, LabelX, LabelY, LabelWidth, LabelHeight, DialogHandle, 0, GetModuleHandle(0), 0);int TBoxX = 5, TBoxY = 29;int TBoxWidth = Width - 20, TBoxHeight = 24;InputHandle = CreateWindowEx(0, InputBoxString("EDIT"), InputText.c_str(), WS_BORDER | WS_CHILD | WS_VISIBLE, TBoxX, TBoxY, TBoxWidth, TBoxHeight, DialogHandle, (HMENU)1001, GetModuleHandle(0), 0);int OkBtnX = (Width / 2) - 75, OkBtnY = Height - 60;int OkBtnWidth = 75, OkBtnHeight = 24;OkButtonHandle = CreateWindowEx(0, InputBoxString("BUTTON"), InputBoxString("确定"), WS_CHILD | WS_VISIBLE, OkBtnX, OkBtnY, OkBtnWidth, OkBtnHeight, DialogHandle, (HMENU)1002, GetModuleHandle(0), 0);int CancelBtnX = (Width / 2) + 25, CancelBtnY = Height - 60;int CancelBtnWidth = 75, CancelBtnHeight = 24;CancelButtonHandle = CreateWindowEx(0, InputBoxString("BUTTON"), InputBoxString("取消"), WS_CHILD | WS_VISIBLE, CancelBtnX, CancelBtnY, CancelBtnWidth, CancelBtnHeight, DialogHandle, (HMENU)1003, GetModuleHandle(0), 0);if(!DialogHandle)return Result;//在桌面显示窗口ShowWindow(DialogHandle,SW_SHOWNORMAL);//刷新窗口客户区UpdateWindow(DialogHandle);MSG msg;ZeroMemory(&msg,sizeof(msg));while(msg.message != WM_QUIT){if(GetMessage(&msg, 0, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}}return Result;}


 

//InputDialog.h#pragma once#include <windows.h>#include <string>#ifdef UNICODEtypedef std::wstring InputBoxStringType;typedef LPWSTR InputBoxCharPointerType;typedef wchar_t InputBoxCharType;#define InputBoxString(x) L##x#elsetypedef std::string InputBoxStringType;typedef LPSTR InputBoxCharPointerType;typedef char InputBoxCharType;#define InputBoxString(x) x#endifclass CInputDialog{public:CInputDialog(void);~CInputDialog(void);InputBoxStringType CInputDialog::ShowInputBox(HWND OwnerWindowHandle, const InputBoxStringType &InputLabel, const InputBoxStringType &InputText, const InputBoxStringType &Title);};


 

//test#include "InputDialog.h" int _tmain(int argc, _TCHAR* argv[]){CInputDialog dlg;InputBoxStringType Result = dlg.ShowInputBox(0, L"请输入密码:", L"Unknown", L"91wan程序保护");MessageBox(0, Result.c_str(), L"Your name is:", MB_OK);return 0;}