图形界面接收不到键盘输入
VS下的图形界面,想通过键盘的输入接受到用户名,再反显到图形界面中,但是用这段代码实现不了,没想明白问题在哪,求助大佬们
#include <stdio.h>
int main()
{
/*输入用户名*/
int i, j = 0, k = 0;
char ch;
char s[2];
MOUSEMSG m;//定义鼠标信息
FlushMouseMsgBuffer();
m = GetMouseMsg();//获取一条鼠标消息
while(1){
while (_kbhit()) { //检测是否有键盘输入
ch = _getch();
sprintf_s(s, "%c", ch);
setbkmode(TRANSPARENT);
settextcolor(WHITE);
settextstyle(20, 0, _T("黑体"));
outtextxy(230 + k, 270, s);
k += 15;
j++;
}
}
}
1. 你没有创建绘图窗口,所以你看不到任何输出。
2. 你的代码,使用的 MBCS 编码。建议使用 Unicode 编码,用 wchar_t 类型字符存储用户输入,这样能接受用户输入汉字。
以下例子,可以实现将用户输入显示到屏幕上,允许中文和英文输入。
#include <easyx.h>
#include <conio.h>
int main()
{
initgraph(640, 480);
wchar_t ch;
settextstyle(16, 0, _T("宋体"));
int x = 0;
while (x < 640)
{
ch = _getwch();
outtextxy(x, 100, ch);
x += (ch < 256) ? 8 : 16;
}
closegraph();
return 0;
}