Add a New button to the Windows window title bar

Source: Internet
Author: User

Add a New button to the Windows window title bar 

For the standard Windows window that we are familiar with , the title bar typically contains 3 buttons, the Maximize button, the Minimize button, and the Close button. Do you want to add a new custom button to your Windows window's title bar to personalize your needs and make your own Windows more distinctive?

Let's discuss how to add a new button to the title bar of a window in Delphi.

  First, the implementation of the following procedures to define:

1, define the Drawcaptbutton process, the function of this process is to draw a button at the specified position.

In the process to use the Win32 function GetSystemMetrics to get the size of the window and the size of the caption button; Use the Delphi function bounds define a rectangle, which is the position of the new button, and then define a small rectangle, which is to fill in the text Finally, I call the more useful function in Delphi to drawbuttonface the button.

2, each time we operate on the window, for example, maximize the operation or minimize the operation, the new button will disappear, in order to solve this problem, we have to deal with all the messages, to write a process for each message to redraw the button.

A, define the Wmncpaint (Var msg:twmncpaint) process and process the message wm_ncpaint.

B, define the Wmncactivate (Var msg:twmncactivate) process and process the message wm_ncactivate.

C, define the Wmsettext (Var msg:twmsettext) process and process the message wm_settext.

D, define the Wmnchittest (Var msg:twmnchittest) process and process the message wm_nchittest.

E, define the Wmnclbuttondown (Var msg:twmnclbuttondown) process and process the message wm_nclbuttondown.

 Second, the specific source procedures are as follows:

We use the source program to tell the implementation of the process, from which we can see how the program calls the Win32 function and how the Drawbuttonface function is used.

Unit Main;
Interface

Uses
Windows, Buttons, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs;
Type
TForm1 = Class (Tform)
Procedure Formresize (Sender:tobject);
Private
Captionbtn:trect;
Procedure Drawcaptbutton;
Procedure Wmncpaint (var msg:twmncpaint); Message wm_ncpaint;
Procedure Wmncactivate (var msg:twmncactivate); Message wm_ncactivate;
Procedure Wmsettext (var msg:twmsettext); Message Wm_settext;
Procedure Wmnchittest (var msg:twmnchittest); Message wm_nchittest;
Procedure Wmnclbuttondown (var msg:twmnclbuttondown); Message Wm_nclbuttondown;
Public
{Public declarations}
End

Var
Form1:tform1;

Implementation

Const
HTCAPTIONBTN = htsizelast + 1;
{$R *. DFM}

Procedure Tform1.drawcaptbutton;  
File://drawcapbuttton Implementation of the procedure
Var
xframe,
Yframe,
Xsize,
Ysize:integer;
R:trect;  
Begin
Xframe: = GetSystemMetrics (sm_cxframe);
Yframe: = GetSystemMetrics (Sm_cyframe);  
file://places the width of the window in the variable xframe, placing the height of the window in the variable yframe
xsize:= getsystemmetrics (sm_cxsize);
ysize:= GetSystemMetrics (sm_cysize);
//The width of the title bar button is placed in the variable xsize, the height of the title bar button is placed in the variable ysize
captionbtn: = Bounds (Width-xframe-4*xsize + 2,
Yframe + 2, XS Ize-2, ySize-4);  
file://defines the position of the new Caption button, and the value is placed in the variable captionbtn
Canvas.handle: = GETWINDOWDC (self.handle);
  FILE://gets the handle of the window  
Canvas.Font.Name: = ' Symbol ';
Canvas.Font.Color: = Clblue;
Canvas.Font.Style: = [Fsbold];
Canvas.Pen.Color: = Clyellow;
Canvas.Brush.Color: = Clbtnface;  
file://defines the font, brush, brush, and other properties of the canvas
Try
drawbuttonface (canvas, captionbtn, 1, Bsautodetect, False, False, false);
file://to draw a defined button on the canvas

R: = Bounds (width-xframe-4 * xsize + 2,
Yframe + 3, xSize-6, ySize-7);
file://draw a small rectangle on the new button
With Captionbtn do
Canvas.textrect (R, R.left + 2, r.top-1, ' W ');
file://fill in a character ' W ' character on the small rectangle drawn above
Finally
ReleaseDC (Self.handle, Canvas.handle);
Canvas.handle: = 0;
file://fault tolerant processing, if an exception occurs, the handle is released
End
End

Procedure Tform1.wmncpaint (var msg:twmncpaint);
A concrete implementation of the Wmncpaint process, which is called when the window is drawn
Begin
inherited;//inheriting the default message handler
drawcaptbutton;//the button to redraw
End

Procedure TForm1. (Var msg:twmncactivate);
The wmncactivate procedure is the same as the Wmncpaint procedure, which is called when the non-client area of the window is changed to an active or inactive state.
Begin
inherited;
Drawcaptbutton;
End

Procedure Tform1.wmsettext (var msg:twmsettext);
The Wmsettext procedure is the same as the Wmncpaint procedure, which is called when the text of the window is set
Begin
inherited;
Drawcaptbutton;
End

Procedure Tform1.wmnchittest (var msg:twmnchittest);
The file://wmnchittest procedure is the same as the Wmncpaint procedure, which is called when the cursor moves or the mouse button is pressed or the mouse button is released.
Begin
inherited;
With MSG do
If PtInRect (captionbtn, point (Xpos-left, ypos-top) and then
Result: = htcaptionbtn;//Determines whether the position of the mouse is within the rectangle of the new button, if the identity value of the new button is returned
End

Procedure Tform1.wmnclbuttondown (var msg:twmnclbuttondown);
The Wmnclbuttondown procedure is the same as the Wmncpaint procedure, which is called when the left mouse button is pressed while the cursor is in the window's non-client area.
Begin
inherited;
if (msg.hittest = htcaptionbtn) Then
ShowMessage (' You clicked on the New button on the title bar ');
file://determine if the clicked is a new button, if it is to display the above information, here you can write the program code as you need
End
Procedure Tform1.formresize (Sender:tobject);
Begin
Perform (Wm_ncactivate, Word (Active), 0);
file://Redraw the title bar if the window size changes
End
End.

Add a New button to the Windows window title bar

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.