1 #define ID_INPUTEDIT 1001 2 #define ID_OK 1002 3 4 BOOL closing = FALSE; 5 HWND dialogHandle = 0; 6 7 LRESULT CALLBACK inputBoxWndProc(HWND wndHandle, UINT message, WPARAM wParam, LPARAM lParam) 8 { 9 static char **result = 0; 10 static HWND inputHandle = 0; 11 12 switch (message) 13 { 14 case WM_NCCREATE: 15 result = ((CREATESTRUCT *)lParam)->lpCreateParams; 16 break; 17 18 case WM_CLOSE: 19 case WM_DESTROY: 20 closing = TRUE; 21 dialogHandle = 0; 22 PostQuitMessage(0); 23 break; 24 25 case WM_COMMAND: 26 int notifyCode = HIWORD(wParam); 27 int controlId = LOWORD(wParam); 28 HWND controlHandle = (HWND)lParam; 29 30 switch (controlId) 31 { 32 case ID_INPUTEDIT: 33 inputHandle = controlHandle; 34 35 if (notifyCode == EN_UPDATE) 36 { 37 if (*result == 0) 38 *result = malloc(256); 39 GetWindowText(inputHandle, *result, 256); 40 PostQuitMessage(0); 41 } 42 break; 43 44 case ID_OK: 45 SendMessage(wndHandle, WM_CLOSE, 0, 0); 46 break; 47 } 48 break; 49 } 50 51 return DefWindowProc(wndHandle, message, wParam, lParam); 52 } 53 54 WNDCLASS wndClass; // 全局变量自动清0 55 56 void inputbox_init(void) 57 { 58 wndClass.hCursor = LoadCursor(0, IDC_ARROW); 59 // WndClass.hIcon = LoadIcon(0, IDI_APPLICATION); 60 // wndClass.cbSize = sizeof wndClass; 61 wndClass.style = CS_HREDRAW | CS_VREDRAW; 62 wndClass.lpfnWndProc = inputBoxWndProc; 63 wndClass.hbrBackground = (HBRUSH)COLOR_WINDOWFRAME; 64 wndClass.lpszClassName = "InputBox"; 65 wndClass.hInstance = GetModuleHandle(0); 66 67 RegisterClass(&wndClass); 68 } 69 70 HWND dialog_init(char *hint, char **result) 71 { 72 #define X 5 73 #define OKBUTTON_WIDTH 75 74 #define INPUTEDIT_WIDTH (width0 - OKBUTTON_WIDTH) 75 #define INPUTEDIT_Y 29 76 #define HINT_HEIGHT 24 77 #define CHAR_WIDTH 8 78 HWND dialogHandle, inputLabelHandle, inputHandle, okButtonHandle; 79 80 int hint_width = strlen(hint) * CHAR_WIDTH, 81 width0 = hint_width + OKBUTTON_WIDTH, 82 height = HINT_HEIGHT + INPUTEDIT_Y + 50; 83 84 dialogHandle = CreateWindow( 85 wndClass.lpszClassName, "文本输入框的标题", WS_OVERLAPPEDWINDOW, 86 100, 100, width0 + 50, height, 87 0, 0, wndClass.hInstance, result); 88 89 inputLabelHandle = CreateWindow( 90 "STATIC", hint, WS_CHILD | WS_VISIBLE, 91 X, 5, hint_width, HINT_HEIGHT, 92 dialogHandle, 0, wndClass.hInstance, 0); 93 94 inputHandle = CreateWindow( 95 "EDIT", "", WS_BORDER | WS_CHILD | WS_VISIBLE, 96 X, INPUTEDIT_Y, INPUTEDIT_WIDTH, 24, 97 dialogHandle, (HMENU)ID_INPUTEDIT, wndClass.hInstance, 0); 98 99 okButtonHandle = CreateWindow( 100 "BUTTON", "确定按钮", WS_CHILD | WS_VISIBLE, 101 X + INPUTEDIT_WIDTH + 5, INPUTEDIT_Y, OKBUTTON_WIDTH, 24, 102 dialogHandle, (HMENU)ID_OK, wndClass.hInstance, 0); 103 104 return dialogHandle; 105 } 106 107 char *inputbox(char *hint) 108 { 109 static char *result = 0; 110 MSG msg; 111 112 if (dialogHandle == 0) 113 { 114 dialogHandle = dialog_init(hint, &result); 115 116 if (dialogHandle == 0) 117 return 0; 118 } 119 120 // 在桌面显示窗口 121 ShowWindow(dialogHandle, SW_SHOWNORMAL); 122 123 // 刷新窗口客户区 124 // UpdateWindow(dialogHandle); 125 126 while (GetMessage(&msg, 0, 0, 0)) 127 { 128 TranslateMessage(&msg); 129 DispatchMessage(&msg); 130 } 131 132 return result; 133 }