我一直在尝试学习如何使用 Windows API 制作 C++ 应用程序。我尝试使用一个简单的函数 ,SetConsoleTitle()来设置控制台窗口的标题。这是我的程序:
#include <iostream>
#include <tchar.h>
#include <windows.h>
int main(){
TCHAR abc[5];
abc[0] = 'H';
abc[1] = 'E';
abc[2] = 'L';
abc[3] = 'L';
abc[4] = 'O';
SetConsoleTitle(abc);
std::cout << "Hello World\n";
system("pause > nul");
}
Run Code Online (Sandbox Code Playgroud)
这是结果
标题应该是"HELLO". 其他角色在那里做什么,我该如何摆脱他们?
我使用 Visual Studio Code 2019 来编译这段代码。
正如@AlanBirtles 所说,您需要空终止您的字符串。这是 C++ 知道字符串有多长的唯一方法。有三种方法可以做到这一点:
TCHAR abc[6]; // <-- increase this size
...
abc[5] = '\0'; // <-- add this
Run Code Online (Sandbox Code Playgroud)
TCHAR abc[] = "HELLO"; // the terminator will be added automatically
Run Code Online (Sandbox Code Playgroud)
TCHAR abc[6] = {0}; // the string will consist of all nulls and you can then overwrite just the first 5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |