gpt4 book ai didi

c++ - 获取 TextBox Value 并将其显示为 MessageBox WinApi

转载 作者:行者123 更新时间:2023-11-30 01:16:06 25 4
gpt4 key购买 nike

当我在 TextBox 中输入一个值然后单击按钮时,MessageBox 必须显示输入的值而不是空的 MessageBox。那么如何让点击按钮后输入的值显示在MessageBox上呢?

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

HWND TextBox;
char textSaved[20];
switch(Message) {
case WM_CREATE:

TextBox = CreateWindow("EDIT",
"",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10,10,300,20,
hwnd, NULL, NULL, NULL);
CreateWindow("BUTTON",
"Go",
WS_VISIBLE | WS_CHILD | WS_BORDER,
420, 10, 70, 20,
hwnd, (HMENU) 1, NULL, NULL);

break;

case WM_COMMAND:

switch(LOWORD(wParam))
{
case 1:

int gwtstat =0;
gwtstat = GetWindowText(TextBox, &textSaved[0], 20);
::MessageBox(hwnd, textSaved, textSaved, MB_OK);
break;
}

break;

case WM_DESTROY: { PostQuitMessage(0);break;}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));
wc.hbrBackground = CreateSolidBrush(RGB(20, 60, 12));//(brush)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);

if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}

while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

最佳答案

首先您需要获取文本框文本,在您的情况下它没有 id,然后使用此代码创建带有 id 的文本框,因为在您的情况下每次调用窗口过程时 HWND TextBox 都会重置:

 CreateWindowW(L"EDIT", L"", WS_BORDER | WS_CHILD | WS_VISIBLE, 10,10,300,20, hwnd, (HMENU)2, NULL, NULL);

现在您的文本框有一个 id,我们不需要 HWND TextBox,使用 GetDlgItem 获取 TextBox,使用 GetWindowTextW 获取 TextBox 的文本并存储在 wchar_t 数组中:

case 1:
int len = GetWindowTextLengthW(GetDlgItem(hwnd, 2)) + 1;
wchar_t text[len];
GetWindowTextW(GetDlgItem(hwnd, 2), text, len);
MessageBoxW(NULL, text, L"MsgFromTextBox", MB_OK);

关于c++ - 获取 TextBox Value 并将其显示为 MessageBox WinApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27260843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com