How to capture VCL Windows messages that are not processed

Source: Internet
Author: User
Tags contains table definition

----C + + Builer VCL provides a processing mechanism for most Windows messages, which is sufficient for a generic application, but VCL is not all-encompassing, and how do you capture Windows messages that are not handled by VCL? C + + Builder uses a message-map mechanism to link a particular Windows message to a function in the code through a message image table, and this function is called when the window captures the message, which is very similar to the event handle.

The----C + + Builder message image table definition forms the following:

BEGIN_MESSAGE_MAP
    MESSAGE_HANDLER(< message >,
< message structure >,< message handler >)
    ……
    MESSAGE_HANDLER(< message >,
< message structure >,< message handler >)
   END_MESSAGE_MAP(ClassName)

----classname is the underlying class name, the message parameter is the Windows message to be captured, and the messages structure parameter is used to pass VCL the parsed structure name used to pass the parameter after parsing the message, which contains all the parameters needed to process the message. Different messages have different message profiling structures, and the general message profiling structure is defined as follows:

struct Tmessage
{
  unsigned int Msg;//Windows消息
  long Wparam;
  long Lparam;
  long Result;
}

----The key here is the result data member, which is used to set the return value after the message is processed, and the program can handle it according to the different return values. The message handler parameter is the name of the function that handles the messages.

----as the saying goes: thousand words inferior to a picture. Here's an example to illustrate, let's drag an untitled window by capturing the WM_NCHITTEST message and sending a message when the mouse clicks the client area of the form so that Windows thinks it is on the title bar of the form.

----Create a new project, save the unit file as Mainform.cpp, and save the project file as NOCAPTION.BPR. First you have to create a form without a title bar, set the Form1 bordericons collection to empty, delete the window's system menu, Maximize and Minimize buttons, and set the BorderStyle property to Bsdialog. Note that all C + + Builder forms that you create at design time have a title bar, and you can't simply get a untitled form by setting the Form1 Caption property to null as in VB, you have to overload the CreateParams function in the Tform derived class, Remove the corresponding window identification bit and delete the title bar of the form at run time. Open the Mainform.h file and add the CreateParams prototype to the private segment:

void __fastcall createparams (tcreateparams& Params);

The TCREATEPARAMS structure contains various parameters for creating a form,

Here we want to shield off the ws_caption bit.

Add the following code to the Mainform.cpp:

void __fastcall TForm1::CreateParams(TCreateParams& Params)
{
  TForm::CreateParams(Params);//先调用基础类中的成员函数
  Params.Style &=~WS_CAPTION;
}

----Create a message image table below, capture the wm_nchittest message, and enter the code in the public segment of mainform.h:

BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER
(WM_NCHITTEST,Tmessage,WMNchitTest)
END_MESSAGE_MAP(TForm)

----The WM_NCHITTEST message name is "non-client hit test", which takes precedence over all other client and non client area mouse messages, Windows applications typically pass this message to DefWindowProc, Windows then uses this message to produce all other mouse messages based on the mouse position.

----a declaration to join a message handler function in a private segment:

void __fastcall wmnchittest (tmessage &message);

----Enter the definition of this function in Mainform.cpp:

void __fastcall TForm1::WMNchitTest(Tmessage &message)
{
  if(GetAsyncKeyState(VK_LBUTTON)< 0)
---- message.Result=HTCAPTION; //如果鼠标左键按下,就通知Windows鼠标所在区域是标题栏区,Windows就会按要求完成拖动操作。
  else
message.Result=HTCLIENT;
}

----here with the Windows API function Getasynckeystate Real-time check the state of the mouse button, by detecting the Vk_lbutton of the virtual key code is placed to detect the mouse action, if the left mouse button, this key code is placed 1 high, At this point the Getasynckeystate function returns a negative number.

----press F9 to compile and run the program, click anywhere in the form to drag the form as you would click on the title bar, and there is no mechanism to end the program, which can be achieved by adding popmenu.

----The above code in C + + BUILDER3, Pwin98 Environment compiled, run through.

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.