Create a Windows window that receives messages

Source: Internet
Author: User

I think it's time to get familiar with Windows programming, or else the project code won't look. today at home, implemented a simple Windows window that receives messages that can receive messages sent over by other processes.

Windows programming begins with a basic framework for a Windows program that creates a visual window with a lot of steps. If you create a window that is only for receiving messages, it is much simpler and does not require us to register a custom wndclass or display the interface.

Let's take a look at CreateWindow's help documentation:

Http://msdn.microsoft.com/en-us/library/ms632679.aspx

Lpclassname can register themselves, can also be used in the system predefined, I use static in the program, if this parameter is filled with a nonexistent classname, then CreateWindow will return null. The user can customize the message, needs to be defined from Wm_user, and the process that processes the message and sends the message needs to know the definition. WndProc is a window processing function that is registered with the window through SetWindowLong. I wrote WndProc only processed Wm_user + 100 and Wm_user + 200 messages, and other messages were delegated to DefWindowProc processing.

#include  <tchar.h> #include  <windows.h> #include  <iostream>using namespace  std; lresult callback wndproc  (Hwnd hwnd, uint message, wparam wparam,  lparam lparam) {tchar sztext[max_path] = {0};_stprintf (SzText, _T ("wParam=%d  lparam=%d "),  wparam, lparam); switch (message) {Case wm_user + 100:o Utputdebugstring (_t ("wm_user + 100")); OutputDebugString (sztext); return 0;case wm_user  + 200:outputdebugstring (_t ("wm_user + 200")); OutputDebugString (SzText); PostQuitMessage (0); Return 0;default:outputdebugstring (_t ("Default")); OutputDebugString (sztext); break;} return defwindowproc  (Hwnd, message, wparam, lparam);} int winapi winmain  (hinstance hinstance, hinstance hprevinstance,pstr  Szcmdline, int icmdshow) {Hwnd hwnd = createwindow (_t ("Static"), _t (" Msgrecv "), Ws_overlappedwindow,,cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault,null,null,null, NULL), if (Hwnd != null && iswindow (hWnd)) {SetWindowLong (Hwnd, gwl_wndproc,   (LONG)  wndproc);} msg msg;while  (getmessage  (&msg, null, 0, 0)) {TranslateMessage  (& msg);D ispatchmessage  (&msg);} Return msg.wparam;}

The process implementation of sending the message is relatively simple, findwindow find the window handle first, then SendMessage. In the following code, I send a wm_user + 100 message every 10ms, send 100 times, I send a Wm_user + 200 let the window to receive messages exit.

#include <windows.h> #include <tchar.h> #include <iostream>using namespace Std;int main () {HWND hwnd =: : FindWindow (_t ("Static"), _t ("Msgrecv")), if (HWND) {for (int i = 1; i <=; i++) {:: SendMessage (hwnd, Wm_user + +, WP Aram (i), NULL); Sleep (hWnd, Wm_user +}::sendmessage, NULL, NULL), cout << "Finish send" << Endl;} Else{cout << "hwnd is null" << Endl;} return 0;}

Such a window program to receive the message is complete. Run the process of creating the window first, then run the process that sends the message, and you can see the effect in DebugView.

This example has many scenarios, such as the need to display the progress of a scan thread in the client UI, where the scanning process is reporting progress with SendMessage, the UI thread creates a window to receive progress information, and a UI-related display in WndProc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.