Write Control Panel applications

Source: Internet
Author: User
When we open the control panel, we will see some control panel projects, such as "Add/delete programs", "modem", and "system. We often need to configure windows through these projects. Some software, such as Yamaha Sound Card Drivers, will add their own configuration items in the control panel. C ++ builder allows you to quickly develop your own control panel applications.

The use of standard control panel program is actually a DLL (Dynamic Link Library) file, the key is that it implements the cplapplet function. Cplapplet is a callback function that processes all messages sent to the control panel application. When the control panel application is running, the program that calls it obtains the address of the cplapplet function from the Control Panel application, then calls the cplapplet function with this address, and passes the message to it. Messages must be processed in a certain order. Another feature of the Control Panel application is that a DLL file can implement multiple modules. Each module can have its own icons and string resources. Each module corresponds to a project in the control panel.
First, let's take a look at the execution process of the Control Panel application and understand this to compile the correct control panel application.

Since the control panel application is a DLL file, the caller must use the localibrary () function to load and execute it. When the control panel application loads, the cplapplet function receives the cpl_init message, which indicates that the control panel application is initializing. In this case, the cplapplet function should perform some necessary initialization work. If initialization fails, the cplapplet function should return a zero value to notify the caller to stop running the control panel application and release the occupied resources. The message is sent only once.

After the cplapplet function returns the successful initialization information, the caller will send the cpl_getcount message to it. The cplapplet function should return the number of modules in the control panel application. The message is sent only once.

Then, for each module, the cplapplet function will receive a cpl_inquire and cpl_newinquire message. In response to this message, the cplapplet function fills in the cplinfo or newcplinfo structure, which stores the name, icon, and description of the Control Panel application. Generally, you only need to process the cpl_inquire message. If you want to change the icon and display information (such as displaying different language texts on different language platforms), you need to respond to the cpl_newinquire message. Each module sends the two messages once.

When the cplapplet function receives the cpl_dblclk message, it indicates that the corresponding module will be run. This message can be sent multiple times. Generally, a cpl_dblclk message is sent when you double-click an icon. The message contains the module identification number. Therefore, the cplapplet function can identify the module to be executed.

Before the control panel application exits, the cplapplet function of each module receives a cpl_stop message, which contains the identification number of the module. In this case, the cplapplet function needs to clean up different modules.

After processing the last cpl_stop message, the cplapplet function receives the cpl_exit message, indicating that the control panel application is about to exit. After the cplapplet function returns, the caller immediately uses the freelibrary () function to release the occupied resources.

Next we will use C ++ builder to create a DLL file and compile the cplapplet function code. The control panel application contains a module that displays a window with "Hello World ". The following code is successfully debugged in bcb4 Professional Edition and bcb5 Enterprise Edition.

1. define a new file

Select "file-> New" and select "DLL wizard" in the displayed screen ".

2. Save the file

Select "Save project as" in the menu to save the project and CPP files to the specified directory. The default project name is project1.

3. Define icons

Defines the icons that the control panel application displays in the control panel. Here we use the RC resource file.

Select "new-> text file" in the menu, generate a text file, and save it as an ico. RC file. Edit the file, add a line "myicon icon1.ico" to it, copy an icon file to the directory where the program is located, change the file name to icon1.ico, or use image editor to create one. Note that the Icon size should be 32x32 (large icon ).

In Project Manager, add the RC file to the project.

4. Add a new form, which will be displayed as a window

Select "file-> New" from the menu and select form. Then add a label object to form and set its caption to "Hello World ".

5. Edit the project1.cpp File

First, you need to include the header file Cpl. h to use some constants, such as cpl_init and other messages.

Then define a global variable ginstance.

Process the DLL entry function and save the program handle to the ginstance global variable.

The program content is as follows: # include <VCL. h> # include <Cpl. h> hinstance ginstance; int winapi dllentrypoint (hinstance hinst, unsigned long reason, void *) {If (reason = dll_process_attach) ginstance = hinst; // The ginstance stores the program handle. Return 1 ;}

6. Edit the project1.cpp file and complete the cplapplet function. // The cplapplet function must be output because it needs to be used outside the DLL. Extern "C" int _ stdcall _ declspec (dllexport) cplapplet (hwnd hwcontrolpanel, int MSG, int lparam1, int lparam2) {Switch (MSG) {Case cpl_init: Return true; // return the successful initialization information case cpl_getcount: return 1; // inform the caller that the control panel application contains a module case cpl_newinquire: {// set the icon here, newcplinfo * info = (newcplinfo *) lparam2; zeromemory (Info, sizeof (newcplinfo); Info-> dwsize = sizeof (newcplinfo); // icon resource file ICO. myi defined in the RC file Con Info-> hicon = loadicon (ginstance, "myicon"); strcpy (Info-> szname, "module name "); // szname is the name of the module in the control panel strcpy (Info-> szinfo, "module details"); // szinfo is the description of this module. When you view the list in the control panel, the left side is the module name and the right side is the description of the module. Return 0;} case cpl_dblclk: // Add the required functions here. // Here we simply display the form. Try {application-> initialize (); Application-> createform (_ classid (tform1), & form1); Application-> Run ();} catch (exception & exception) {application-> showexception (& exception) ;}return 0 ;}}

In bcb4, DLL wizard generates the project1.cpp file. The newly added form is form1 and the corresponding CPP file is unit1.cpp. In bcb5, project1.cpp is not generated, instead, unit1.cpp is generated. The newly added form is form2, and the corresponding CPP file is unit2.cpp. If you are using bcb5, You need to modify the above Code.

7. Compile and run

Now let's compile the program. After successful compilation, A project1.dll file will be generated. To run the DLL file as a control panel application, you must use either of the following methods:

1. Change the extension to CPL and copy the file to the Windows System directory. For Win9x, copy to the C:/Windows/system directory; For WinNT, copy to C:/winnt/system32.

2. Change the extension to CPL and run the command in C:/Windows/control. in the [mmcpl] section of the INI file, add the file path in the form of "name = CPL file full path ".

Open the control panel, and an additional "module name" is displayed ". Use the control panel application class tappletmodule bcb5 provided by C ++ B to directly generate control panel applications. The following example generates a control panel application with two modules. That is to say, two new projects will appear in the control panel. Run C ++ builder5, execute "file-> New" in the menu, and select "Control Panel application ".

By default, bcb5 already has a tappletmodule1 module. We need to add another tappletmodule2 module.

Select "file-> New" from the execution menu and select "Control Panel module ". The CPP files of Module 1 and module 2 are unit1.cpp and unit2.cpp respectively.

Set the icons and names of the two modules in "Object inspector. Here, the caption of tappletmodule1 is set to "Module 1", and the caption of tappletmodule2 is set to "module 2", which is the name of the module displayed in the control panel. Click the appleticon attribute and select different icons for them.

Compile the message processing functions in two modules respectively. The tappletmodule provides seven events. Onactivate corresponds to the cpl_dblclick message, oninquire corresponds to the publish message, onnewinquire corresponds to the newinquire message, onstop corresponds to the cpl_stop message, oncreate corresponds to the cpl_init message, and ondestroy corresponds to the CPL. An onstartwparmsevent is triggered if the Panel application is started by rundll(package rundll.exe and rundll32.exe. For example, if you want to run the "modem" in the control panel, you can run rundll32 shell32.dll and control_rundll MODEM. Cpl on the command line. The onstartwparms event will be triggered. If you enter the Control Panel to run the "modem", The onstartwparms event will not be triggered.

To add functions and implementation windows, add form to the two modules respectively. Each module can be written as a standard BCB window program.

So far, a control panel application has been compiled. After compilation, copy the generated project1.cpl to the C:/Windows/system directory. Is the display in the control panel. Module 1 and module 2 are actually implemented by a control panel program.

From the above steps, we can see that it is very convenient to use bcb5 to write control panel applications. All the frameworks BCB have been automatically compiled for you. You only need to decide to use several modules, function Code is written for each module to avoid tedious message processing.

 

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.