Use C ++ Builder Custom Windows Forms "System Menu"

xiaoxiao2021-03-06  65

Use C Builder Custom Windows Forms "System Menu"

Zhang Jianjun, Hefei Intelligent Machinery Research Institute, Anhui

-------------------------------------------------- -----------------------------

When developing a Windows application, the software person wants to have a unique user interface. For this reason, I don't hesitate to use some third-party OCX controls that occupy the system memory resources. As a result, the program runs slowly or after packaging. The program is too large. In fact, if we carefully study hundreds of WINAPIs provided by Windows, it is not difficult to find that by directly calling API functions, sending, receiving, or intercepting system messages, etc., can also be implemented in some cases can only be called OCX. The interface effect is achieved.

For example, it is also a relatively special interface effect for the screen graphics software we are familiar with the screen, behind its powerful screen graphics.

Typically, when you click on the ICON in the upper left corner of the Windows Form with a mouse, the system menu that is usually fixed.

HyperSnap's form changes the standard "System Menu", adds multiple new menu items and gives them different functions separately. In fact, this effect is not complicated, mainly by calling getSystemMenu, appendmenu and other API functions. Below, we take C Builder as an example, discuss how to implement interface effects similar to HypersNAP in their own application, and give full code instance.

1. API function introduction

(1) GetSystemMenu

1 function:

Allow access (copy or modification) System menu, the normal Windows Form If the standard Windows menu is automatically adopted, "Restore", "Maximize", "Close", "Turns" Wait.

2 original shape:

Hmenu getsystemmenu

HWND HWND,

Bool Brevert

);

3 Entry parameters:

HWnd is a form handle for a pre-changing system menu.

BREVERT is a sign.

When Brevert is a false, getSystemMenu returns hWnd represents a copy of the form, in the initial state, the copy is consistent with the original form, but allows the modification.

When Brevert is true, getSystemMenu restores the default status of the form menu represented by HWnd and cancels the modification of the menu.

4 return value:

When Brevert is a fake, return the handle of the form copy represented by hWnd, when Brevert is true, return

The value is NULL.

(2) APPENUUNU

1 function:

Insert a menu item at the final plug-in, and can specify the text content, appearance, and trigger events of the menu item.

2 original shape:

Bool appendmenu

Hmenu Hmenu

Uint uflags

Uint UidNewItem

LPCTSTR LPNEWITEM

);

3 Entry parameters:

HMenu wants to add menu handles for menu items.

UFlags is a sign of new menu items, which can be a combination of several system constants.

UidNewItem is the identity of the new menu item.

LpnewItem is the text content of the new menu item, which can take MF_STRING, MF_SEPARATOR.

4 return value:

Successfully returns 0, and the failure returns to 0.

(3) WM_SYSCOMMAND

System message triggered when you click on the menu item.

⒉ Programming instance

(1) New project

In the new project Project1 in C Builder 5.0 IDE, the Project1 contains Form1. (2) Form_click event

Void __fastcall tform1 :: formclick (Tobject * Sender)

{

ChangesystemMenu (Form1, "-", 0);

ChangesystemMenu (Form1, "Customized Project ...", 99);

Application-> onMessage = form1-> registermsg; // Specify the form message processing function

}

(3) Custom Message Process

Void __fastcall tform1 :: registermsg (tagmsg & msg, bool & hand)

{

IF (msg.message == wm_syscommand)

IF (msg.wparam == 99)

ShowMessage ("You Click the Custom Menu"); / / Custom Menu Response Code

}

⑷ Change the system menu process

Void __fastcall change consisting of Void_FastCall ChangesystemMenu (TForm * Form, Ansistring Item, Word ItemID)

{

Void * NormalsystemMenu;

Void * minsystemmenu;

Char * aitem = new char [255];

Pchar pitem;

NormalsystemMenu = GetSystemMenu (form-> handle, false);

MinSystemMenu = GetSystemMenu (Application-> Handle, False);

IF (item == "-")

{

Appendmenu (NormalsystemMenu, MF_SEPARATOR, 0, 0); // Add menu separation

Appendmenu (MinsystemMenu, MF_SEPARATOR, 0, 0); // Add menu separation

}

Else

{

Pitem = strpcopy (AITEM, ITEM);

Appendmenu (NormalsystemMenu, MF_String, itemid, Pitem); // Add menu items

Appendmenu (minsystemmenu, mf_string, itemid, pitem); // Add menu items

}

}

⒊ instance code

(1) Unit1.h file

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ifndef UnitAppendmenuh

#define UnitAppendmenuh

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#include

#include

#include

/ / -------------------------------------------------------------------------------------------- ---------------------------

Class TFORM1: Public TForm

{

__published: // Ide-management Components

Void __fastcall registermsg (tagmsg & msg, bool & hand); // Custom menu response event

Void __fastcall formclick (TOBJECT * Sender); private: // user declarations

Public: // user declarations

__fastcall tform1 (tComponent * Owner);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Extern package tform1 * form1;

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ENDIF

(2) Unit1.cpp file

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#pragma HDRSTOP

#include "unitAppendmenu.h"

/ / -------------------------------------------------------------------------------------------- ---------------------------

#pragma package (smart_init)

#pragma resource "* .dfm"

TFORM1 * FORM1;

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tform1 :: tform1 (tComponent * Owner)

: TFORM (OWNER)

{

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall change consisting of Void_FastCall ChangesystemMenu (TForm * Form, Ansistring Item, Word ItemID)

{

Void * NormalsystemMenu;

Void * minsystemmenu;

Char * aitem = new char [255];

Pchar pitem;

NormalsystemMenu = GetSystemMenu (form-> handle, false);

MinSystemMenu = GetSystemMenu (Application-> Handle, False);

IF (item == "-")

{

Appendmenu (NormalsystemMenu, MF_SEPARATOR, 0, 0); // Add menu separation

Appendmenu (MinsystemMenu, MF_SEPARATOR, 0, 0); // Add menu separation

}

Else

{

Pitem = strpcopy (AITEM, ITEM);

Appendmenu (NormalsystemMenu, MF_String, itemid, Pitem); // Add menu items

Appendmenu (minsystemmenu, mf_string, itemid, pitem); // Add menu items

}

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: registermsg (tagmsg & msg, bool & hand)

{

IF (msg.message == wm_syscommand)

IF (Msg.wParam == 99) ShowMessage ("You Click Custom Menu"); / / Custom Menu Response Code

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tform1 :: formclick (Tobject * Sender)

{

ChangesystemMenu (Form1, "-", 0);

ChangesystemMenu (Form1, "Customized Project ...", 99);

Application-> onMessage = form1-> registermsg; // Specify the form message processing function

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

4. Example effect

The above code is debugged in the WIN98, C Builder 5.0 environment, and the interface effect is shown in Figure 1.

Figure 1. Custom Form "System Menu" implemented by this document

-------------------------------------------------- ----------------------------

转载请注明原文地址:https://www.9cbs.com/read-89491.html

New Post(0)