如何发送消息

发布于 2024-10-17 16:40:04 字数 1179 浏览 1 评论 0原文

我有一个问题。我想在我的程序中(遵循代码)有2个窗口:控制台和空表单来输出图形。并从我的 func main 发送消息到 form 来绘制形状。将数据输入到控制台。但 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;
}

谢谢。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-10-24 16:40:11

SendMessage 函数直到消息被窗口处理后才返回。您需要有一个事件循环才能处理消息。 此处查找教程。

在事件循环中,您必须处理两个窗口的消息:控制台窗口和 GUI 窗口。对于控制台消息,您需要处理按键事件,并将自定义消息 (WM_USER + X) 发送到 GUI 窗口。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文