研究了你的问题,我做了如下测试,可以得到预期的效果。
读取另一个进程字符串后,使用WriteConsoleOutputCharacter在不同位置写入字符。
WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString), { 0,0 }, &dwWritten);
光标的坐标可以根据实际情况进行修改,比如实现消息滚动。
注意:您可以先使用SetConsoleCursorPosition定位到另一个位置接受输入流。
这样,
coord.X = 0;
coord.Y = 10;
SetConsoleCursorPosition(hStdOut, coord);
ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);
更新:
这是我测试的示例,我附上一个快速gif demo。
服务器.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);
}
}
客户端.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);
}
}
其实客户端和服务器端的代码几乎是一样的。我通过发送WM_COPYDATA 消息做到了。
这仅适用于同一台计算机上的两个不同进程。两台机器的控制台聊天,需要依赖TCP协议。