Summary: There are a variety of methods for the configuration of the computer's operating environment, and a new method is introduced in Windows 9X / NT, that is, the control panel. Use the control panel to configure the environment, which is convenient to intuitive. This article describes the principles and writing methods of the control panel program, and gives an actual example written in Delphi4.0. Keywords: Control panel program, dynamic link library, message processing, registry preface: When writing software, sometimes users need to set the application environment. Common practices have a setup program that performs parameter configuration during the installation process, or provides a dedicated function option in the software, and users can use these function options when using. Both methods, especially the latter, is the most commonly used method in general software. Here, introduce you to a new method: use the control panel program (Control Panel Applets). We all know that the control panel is provided in Windows 9X / NT, and users can easily complete the settings of various environment variables, such as input methods, ODBC, and more. This article will use a very simple example to explain how to implement a control panel program in Delphi. 1. Control panel programs 1.1cplapplet control panel program is a special dynamic link library (DLL), first, its extension is different from the general dynamic link library, not .dll but .cpl. Second, its standard entry function is not dllmain () but is CPLApplet (). The CPLApplet receives the message sent from the control panel and makes a corresponding reflection. The function of the CPLApplet is as follows: function cpLapplet (HWNDCPL: HWND; // Handle UMSG: word; // Send to Control Panel LParam1: Longint; // Message Parameters LPARAM2: Longint // Message Parameters): Longint UMSG has several forms: the message occurrence time corresponding to the processing method CPL_DBLCLK user double-click the main interface of the program corresponding to the program CPL_EXIT CPL_STOP releases all resources CPL_GETCOUNT CPL_INIT After returning the number of dialog boxes in the dialog box, the number of CPL_INIT programs are loaded After the memory is initialized, the CPL_INQUIRE * CPL_GETCOUNT sets the nature of a particular window CPL_NEWINQUIRE CPL_GETCOUNT Set the nature of a particular window CPL_SELECT * CPL_STOP program to release the dialog box resource * is compatible with the old version, Windows 9X / NT 4.0 No longer used. 1.2 Controlling Message Processing CPLApplet function processing in the Control Panel program Sends all messages to the control panel program. The message sent to the control panel program is performed in a certain order. 1 When the program is loaded into the memory, the CPLApplet function receives the CPL_INT message. When the message is received, the CPLApplet function should be some initialization, such as allocation of memory. If the initialization is complete, it should return non-zero, otherwise, returns zero, so the program will be terminated. 2 If the CPL_INT is successfully processed, the CPLApplet function will receive the CPL_GetCount message. The CPLApplet must return the number of dialogs. 3 Next, the CPLApplet receives the CPL_Newinquire message. The LPARAM2 at this time is a pointer to the NewCPL_INFO structure.
TYPE NewCPL_INFO = Record DWSIZE: Longword; // This structure size dwflags: longword; // does not use dwhelpContext: longword; // unused LDATA: Integer; // When the message is CPL_DBClick or CPL_Stop, it is a return value Hicon: The handle of the icon of the Hicon; // dialog box szname: array [0..31] of char; // Description text under the control panel. SZINFO: Array [0..63] of char; / / In the state line in the control panel. Szhelpfile: array [0..127] of char; // Unexpected END; After receiving the message, the CPLApplet must populate the structure. Point out its name, prompt information, icon, etc. 4 Next, the CPLApplet receives the CPL_DBClick message, here, generally the display dialog and the input of the user, etc. 5 When the user closes the program, the CPLApplet receives the CPL_STOP message. At this point, the allocated memory resources should be released. If there is no, you can ignore this step. 6 Next, the CPLApplet receives the CPL_EXIT message, the Unregister's various windows. 7 Finally, the system call freeELibrary to remove the DLL (the control panel program) from memory. In general, we only need to process CPL_DBClick and CPL_Newinquire. 2. A simple example: This example is a demos of a CPL, its role is to set the communication port of its socket for a customer software. This port value is placed in the port under HKEY_LOCAL_MACHINE / SOFTWARE / SILICON / PAPERS. The port number can be changed by this program.
The source code is as follows: //mycpl.dpr source code library mycpl; uses sysutils, classes, u_main in 'u_main.pas' {f_main}; {$ E CPL} exports cplapplet; begin end. //U_main.pas Source Code unit u_main; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ToolWin, StdCtrls, Buttons, registry; type Tf_main = class (TForm) edPort: TEdit; btnOk: TBitBtn; btnCancel: TBitBtn ; Label1: TLabel; procedure FormCreate (Sender: TObject); procedure btnOkClick (Sender: TObject); private {Private declarations} public {public declarations} end; type NEWCPLINFO = record // ncpli dwSize: Longword; dwFlags: Longword; dwHelpContext: Longword; ldata: integer; hicon: hic; szname: array [0..31] of char; szinfo: array [0..63] of char; szhelpfile: array [0..127] of char; end; type pnewcplinfo = ^ NEWCPLINFO; const CPL_INIT = 1; const CPL_GETCOUNT = 2; const CPL_INQUIRE = 3; const CPL_SELECT = 4; const CPL_DBLCLK = 5; const CPL_STOP = 6; const CPL_EXIT = 7; const CPL_NEWINQUIRE = 8; var f_main: Tf_main; aa : Ticon; regf: Tregistry; function CPlApplet (hwndCPl: HWND; uMsg: word; lParam1: Longint; lParam2: Longint): Longint; stdcall; function OnInquire (uAppNum: word; pInfo: PNEWCPLINFO): integer; stdcall; function OnDblclk (hwndCPl: HWND; uAppNum: word LDATA: INTEGER: INTEGER; stdcall; importation {$ r * .dfm} {$ r icon.res} // handles Various messages Function CPLApplet (HWNDCPL: hwnd; umsg: word; lparam1: longint; lparam2: longint) : Longint; stdcall; begin case uMsg of CPL_DBLCLK: OnDblclk (hwndCPl, lParam1, lParam2); CPL_EXIT: result: = 0; CPL_GETCOUNT: result: = 1; CPL_INIT: result: = 1; CPL_NEWINQUIRE: OnInquire (lParam1, PNEWCPLINFO (lParam2 ))); CPL_INQUIRE: Result: = 0; // NOT HANDED CPL_SELECT: Result: = 1; CPL_Stop: Result: = 1;
Else Result: = 0; End; Result: = 1; end; // Fill the NewcplInfo structure. function OnInquire (uAppNum: word; pInfo: PNEWCPLINFO): integer; stdcall; begin with pInfo ^ do begin dwSize: = sizeof (NEWCPLINFO); dwFlags: = 0; dwHelpContext: = 0; lData: = 0; hIcon: = loadicon ( Hinstance, 'I1'); szname: = 'mydemo'; szinfo: = 'MyDemo system setting'; szhelpfile: = '; end; result: = 0; end; // Display dialog Function Ondblclk (hwndcpl: hwnd; UAPPNUM: WORD; LDATA: INTEGER: Integer; stdcall; var f_main: tf_main; begin f_main: = tf_main.create (Application); f_main.showmodal; result: = 0; end; // Put the value of Port from the registry Read, write to Edit Edport. Procedure TF_MAIN.FormCreate (Sender: TOBJECT); VAR port: integer; begin aa: = ticon.create; aa.handle: = loading (Hinstance, 'I1'); if (aa.handle = 0) Then ShowMessage ('error Icon '); icon: = aa; regf: = Tregistry.create; regf.rootkey: = hkey_local_machine; regf.openkey (' Software / Silicon / PAPERS ', TRUE); port: = 0; if regf.valueexists (' Port) ') = True the port: = regf.readinteger (' port '); edport.text: = INTOSTOSTR (port); regf.closekey; regf.free; end; // Write the value of the port back to the Registry Procedure TF_MAIN. btnOkClick (Sender: TObject); begin regF: = TRegistry.Create; RegF.RootKey: = HKEY_LOCAL_MACHINE; RegF.OpenKey ( 'SOFTWARE / Silicon / papers', True); RegF.WriteInteger ( 'port', strtoint (edPort.text ); Regf.closekey; regf.free; end; end. Description: 1 where Icon.Res contains the icon icon, so add a sentence {$ R icon.res} in the program. 2 {$ E CPL} indicates the extension of the dynamic connection library as the CPL. 3 After the exports keyword indicates the function of the output, simply output the CPLApplet. 3. Control Panel installation: Select Project | Bulid MyCPL Compiler in Delphi. MyCPL is the name of the sample program. Then copy mycpl.cpl to the System directory under the Windows directory (SYSTEM32 under Windows NT). Then select | Start | Set the Control Panel to see the result.