简体   繁体   English

如何获取控制文本?

[英]How to get control text?

I want to get control text.我想获得控制文本。 but this code return parent class name.但此代码返回父类名。

const wchar_t* textInput = L"Login";   

HWND btnHandle = CreateWindowEx(0, L"BUTTON", textInput, WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                 mCoordinate.x, mCoordinate.y, mDimension.cx, mDimension.cy, parentHandle, NULL, 
                 (HINSTANCE)GetWindowLongPtr(parentHandle, GWLP_HINSTANCE), NULL);

wchar_t* textOutput;
int length = GetWindowTextLengthW(btnHandle);
GetWindowText(btnHandle, textOutput, length);

MessageBox(NULL, textOutput, L"Window Text", MB_OK);

As documented, the lpString argument to GetWindowTextW is:如文档所述, GetWindowTextWlpString参数是:

The buffer that will receive the text.将接收文本的缓冲区。

The API does not provide that buffer for you. API 不会为您提供该缓冲区。 Instead, you will have to pass it in, as illustrated here:相反,您必须将其传入,如下所示:

size_t length{ GetWindowTextLengthW(btnHandle) };
// Allocates a buffer for `length` characters plus a NUL terminator
std::wstring text(length, L'\0');
// The API promises to write a NUL terminator into the final character
// so it is safe to lie about the length
length = GetWindowTextW(btnHandle, text.data(), text.size() + 1);
// Resize in case we got less than promised
text.resize(length);

MessageBoxW(NULL, text.c_str(), L"Window Text", MB_OK);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM