Customized General dialog box in Delphi ------ Custom Open File dialog

zhaozj2021-02-11  203

Customized General dialog box under Delphi -------------- Custom Open File dialog box encountered someone in the forum to ask how to define the general dialog in Delphi, I The problem is also more interested, so the time to take the time, now "results" share "results". The topic of this article has a big point, and there is a lot of common dialogs, but here only open the file dialog box as an example. In fact, the principle of customizing all general dialogs is the same. Step 1: Establish a dialog box to make a concept: dialog box mode. The dialog template is a resource, defined in the .rc file, compiles, generates .res files, ultimately exist in the resource dynamic link library (DLL) or in executable. In a professional sharing software, you usually use a template to create a dialog box. The dialog generated by the template is typically used to collect user input, which can put standard Windows controls, such as Button, Label, TextBox, ListBox, ListView, TreeView, and more. The general dialog is also a form generated by the dialog template, but these dialog box is defined by the operating system, the custom common dialog is implemented by changing these templates (open and save the file dialog box, they are added The new template is from the definition). So the first step is to know how to define the dialog template, you can knock directly in NOTPAD (this method is not used here), you can also use an existing tool, the best tool on my machine is Visual Studio. .NET IDE, just a few mice you need. (You can also view it, modify the resources in the executable, open the -> file directly, open the executable file). Now first define a dialog template (procedural) in VS.NET, the template is to customize the part on the file dialog, you need to pay attention to the template must have DS_3DLOOK, DS_CONTROL, WS_CHILD, WS_CLIPSIBLINGS style and There is no Border because the general dialog is the relationship between our entire template as sub-form subclass to the original dialog (similar to Button and other standard controls with its owner). I put the dialog template and the content of the .rc file: // .rc file content 131 DialoGex 0, 0, 282, 36 style DS_SETFONT | DS_3DLOOK | DS_FIXEDSYS | DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS FONT 8, "MS shell DLG ", 400, 0, 0x1 Begin LText" file name: ", - 1, 7, 8, 40, 8 LTEXT" This static control is used to display file name ", 1004, 51, 8, 224, 8, ss_pathellips control" if selected The file is a picture file for preview ", 1005," Button ", BS_AUTOCHECKBOX | WS_TABSTOP, 7, 18, 268, 10 End The above dialog box is not any actual meaning, only use to explain the function. Compile this .rc file into .RES files (you can use VS.NET directly, you can also use the Delphi Belt BRCC32 tool) to use later examples.

Step 2: Inheriting the TopEndialog class call Open File dialog requires only one API: getopenFileName, this API requires a parameter of an OpenFileName structure. When the custom dialog is specified as the ID of the dialog template (Identifier) The OFN_ENABLETEMPLATE constant is included in the Flags member. The dialog template and each control therebet should have its own unique identifier, and these identities cannot be repeated with the identity of the original control on the general dialog. There are two types: string identification and digital identifier, in this case, using numeric ID: Dialog Sample ID 131, the first static control is identified as -1, the second static control is 1,004, Checkbox The logo is 1005. (Identifying a control of -1 is generally constant constant static control.) The type of LPTemplatenAme member of the OpenFileName structure is a null-terminated string pointer. If the dialog template is identified as a string, you can directly assign a value, such as a certain The template is identified as IDD_mydialog, then the assignment statement is: lptemplatename: = PCHAR ('IDD_MYDIALOG'), if a number, such as the identity of the template in this example is 131, the assignment statement is: lptemplatename: = Windows.MakeintResource (131) . All General Dialogs in Delphi are inherited from the TcommondialG abstraction class, defined in this abstract class: Template, type PCHAR. This attribute is used to store template identity, but unfortunately we cannot access it (because of protected) other than its inheritance class. So we need to redefine a public attribute in its grandson class to access it indirectly. Create a VCL class TMYOPENDIALOG in Delphi inherited from the TopEndialog class, define a publication of Templateres, assigns the protected Template property by its write method, listed below: // *********** ***************************************

// myopendialog.pas

// TMYOPENDIALOG class implements custom open file dialog

// by: Joe Huang Date: 2004-01-05

// *************************************************

Unit myopendialog;

Interface

Uses

Sysutils, Classes, Dialogs, Windows, Messages, Commdlg

Type

Tcommandevent = procedure (control) of Of Object;

TMYOPENDIALOG = Class (TopEndialog)

Private

{Private Declarations}

Ftemplateres: pchar;

FONCOMMAND: TCOMMANDEVENT;

Procedure setTemplates (Const value: pchar);

protected

{Protected Declarations}

Procedure WndProc (var message: tMessage; OVERRIDE;

public

{Public declarations}

/ / This property is used to specify the identity of the custom template

Property Templateres: Pchar Read Ftemplateres Write setTemplateres;

Published

{Published Declarations}

Property Oncommand: Tcommandevent Read Foncommand Write Foncommand;

END;

PROCEDURE register;

IMPLEMentation

PROCEDURE register;

Begin

RegisterComponents ('Samples', [TMYOPENDIALOG]);

END;

{TMYOPENDIALOG}

Procedure Tmyopendialog.setTemplateres (Const Value: Pchar);

Begin

FTEMPLATERES: = Value;

Self.Template: = value;

END;

Procedure TMYOPENDIALOG.WNDPROC (Var Message: TMESSAGE);

Begin

Message.Result: = 0;

IF (Message.msg = WM_COMMAND) THEN

Begin

IF assigned (foncommand) THEN

FonCommand (Message.wParamlo);

END;

Inherited WNDPROC (Message);

END;

End.

The TMYOPENDIALOG class also defines an event to capture changes in control status on dialog templates (later illustrate this event). Register this class to the Samples page of the component panel. Step 3: Establish an engineering implementation Custom dialog New project saved to the directory, put the .res file in the first step into this directory and join the project file. Drag our new control into Form1 from the Samples page, name is Myopendialog1, and then put the button1 on Form1, button1's ON_Click event code is as follows:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Myopendialog1.templateres: = Windows.makeintResource (131);

Myopendialog1.execute;

END;

Myopendialog1 ON_SHOW event code is as follows:

Procedure TFORM1.MYOPENDIALOG1SHOW (Sender: TOBJECT);

Begin

/ / Logo the control of 1004 is the second static control, used to display the full name of the selected file

// This event is generally used to initialize the contents and status of the template

Setdlgitemtext (myopendialog1.handle, 1004, 'initialization ...');

END;

Myopendialog1's ON_SELECTIONCHANGE event code is as follows:

Procedure TFORM1.MYOPENDIALOG1SELECTIONCHANGE (Sender: TOBJECT);

Begin

/ / Show the full name of the selected file on the second static control

Setdlgitemtext (myopendialog1.handle, 1004, pchar (myopendialog1.filename);

END;

Myopendialog1 on_command event code is as follows:

Procedure TFORM1.MYOPENDIALOG1COMMAND (ControlID: Word); var

HCTRL: HWND;

Begin

If constralid = 1005 THEN / / THECKBOX control, triggered this event when the status changes

Begin

HCTR: = Getdlgitem (myopendialog1.handle, control);

IF hctrl <= 0 dam

IF sendMessage (hctrl, bm_getcheck, 0, 0) = bst_checked then

ShowMessage ('you checked me.')

Else

ShowMessage ('you'll unchecked me.');

END;

END;

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

New Post(0)