windows 如何发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4983561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to SendMessage
提问by kill
I have a problem. I want in my program(follow code) have 2 windows: console and empty form to output graphics. And from my func main send messages to form to draw shapes. Input data to console. But func SendMessage() doesn't work. What wrong?
我有个问题。我希望在我的程序(遵循代码)中有 2 个窗口:控制台和空窗体来输出图形。并从我的 func main 发送消息到表单以绘制形状。将数据输入到控制台。但是 func SendMessage() 不起作用。什么错?
int main()
{
char szClassName[] = "CG_WAPI_Template";
HWND hWnd = GetConsoleWindow();
HINSTANCE hInstance = NULL;
MSG lpMsg;
if(!AllocConsole())
MessageBox(NULL, "Failed to create the console!", "Ошибка", MB_ICONEXCLAMATION|MB_OK);
void *h_inc = GetStdHandle(STD_INPUT_HANDLE);
void *h_out = GetStdHandle(STD_OUTPUT_HANDLE);
WNDCLASS wc;
/*wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
...
*/
if(!RegisterClass(&wc))
{MessageBox(NULL, "Не могу зарегистрировать класс окна!", "Ошибка", MB_OK);
return 0;
}
hWnd = CreateWindow(...);
ShowWindow(hWnd, SW_MAXIMIZE);
UpdateWindow(hWnd);
char buf[2];
unsigned long lengh;
ReadConsole(h_inc,buf,1,&lengh,NULL);
SendMessage(hWnd, WM_USER+2, 0, 0);
if(GetMessage(&lpMsg, NULL, 0, 0))
{
TranslateMessage(&lpMsg);
DispatchMessage(&lpMsg);
}
ReadConsole(h_inc,buf,1,&lengh,NULL);
if (!FreeConsole())
MessageBox(NULL, "Could not free the console!", "Ошибка", MB_OK);
return 0;
}
Thank you.
谢谢你。
回答by ak.
SendMessage function does not return until the message is processed by the window. You need to have an event loop in order to handle messages. Look fo r a tutorial here.
SendMessage 函数在消息被窗口处理之前不会返回。您需要有一个事件循环才能处理消息。在这里寻找 ra 教程。
In your event loop you will have to handle messages for two windows: for the console window and for the GUI winodow. For the console messages you will need to handle the key press events, and send your custom message (WM_USER + X) to the GUI window.
在您的事件循环中,您必须处理两个窗口的消息:控制台窗口和 GUI 窗口。对于控制台消息,您需要处理按键事件,并将您的自定义消息 (WM_USER + X) 发送到 GUI 窗口。