How to write a control panel program

zhaozj2021-02-11  212

How to write a control panel program

Control panel programs are some special dynamic connection libraries (DLLs) for configuring the Windows environment. Windows has provided us with some standard control panel programs such as fonts, keyboards, mouse settings, etc .; as shown in Figure 1. We can also create your own control panel programs based on actual needs to allow users to view or modify parameter settings for specific hardware and hardware.

The function and operation of the control panel control panel program is that a dialog box allows users to complete specific tasks. However, different from other applications is that the control panel program is not a separate application, which does not provide the standard menu or other method to let the user directly operate these dialogs, but other applications (such as control panel) Control and display their dialog box. Control panel programs are typically controlled by a Windows system tool (ie, control panel) to allow users to run. However, other applications can only load the messages required by the control panel and process their return values, which can also be loaded and manage these control panel programs, in order to express the simple, here we collect such a program as a control panel. Most of the control panel programs are only displayed and maintained for the user to control the parameter settings of the system a component, but in fact the control panel program can provide multiple dialogs to control multiple system components (these single The dialog is often referred to as an Applet applet). To distinguish these dialogs, the control panel program typically provides an icon for each dialog. Control panels are displayed in the user to select different dialogs for configuration.

The program entry function is mentioned earlier, the control panel program is a special dynamic connection library (DLL), which must provide a standard function entry CPLApplet, the control panel program receives the message sent by the control panel through this function. Thereby performing the corresponding functions, such as initialization programs, display, and managing dialogs, and closing programs. When the control panel first loads the control panel program, obtain the address of the CPLAPPLET function, and call the function of the control panel program and send it a message to it. The control panel can send to the control panel program and its significance as shown in the following table: Message Name Meaning CPL_DBLCLK This message Notifies the CPLApplet function user Double-click the icon associated with a dialog. The CPLApplet should display the corresponding dialog and complete the task specified by the user. CPL_EXIT This message is called after the last CPL_STOP message, and the control panel calls the FreeELBRARY function release before the dynamic connection DLL containing the control panel program. CPLApplets should release the resource used to be ready to close. CPL_GETCOUNT This message is sent after sending a CPL_INIT message, and the CPLApplet should return an integer to indicate how many dialogs it supports. CPL_INIT This message is called when the control panel dynamic connection library DLL program is first loaded, and the CPLApplet can perform some initialization processes, including memory allocation. CPL_INQUIRE This message is sent after the CPL_GetCount message, allows the cpLapplet to provide information on a specific dialog. The LPARAM2 parameter of the CPLAPPLET points to a CPLINFO structure. CPL_NEWINQUIRE This message is sent after the CPL_GetCount message, allows the CPLApplet to provide information on the specified dialog. Parameter lparam2 is a pointer to the Newcplinfo structure, in Windows 95 and Windows NT Version 4.0, in order to improve performance, the control panel program should process the CPL_INQUIRE message instead of the CPL_Newinquire message. CPL_STOP Sends the message to each dialog box before the control panel is turned off. The CPLApplet should release the memory resources associated with the given dialog.

The message processing callback function CPLApplet handles the above message sent by the control panel to the control panel program, which is transmitted and processed in a particular order. When the control panel first loads a dynamic connection library of the control panel program, the CPLApplet receives the CPL_INIT message. CPLAPPET should perform some initialization work in a program that handles the message, such as allocating memory, etc., and returns a non-0 value; if the CPLApplet is initialized, it should return 0, the control panel will stop sending messages to the CPLApplet and release the dynamic that has been loaded. Link library DLL. When the CPL_INIT message is successful, the CPLApplet function will receive the CPL_GetCount message, and the CPLApplet function must return the number of dialogs supported by the control panel program. For each dialog supported by the control panel, the CPLApplet function receives a CPL_INQUIRE message sent by the control panel and a CPL_Newinquire message. The function needs to populate the CPLINFO or NewCPLINFO structure, such as name, icon, and description string. Most control panel programs are ignored using only CPL_INQUIRE messages only. CPL_INQUIRE is stored by a control panel to store information to improve performance. CPL_Newinquire is only useful when you need to change the icon of the control panel according to the status of the computer and the display string. When the user doubles the icon corresponding to a dialog in the control panel, the CPLApplet function will receive a CPL_DBLCLK message, and the CPLApplet function can receive the message multiple times. This message contains the logo of the dialog and a LDATA data, and the CPLApplet function should display the corresponding dialog and process the user's input. When the control panel is turned off, the CPLApplet function will receive a CPL_Stop message corresponding to each dialog supported by the control panel program. This message contains the logo of the dialog and LDATA data, and the CPLApplet function should release the resources occupied by this dialog. After the last CPL_STOP message, the CPLApplet will receive a CPL_EXIT message. This point should be released all resources occupied by this control panel program. After the message returns, the control panel will call freeElibrary to release the control panel program dynamic connection library. Control Panel Mounting The output file extension is changed to .cpl in the Compile Connection Output file name option, copy it to the system directory of Windows after the compilation connection is successful.

Sample program: This sample program will display a dialog and change the corresponding data in the system registry according to the user's input. After install the control panel sample program, start the control panel again, in the control panel The icon of the sample program will appear, and one dialog will pop up after double-click the icon. as shown in picture 2. The program uses Visual C , select New from the File menu, then select the MFC AppWizard, the project name is CPL, as shown in Figure 3. Select the MFC Extension DLL after clicking OK, as shown in Figure 4. In this way, Visual C will create a dynamic connection library project that contains resource files and DEF files. In Visual C AppWizard, add an icon resource and string resources, ID_ICON1 and ID_STRING1, then add one of the dialogs shown in Figure 2, where there is a checkbox, ID_Check1. And change the output file name of the project to cdauto.cpl, the method is the settings ... item of the Project menu, as shown in Figure 5. The following is a list of procedures.

Program List: // CPL.cpp: Defines The Initialization Routines for the DLL.//include "stdafx.h" #include #include #include

#include "resource.h"

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

Static AFX_EXTENSION_MODULE CPLDLL = {null, null};

Hinstance hinst = null;

BOOL FAR PASCAL CPLDlg (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); file: // control panel interface function CPlAppletextern "C" int APIENTRY CPlApplet (HWND hwndCPL, UINT uMsg, LPARAM lParam1, LPARAM lParam2); extern "C "INT APIENTRYDLMAIN (Hinstance Hinstance, DWORD DWREASON, LPVOID LPRESERVED) {// Remove this if you use lpreservedunreferenced_parameter (LPRESERVED);

IF (dwreason == DLL_PROCESS_ATTACH) {trace0 ("cpl.dll initializing! / n");

// Extension DLL One-Time Initializationif (! AfxiniteXtensionModule (CPLDLL, Hinstance)) Return 0;

// Insert this DLL into the resource chain // NOTE: If this Extension DLL is being implicitly linked to by // an MFC Regular DLL (such as an ActiveX Control) // instead of an MFC application, then you will want to / / remove this line from DllMain and put it in a separate // function exported from this Extension DLL. The Regular DLL // that uses this Extension DLL should then explicitly call that // function to initialize this Extension DLL. Otherwise, // the CDynlinklibrary Object Will Not Be attached to the // Regular Dll's Resource Chain, and Serious Problems Will // RESULT.

new CDynLinkLibrary (CPLDLL);} else if (dwReason == DLL_PROCESS_DETACH) {TRACE0 ( "CPL.DLL Terminating / n!"); // Terminate the library before destructors are calledAfxTermExtensionModule (CPLDLL);} hinst = hInstance; return 1; // ok} int APLAPPLET (HWND HWNDCPL, UINT UMSG, LPARAM LPARAM1, LPARAM LPARAM2) {INT I; LPCPLINFO LPCPLINFO;

i = (int) LPARAM1;

Switch (UMSG) {CASE CPL_INIT: / / FIRST Message, Sent Once Return TRUE

Case CPL_Getcount: // Second Message, Sent ONCE RETURN 1; Break

case CPL_INQUIRE: // third message, sent once per application lpCPlInfo = (LPCPLINFO) lParam2; lpCPlInfo-> lData = 0; lpCPlInfo-> idIcon = IDI_ICON1; lpCPlInfo-> idName = IDS_STRING1; lpCPlInfo-> idInfo = IDS_STRING1; break;

case CPL_DBLCLK: // application icon double-clicked FARPROC lpProcDlg; lpProcDlg = (FARPROC) MakeProcInstance (CPLDlg, hinst); DialogBox (hinst, MAKEINTRESOURCE (IDD_DIALOG1), hwndCPL, (DLGPROC) lpProcDlg); FreeProcInstance (lpProcDlg); break;

Case CPL_Stop: // Sent ONCE Per Application Before CPL_EXIT BREAK;

Case CPL_EXIT: // Sent Once Before Free Freelibrary Is Called Break;

DEFAULT: BREAK;

} Return 0;}

Bool Far Pascal Cpldlg (HWND HDLG, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {

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

New Post(0)