Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将控制台输入文本移到下方_C++_C_Windows_Winapi - Fatal编程技术网

C++ 将控制台输入文本移到下方

C++ 将控制台输入文本移到下方,c++,c,windows,winapi,C++,C,Windows,Winapi,问题: 我有一个控制台聊天。当我输入一条消息,而另一个人发送一条消息时,他们的消息会在我输入的最后一个字符后立即打印出来,并将其打断 例如: 我尝试键入“你好!”,但“安德鲁”会干扰“你好!” 我想如何处理这个问题: 我在想,在打印来自另一个客户端的消息之前,我会复制我当前在标准输入中的文本,打印其他用户的文本,然后将文本放回标准输入中,下一行 我尝试使用ReadConsoleInput/WriteConsoleInput API,但它没有达到我想要的效果,并且增加了显著的(>10秒)延迟 我无

问题:

我有一个控制台聊天。当我输入一条消息,而另一个人发送一条消息时,他们的消息会在我输入的最后一个字符后立即打印出来,并将其打断

例如:

我尝试键入“你好!”,但“安德鲁”会干扰“你好!”

我想如何处理这个问题:

我在想,在打印来自另一个客户端的消息之前,我会复制我当前在标准输入中的文本,打印其他用户的文本,然后将文本放回标准输入中,下一行

我尝试使用ReadConsoleInput/WriteConsoleInput API,但它没有达到我想要的效果,并且增加了显著的(>10秒)延迟

我无法在MSDN上找到我可以使用的任何其他控制台API。也许我用错了上面的其他方法


如果您能帮我使用这些,我将不胜感激。谢谢

在研究了你的问题后,我做了以下测试,可以得到预期的效果

读取另一个进程字符串后,使用在不同位置写入字符

 WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString), { 0,0 }, &dwWritten);
光标的坐标可以根据实际情况进行修改,如实现消息滚动

注意:您可以使用
setConsoleorPosition
定位到另一个位置以首先接受输入流

像这样,

coord.X = 0;
coord.Y = 10;
SetConsoleCursorPosition(hStdOut, coord);
ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);

更新:

这是我测试过的样品,我附上了一个快速的标签

Server.cpp

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>

using namespace std;

DWORD dwWritten;
DWORD num;
DWORD dwRead;
char strMessage[256];
HWND h_client;
void td1();
COORD coord;
SHORT i = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    if (message == WM_DESTROY) {

        PostQuitMessage(0);
    }
    if (message == WM_COPYDATA)
    {        
        COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
        if (pcds->dwData == 1)
        {
            CHAR* lpszString = (CHAR*)(pcds->lpData);
            WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString)-2, { 0,i++ }, &dwWritten); 
        }
        if (i > 24)
        {
            system("cls");
            i = 0;
            coord.X = 0;
            coord.Y = 25;
            SetConsoleCursorPosition(hStdOut, coord);
            printf("----------------------------------------------------------------------------------------------------------\n");
        }
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
};

HINSTANCE hinst;

int main() {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"win32";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow(L"win32", L"Server", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    std::thread t1(td1);
    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);


    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

void td1()
{
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    while (1)
    {
        h_client = FindWindow(L"win32", L"Client");
        if (h_client)
        {
            break;
        }
    }  
    coord.X = 0;
    coord.Y = 25;
    SetConsoleCursorPosition(hStdOut, coord);
    printf("----------------------------------------------------------------------------------------------------------");
    while (1)
    {
        coord.X = 0;
        coord.Y = 26;
        SetConsoleCursorPosition(hStdOut, coord);
        memset(strMessage, 0, 256);
        ReadFile(hStdIn, strMessage, 256, &dwRead, NULL); 
        SetConsoleCursorPosition(hStdOut, coord);
        for (int i = strlen(strMessage); i > 0; i--)
        {
            putchar(32);
        }  
        COPYDATASTRUCT cds;
        cds.dwData = 1; // can be anything
        cds.cbData = sizeof(CHAR) * strlen(strMessage);
        cds.lpData = strMessage;
        SendMessage(h_client, WM_COPYDATA, (WPARAM)h_client, (LPARAM)(LPVOID)&cds);        
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
德沃德·德沃茨;
德沃德·努姆;
德沃德·德雷德;
字符strMessage[256];
HWND h_客户;
void td1();
合作社;
短i=0;
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM){
句柄hStdOut=GetStdHandle(标准输出句柄);
句柄hStdIn=GetStdHandle(标准输入句柄);
如果(消息==WM_销毁){
PostQuitMessage(0);
}
如果(消息==WM\U COPYDATA)
{        
CopyDataStrut*pcds=(CopyDataStrut*)LPRAM;
如果(pcds->dwData==1)
{
CHAR*lpszString=(CHAR*)(pcds->lpData);
WriteConsoleOutputCharacterA(hstOut、lpszString、strlen(lpszString)-2、{0、i++}、&dwWrite);
}
如果(i>24)
{
系统(“cls”);
i=0;
坐标X=0;
坐标Y=25;
设置控制台位置(HST输出,坐标);
printf(“-----------------------------------------------------------------------------------------------------------------\n”);
}
}
返回DefWindowProc(hwnd、message、wParam、lParam);
};
HINSTANCE hinst;
int main(){
HWND-HWND;
hinst=GetModuleHandle(NULL);
//创建窗口类:
WNDCLASS wc={};
wc.lpfnWndProc=WndProc;
wc.hInstance=hinst;
wc.lpszClassName=L“win32”;
//向操作系统注册类:
注册类(&wc);
//创建并显示窗口:
hwnd=CreateWindow(L“win32”,L“服务器”,0,0,0,0,hwnd_消息,NULL,NULL,NULL);
标准:螺纹t1(td1);
if(hwnd==NULL){
返回0;
}
展示窗口(hwnd、SW_展示);
MSG={};
while(GetMessage(&msg,NULL,0,0)){
翻译信息(&msg);
发送消息(&msg);
}
}
无效td1()
{
句柄hStdIn=GetStdHandle(标准输入句柄);
句柄hStdOut=GetStdHandle(标准输出句柄);
而(1)
{
h_client=FindWindow(L“win32”,L“client”);
if(h_客户机)
{
打破
}
}  
坐标X=0;
坐标Y=25;
设置控制台位置(HST输出,坐标);
printf(“-------------------------------------------------------------------------------------------------------------------------”;
而(1)
{
坐标X=0;
坐标Y=26;
设置控制台位置(HST输出,坐标);
memset(strMessage,0256);
ReadFile(hStdIn,strMessage,256,&dwRead,NULL);
设置控制台位置(HST输出,坐标);
对于(int i=strlen(strMessage);i>0;i--)
{
普查尔(32);
}  
复制数据结构光盘;
cds.dwData=1;//可以是任何内容
cds.cbData=sizeof(CHAR)*strlen(strMessage);
cds.lpData=strMessage;
SendMessage(h_客户端、WM_COPYDATA、(WPARAM)h_客户端、(LPRAM)(LPVOID)和CD);
}
}
Client.cpp

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>

using namespace std;

DWORD dwWritten;
DWORD dwRead;
char strMessage[256];
HWND h_server;
SHORT i = 0;
COORD coord;
void td1();

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    if (message == WM_DESTROY) {

        PostQuitMessage(0);
    }
    if (message == WM_COPYDATA)
    {       
        COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
        if (pcds->dwData == 1)
        {
            CHAR* lpszString = (CHAR*)(pcds->lpData);
            int len = pcds->cbData;
            WriteConsoleOutputCharacterA(hStdOut, lpszString, len - 2, { 0,i++ }, &dwWritten);
        }
        if (i > 24)
        {
            system("cls");
            i = 0;        
            coord.X = 0;
            coord.Y = 25;
            SetConsoleCursorPosition(hStdOut, coord);
            printf("----------------------------------------------------------------------------------------------------------\n");
        }
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

HINSTANCE hinst;

int main() {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"win32";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow(L"win32", L"Client", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    std::thread t1(td1);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);


    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

void td1()
{
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    while (1)
    {
        h_server = FindWindow(L"win32", L"Server");
        if (h_server)
        {
            break;
        }
    }
    coord.X = 0;
    coord.Y = 25;
    SetConsoleCursorPosition(hStdOut, coord);
    printf("----------------------------------------------------------------------------------------------------------");
    while (1)
    {              
        coord.X = 0;
        coord.Y = 26;
        SetConsoleCursorPosition(hStdOut, coord);
        memset(strMessage, 0, 256);
        ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);  
        SetConsoleCursorPosition(hStdOut, coord);
        for (int i = strlen(strMessage); i > 0; i--)
        {
            putchar(32);
        }
        COPYDATASTRUCT cds;
        cds.dwData = 1; // can be anything
        cds.cbData = sizeof(CHAR) * (strlen(strMessage) + 1);
        cds.lpData = strMessage;
        SendMessage(h_server, WM_COPYDATA, (WPARAM)h_server, (LPARAM)(LPVOID)&cds);
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
德沃德·德沃茨;
德沃德·德雷德;
字符strMessage[256];
HWND-h_服务器;
短i=0;
合作社;
void td1();
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM){
句柄hStdOut=GetStdHandle(标准输出句柄);
句柄hStdIn=GetStdHandle(标准输入句柄);
如果(消息==WM_销毁){
PostQuitMessage(0);
}
如果(消息==WM\U COPYDATA)
{       
COPYDATASTRUCT*pcds=(COPYDATASTRUCT*)LPRAM;
如果(pcds->dwData==1)
{
CHAR*lpszString=(CHAR*)(pcds->lpData);
int len=pcds->cbData;
WriteConsoleOutputCharacterA(hStdOut、lpszString、len-2、{0、i++}、&dwwrite);
}
如果(i>24)
{
系统(“cls”);
i=0;
坐标X=0;
坐标Y=25;
设置控制台位置(HST输出,坐标);
printf(“-----------------------------------------------------------------------------------------------------------------\n”);
}
}
返回DefWindowProc(hwnd、message、wParam、lParam);
}
HINSTANCE hinst;
int main(){
HWND-HWND;
hinst=GetModuleHandle(NULL);
//创建窗口类:
WNDCLASS wc={};
wc.lpfnWndProc=WndProc;
wc.hInstance=hinst;
wc.lpszClassName=L“win32”;
//向操作系统注册类:
注册类(&wc);
//创建并显示窗口:
hwnd=CreateWindow(L“win32”,L“Client”,0,0,0,0,hwnd_消息,NULL,NULL,NULL);
标准:螺纹t1(td1);
如果(hwnd)=NU
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>

using namespace std;

DWORD dwWritten;
DWORD num;
DWORD dwRead;
char strMessage[256];
HWND h_client;
void td1();
COORD coord;
SHORT i = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    if (message == WM_DESTROY) {

        PostQuitMessage(0);
    }
    if (message == WM_COPYDATA)
    {        
        COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
        if (pcds->dwData == 1)
        {
            CHAR* lpszString = (CHAR*)(pcds->lpData);
            WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString)-2, { 0,i++ }, &dwWritten); 
        }
        if (i > 24)
        {
            system("cls");
            i = 0;
            coord.X = 0;
            coord.Y = 25;
            SetConsoleCursorPosition(hStdOut, coord);
            printf("----------------------------------------------------------------------------------------------------------\n");
        }
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
};

HINSTANCE hinst;

int main() {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"win32";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow(L"win32", L"Server", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    std::thread t1(td1);
    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);


    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

void td1()
{
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    while (1)
    {
        h_client = FindWindow(L"win32", L"Client");
        if (h_client)
        {
            break;
        }
    }  
    coord.X = 0;
    coord.Y = 25;
    SetConsoleCursorPosition(hStdOut, coord);
    printf("----------------------------------------------------------------------------------------------------------");
    while (1)
    {
        coord.X = 0;
        coord.Y = 26;
        SetConsoleCursorPosition(hStdOut, coord);
        memset(strMessage, 0, 256);
        ReadFile(hStdIn, strMessage, 256, &dwRead, NULL); 
        SetConsoleCursorPosition(hStdOut, coord);
        for (int i = strlen(strMessage); i > 0; i--)
        {
            putchar(32);
        }  
        COPYDATASTRUCT cds;
        cds.dwData = 1; // can be anything
        cds.cbData = sizeof(CHAR) * strlen(strMessage);
        cds.lpData = strMessage;
        SendMessage(h_client, WM_COPYDATA, (WPARAM)h_client, (LPARAM)(LPVOID)&cds);        
    }
}
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>

using namespace std;

DWORD dwWritten;
DWORD dwRead;
char strMessage[256];
HWND h_server;
SHORT i = 0;
COORD coord;
void td1();

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    if (message == WM_DESTROY) {

        PostQuitMessage(0);
    }
    if (message == WM_COPYDATA)
    {       
        COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
        if (pcds->dwData == 1)
        {
            CHAR* lpszString = (CHAR*)(pcds->lpData);
            int len = pcds->cbData;
            WriteConsoleOutputCharacterA(hStdOut, lpszString, len - 2, { 0,i++ }, &dwWritten);
        }
        if (i > 24)
        {
            system("cls");
            i = 0;        
            coord.X = 0;
            coord.Y = 25;
            SetConsoleCursorPosition(hStdOut, coord);
            printf("----------------------------------------------------------------------------------------------------------\n");
        }
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

HINSTANCE hinst;

int main() {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"win32";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow(L"win32", L"Client", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    std::thread t1(td1);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);


    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

void td1()
{
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    while (1)
    {
        h_server = FindWindow(L"win32", L"Server");
        if (h_server)
        {
            break;
        }
    }
    coord.X = 0;
    coord.Y = 25;
    SetConsoleCursorPosition(hStdOut, coord);
    printf("----------------------------------------------------------------------------------------------------------");
    while (1)
    {              
        coord.X = 0;
        coord.Y = 26;
        SetConsoleCursorPosition(hStdOut, coord);
        memset(strMessage, 0, 256);
        ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);  
        SetConsoleCursorPosition(hStdOut, coord);
        for (int i = strlen(strMessage); i > 0; i--)
        {
            putchar(32);
        }
        COPYDATASTRUCT cds;
        cds.dwData = 1; // can be anything
        cds.cbData = sizeof(CHAR) * (strlen(strMessage) + 1);
        cds.lpData = strMessage;
        SendMessage(h_server, WM_COPYDATA, (WPARAM)h_server, (LPARAM)(LPVOID)&cds);
    }
}