我想知道是否可以从文本框中输入文本输入(CreateWindowEx“EDIT”)并将其存储为字符串或更好的矢量
我需要一个用户可以输入或粘贴文本的文本框,当我点击一个按钮时,它会按字母顺序排列单词并计算独特的单词等...
到目前为止,我已将其作为字符读取(我不知道如何使其成为字符串)并按字母顺序排列字符
所以,如果我输入:如何现在棕色的牛 它会输出:bchnnoooorwwww 而不是:棕色牛现在如何
我在WM_COMMAND案例下的代码是
int length;
length = GetWindowTextLength(textbox) + 1;
vector<wchar_t> list(length);
GetWindowText(textbox, &list[0], length);
wstring stxt = &list[0];
wstring str(stxt);
sort(str.begin(), str.end());
SetWindowText(sortedOutput, &str[0]);
答案 0 :(得分:1)
这个答案可能对你设计解决方案有用。我真的不知道一个不是hacky的,但它可以从std :: string中完成c_string()的const的强制转换。
https://stackoverflow.com/a/1986974/128581
在C ++ 98/03标准下,std :: string的分配不保证是连续的,但C ++ 11强制它。在实践中,我和Herb Sutter都不知道不使用连续存储的实现。
请注意,即使在0长度字符串的情况下,&amp; s [0]总是保证可以通过C ++ 11标准工作。如果你使用str.begin()或&amp; * str.begin(),则不能保证,但对于&amp; s [0],标准将operator []定义为:
如果pos&lt;返回:*(begin()+ pos) size(),否则引用类型为T的对象,其值为charT();参考值不得修改 继续,data()定义为:
返回:指针p,使得[0,size()]中的每个i的p + i ==&amp;运算符。 (注意范围两端的方括号)
因此你可以做到这样的事情:
int len = GetWindowTextLength(hwnd) + 1;
std::string s;
s.reserve(len);
GetWindowText(hwnd, const_cast<char*>(s.c_str()), len - 1);
哪个很难看。不过,欢迎使用更多“正确”的答案。
关于在构建时启用unicode的情况,您必须使用wstring或等效代码。刚才测试一下,这可行:
std::wstring title;
title.reserve(GetWindowTextLength(m_window_handle) + 1);
GetWindowText(m_window_handle, const_cast<WCHAR *>(title.c_str()), title.capacity());
一般来说,对于windows api来说,谷歌的全部大写类型定义是有用的,并弄清楚它们到底是什么。
关于拆分字符串,std :: string并不特别擅长这种操作。这是std :: stringstream(或unicode的wstringstream)派上用场的地方。我相当肯定stringstream不能保证在内存中连续,所以你不能真正地直接写入它的缓冲区。
// Initialize a stringstream so we can extract input using >> operator
std::wstringstream ss;
ss.str(title);
// Make a vector, so we can store our words as we're extracting them
// and so we can use sort later, which works on many stl containers
std::vector<std::wstring> words;
std::wstring word;
// This will evaluate to false and thus end the loop when its done
// reading the string word by word
while(ss >> word)
{
words.push_back(word);
}
然后继续排序,但是在新的矢量字上。
答案 1 :(得分:0)
您的问题不是winapi问题。虽然不是唯一的方法,但您找到了一种解决方案,可以在您的编辑框中来回传输字符串。
如何将该字符串转换为字符串的列表/向量,其中单词是该列表/向量的元素实际上是STL问题。
基本上,您正在寻找C#函数 var suggestions =
Rx.Observable.fromEvent(textInput, 'keyup')
.pluck('target','value')
.filter( (text) => {
text = text.trim();
if (!text.length) // empty input field
{
this.username_validation_display("empty");
}
else if (!/^\w{1,20}$/.test(text))
{
this.username_validation_display("invalid");
return false;
}
})
.debounceTime(300)
.distinctUntilChanged()
.switchMap(text=> {
if (text.length > 0) {
return $.ajax({
type: "post",
url: "src/php/search.php",
data: {
username: text,
type: "username"
}
}).promise()}
return Rx.Observable.empty();
}
);
的C ++等价物。
这里有一个很好的问题和答案,在这里:
Most elegant way to split a string?
所以,你要做的只是一次往返:
String.Split()
(请参阅另一个问题,了解如何执行此操作)。std::vector<string>
)。根据您对多字符串和全球化的偏好,您可能会首先决定坚持使用ASCII版本。在Windows上,您可以编译为MBCS或ASCII。然后,相应的字符串类型分别为(std::ostringstream
和TCHAR
或LPCTSTR
和WCHAR
或LPCWSTR
和CHAR
)。所有win32函数都有两种形式,分别以函数名末尾的尾随LPCSTR
或A
来区分。
AFAIK,虽然有W
和std::string
,但std::wstring
没有标准实现,它可以与您的编译选项一起使用。
至于窗口处理,这里有一些代码示例(片段):
在std::basic_string<TCHAR>
中,我使用输入编辑框,我的按钮和静态输出区域创建了对话框(IDD_FORMVIEW),作为主应用程序窗口的子窗口:
InitInstance()
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
HWND hWndChild = CreateDialogW(hInstance, MAKEINTRESOURCE(IDD_FORMVIEW), hWnd, dlgProc);
if (NULL == hWndChild)
{
DWORD lastError = GetLastError();
wchar_t msg[100];
swprintf_s(msg, _countof(msg), L"error code: 0x%0X\n", lastError);
OutputDebugString(msg);
}
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
ShowWindow(hWndChild, SW_SHOW);
UpdateWindow(hWnd);
return TRUE;
}
将指向对话句柄函数的指针作为最后一个参数,在本例中称为CreateDialogW()
。
这就是dlgProc()
函数的样子:
dlgProc()
答案 2 :(得分:0)
我刚刚混合了几行代码,将wchar_t
转换为wstring
转换为std::string
。来啦!
string GetWindowStringText(HWND hwnd)
{
int len = GetWindowTextLength(hwnd) + 1;
vector<wchar_t> buf(len);
GetWindowText(hwnd, &buf[0], len);
wstring wide = &buf[0];
string s(wide.begin(), wide.end());
return s;
}
其中有一个vector
,因此您需要包含它。