VC++:在文本框中插入文本,不重复

VC++:在文本框中插入文本,不重复,c++,visual-c++,C++,Visual C++,我很难在文本框或RichtextBox中插入文本而不重复 使用以下代码: #include "windows.h" #pragma comment(lib, "user32") void actwnd() { wchar_t lastwindow[MAX_PATH]; wchar_t currentwindow[MAX_PATH]; HWND mainwindow; mainwindow = GetForegroundWind

我很难在文本框或RichtextBox中插入文本而不重复

使用以下代码:

#include "windows.h" #pragma comment(lib, "user32") void actwnd() { wchar_t lastwindow[MAX_PATH]; wchar_t currentwindow[MAX_PATH]; HWND mainwindow; mainwindow = GetForegroundWindow(); GetWindowText(mainwindow,currentwindow,sizeof(currentwindow)); if(lastwindow==currentwindow) { } else if(lastwindow!=currentwindow) { strcpy ((char*)lastwindow,(char*)currentwindow); String^ strNew = gcnew String(currentwindow); // String^ wnd = gcnew String(reinterpret_cast(currentwindow)); textBox1->Text += strNew; } } // Set interval for 1000ms in test private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { actwnd(); }
任何帮助都将不胜感激

iflastwindow==currentwindow不比较字符串,而是比较数组的地址,因此它们永远不会相等。使用wcscmp比较wchar\t数组。由于这些是wchar_t数组,因此使用strcpy复制它们也是无效的,strcpy仅用于char数组。改用wcscpy。

谢谢@ScottMcP MVP。根据您的提示,我实现了对follow way=>ifwcscmplastwindow,currentwindow进行一些更改=0{wcscpy lastwindow,currentwindow;String ^strNew=gcnew-Stringcurrentwindow;textBox1->Text+=strNew;}。现在工作正常!