gpt4 book ai didi

c++ - GetWindowText 故障

转载 作者:行者123 更新时间:2023-11-30 05:13:34 29 4
gpt4 key购买 nike

我正在尝试学习如何使用 visual studio 为 C++ 制作 GUI。但是我在使用 GetWindowText() 函数时遇到了一些问题。它不会将 LPTSTR 标题更改为文本框中的文本,并且在调试时我也收到错误消息:“Win32Project1.exe 中的 0x74F8207D (user32.dll) 抛出异常:0xC0000005:访问冲突写入位置 0x002B8D38。”代码可以在下面看到我做错了什么?

#define ID_BUTTON1 1
#define ID_BUTTON2 2
#define ID_TEXT3 3
static HWND hWndTextbox;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HMENU hMenubar = CreateMenu();
HMENU hFile = CreateMenu();
HMENU hOptions = CreateMenu();

AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFile, L"File");
AppendMenu(hMenubar, MF_POPUP, NULL, L"Edit");
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hOptions, L"Options");

AppendMenu(hFile, MF_STRING, NULL, L"Exit");
AppendMenu(hOptions, MF_STRING, NULL, L"option 1");
AppendMenu(hOptions, MF_STRING, NULL, L"option 2");
SetMenu(hWnd, hMenubar);

CreateWindow(TEXT("button"), TEXT("Hello"),
WS_VISIBLE | WS_CHILD,
10, 10, 80, 25,
hWnd, (HMENU) ID_BUTTON1, NULL, NULL);

static HWND hWndTextbox = CreateWindow(TEXT("edit"), TEXT("tekst goes here"),
WS_VISIBLE | WS_CHILD|WS_BORDER | ES_AUTOHSCROLL,
90, 120, 300, 20,
hWnd, (HMENU) ID_TEXT3, NULL, NULL);

CreateWindow(TEXT("button"), TEXT("shiny"),
WS_VISIBLE | WS_CHILD,
50, 50, 80, 50,
hWnd, (HMENU) ID_BUTTON2, NULL, NULL);
}
break;
case WM_COMMAND:
{
if (LOWORD(wParam) == ID_BUTTON1) {
MessageBox(hWnd, L"Button has been clicked", L"title for popup", MB_ICONINFORMATION);
}
if (LOWORD(wParam) == ID_BUTTON2) {
// create some default vars
// int length = GetWindowTextLength(hWndTextbox) + 1;
LPTSTR title = L"test" ;

GetWindowText(hWndTextbox, title, GetWindowTextLength(hWndTextbox) + 1);
MessageBox(hWnd, title, L"Message box", MB_OK);
}
break;
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

其余代码由 visual studio 自动生成,因此我只包含了我进行更改的部分。

最佳答案

GetWindowText() 的文档声明参数 lpString 应该指向一个“将接收文本的缓冲区”,这意味着一个可写 缓冲区。

使用代码 LPTSTR title = L"test"; 可以创建一个指向通常位于只读存储器中的字符串文字的指针。当 GetWindowText() 尝试写入该内存时,会导致访问冲突。

要解决这个问题,请使用这样的可写缓冲区:

// Allocate buffer including terminating null
std::wstring title( GetWindowTextLength(hWndTextbox) + 1, 0 );

// Address of first character is used to obtain pointer to non-const data
// (as opposed to wstring::c_str()).
int size = GetWindowText( hWndTextbox, &title[0], title.size() );

// Resize buffer to the actual text length
title.resize( size );

// MessageBox only needs pointer to const string, so we can use wstring::c_str() here.
MessageBox(hWnd, title.c_str(), L"Message box", MB_OK);

关于c++ - GetWindowText 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43902442/

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