When using some software, have you noticed that some software has some interesting things.
For example, there is a button on the title bar of the main window. InThis small value is everywhere on the Internet.
Control. How can buttons be added to non-customer zones?(Client?
Here, the most important thing is that you should not be misled by traditional knowledge: really think it is
A button. Famous handle (Of course, the handle cannot be placed on the title bar. Experienced programmers
UseSpy ++ will immediately find the secret. It is not a button, only
The process is like a button.
Now that we know why, why can't we do it ourselves? Of course, it's okay. Let's use it next.
Delphi to implement it. Pay attention to my annotations.
Before a specific instance, we should know several important messages about the title bar:
WM_NCPAINT: Re-draws the title bar message. We must stop it. You can redraw the button here;
WM_NCLBUTTONDOWN: Click the left mouse button on the title bar to send a message. We can block it and draw it on the title bar.
Button, And you can process your own Click Event in it, making it like a button;
WM_NCLBUTTONUP: Release the left mouse button message on the title bar. We can intercept it and draw
The button appears;
WM_NCLBUTTONDBLCLK: double-click the left-click message on the title bar. We can intercept it, when it is in the button Area
When double-clicking, we should make it invalid to avoid maximizing and restoring the form.
WM_NCRBUTTONDOWN: Right-click the message on the title bar. We can intercept it.
When it is clicked, we should make it invalid to avoid the pop-up form-based menu.
WM_NCMOUSEMOVE: move the mouse message on the title bar. We can intercept it, when the mouse moves out of the button Area
We must draw the button that is not pressed, that is, the way it is raised.
WM_NCACTIVATE: this message is received when the title bar switches between activation and non-activation. We can stop it,
When processing the activation status of the window, we can do something, such as adding the font on the buttons in the title bar
To indicate the current status of the window. I have not added this function below. If you are interested in
You can do it yourself.
(You can find that the messages in the title bar start with WM_NC)
The instance file is as follows:
Unit main;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus;
Type
TForm1 = class (TForm)
Procedure FormCreate (Sender: TObject );
Procedure FormDestroy (Sender: TObject );
Private
{Private declarations}
CBBtnRect: TRect; // Caption Bar Button Rectangle
CBBtnFont: TFont; // Caption Bar Button Font
Procedure DrawCaptionBtn (uEdge: UINT );
// When you press the left button on the title bar
Procedure WMNcLButtonDown (var m: TMessage); message WM_NCLBUTTONDOWN;
// This process is entered when the left button is opened on the title bar.
Procedure WMNcLButtonUp (var m: TMessage); message WM_NCLBUTTONUP;
// Move the mouse over the title bar.
Procedure WMNcMouseMove (var m: TMessage); message WM_NCMOUSEMOVE;
// This process is entered when you double-click the left button on the title bar.
Procedure WMNcLButtonDBLClk (var m: TMessage); message WM_NCLBUTTONDBLCLK;
// This process is entered when you press the right button on the title bar
Procedure WMNcRButtonDown (var m: TMessage); message WM_NCRBUTTONDOWN;
// This process is entered when the title bar is drawn.
Procedure WMNcPaint (var m: TMessage); message WM_NCPAINT;
// This process is entered when the title bar is switched between activation and non-activation.
Procedure WMNcActivate (var m: TMessage); message WM_NCACTIVATE;
Public
{Public declarations}
End;
Var
Form1: TForm1;
Implementation
{$ R *. DFM}
Procedure TForm1.DrawCaptionBtn (uEdge: UINT );
Var
HCaptionDC: HDC; // Title bar Device Context
HOldFont: HFONT; // original font
R: TRect;
Begin
HCaptionDC: = GetWindowDC (Self. Handle); // note that GetDC cannot be used.
To title bar
// Device context
File: // how the button looks. If uEdge = EDGE_RAISED, the image is highlighted. If
File: // uEdge = EDGE_SUNKEN.
DrawEdge (hCaptionDC, CBBtnRect, uEdge, BF_RECT or BF_MIDDLE or
BF_SOFT );