How to Create Control Panel Applet ?

Though Windows provides a number of standard Control Panel applets, we are allowed to add new ones to suit our requirement. A Control Panel applet resides in a DLL having an extension ‘.cpl’. In this article we will discuss how to create an applet that will allow you to swap mouse buttons and change typical mouse settings like mouse trails and double-click time.

Follow the instructions given below to build the applet.

  1. Create a new project called setmouse. Select the project type as ‘MFC AppWizard (dll)’ and choose the option ‘Regular DLL with MFC statically linked’. This option would ensure that the MFC DLL’s used by our applet would also become part of our DLL. As a result, we can run this applet easily on machines that do not have MFC.
  2. Since a ‘.cpl’ file is a DLL we cannot execute it directly. So we will have to build a cpl file, copy it into C:\Windows\System directory and run it through ‘Start | Settings | Control Panel’. During the course of creation of an applet we are likely to change the source code often. This would necessitate copying of .cpl to System directory every time before we can test it. To avoid this select ‘Project | Settings’ menu. A dialog would pop up. In the ‘Debug’ tab type C:\Windows\Control.exe in the edit box titled ‘Select Executable for debug session’. Also, in the ‘Link’ tab type C:\Windows\System\setmouse.cpl in ‘Output file name’ edit box. Once these settings have been made we can directly compile and execute the applet using the normal Ctrl F5.

Remember that path of ‘control.exe’ and the ‘System’ directory may be different in your machine. So make sure that you give correct entries in the edit boxes.

  1. Select ‘File | New | C++ Source File’. Give file name as ‘mousecpl’. This file will contain definition of exported function. Select ‘File | New | C++ Header File’. Give file name as ‘mousecpl’. This file will contain declaration of exported function.
  2. Create an icon (using Resource Editor) for our applet. This icon will be displayed in Control Panel. Also create a dialog that will get displayed on double clicking our applet’s icon in Control Panel. The dialog template should look as shown below:

img1

  1. Insert a new class called mydialog for the above dialog. Add following variables to the mydialog class.
Control ID Type Variable
Slider IDC_SLIDER1 Control m_sliderctrl
Slider IDC_SLIDER1 Value ( int ) m_sliderval
Spin IDC_SPIN1 Control m_spinctrl
Check Box IDC_SWAP Value ( BOOL ) m_swap
  1. Add the following code to OnInitDialog( ) function.

BOOL mydialog::OnInitDialog( )

{

CDialog::OnInitDialog( ) ;

m_sliderctrl.SetRange ( 0, 1000 ) ;

m_sliderctrl.SetPos ( 500 ) ;

m_spinctrl.SetRange ( 1, 100 ) ;

m_spinctrl.SetPos ( 1 ) ;

return TRUE; // return TRUE unless you set the focus to a control

// EXCEPTION: OCX Property Pages should return FALSE

}

Here, we have specified the range and the initial position for the slider and spin controls.

  1. Add the OnOK( ) handler. The code of OnOK( ) function is given below:

void mydialog::OnOK( )

{

CDialog::OnOK( ) ;

::SystemParametersInfo ( SPI_SETMOUSEBUTTONSWAP, m_swap, NULL, NULL ) ;

::SystemParametersInfo ( SPI_SETDOUBLECLICKTIME, m_sliderval, NULL, NULL ) ;

::SystemParametersInfo ( SPI_SETMOUSETRAILS, m_spinctrl.GetPos(), NULL, NULL ) ;

}

Here, firstly, we have called base class’s OnOK( ) function which internally calls UpdateData ( TRUE ). UpdateDate ( TRUE ) transfers values from the controls to the variables associated with the controls. Next, we have called ::SystemParametersInfo( ) function to change the settings according to the values entered by the user.

  1. Every cpl file exports a callback function called CPlApplet( ) which serves as an entry point for an applet. Write the CplApplet( ) function in ‘mousecpl.cpp’ as shown below:

#include “stdafx.h”

#include <cpl.h>

#include “mousecpl.h”

#include “resource.h”

#include “mydialog.h”

LONG APIENTRY CPlApplet ( HWND cplhwnd, UINT msg, LONG lparam1, LONG lparam2 )

{

CPLINFO *cplinfo = ( CPLINFO* ) lparam2 ;

mydialog d ( CWnd::FromHandle ( cplhwnd ) ) ;

switch ( msg )

{

case CPL_DBLCLK:

d.DoModal( ) ;

return 0 ;

case CPL_EXIT:

return 0 ;

case CPL_GETCOUNT:

return 1 ;

case CPL_INIT:

return 1 ;

case CPL_NEWINQUIRE:

return 1 ;

case CPL_INQUIRE:

cplinfo -> idIcon = IDI_ICON1 ;

cplinfo -> idInfo = IDINFO ;

cplinfo -> lData = 0 ;

cplinfo -> idName = IDNAME ;

return 0;

case CPL_SELECT:

return 1 ;

case CPL_STOP:

return 1 ;

default:

break;

}

return 1;

}

Note that the file ‘cpl.h’ has been #included in this file. CPlApplet( ) function receives requests in the form of Control Panel (CPL) messages and then carries out the requested work i.e initializing the application, displaying and managing the dialog box, and closing the application.

When the controlling application first loads the Control Panel applet, it retrieves the address of the CPlApplet( ) function and subsequently uses the address to call the function and pass it messages. Returning a 0 indicates that we have processed the message. Otherwise we must return 1. The meaning of the various messages is given below in brief.

  • CPL_DBLCLK: Sent to notify CPlApplet( ) that the user has chosen the icon associated with a given dialog box. CPlApplet( ) should display the corresponding dialog box.
  • CPL_INIT: Sent immediately after the DLL containing the Control Panel application is loaded. All initializations are done when this message is received.
  • CPL_GETCOUNT: Prompts CPlApplet( ) to return a number that indicates how many dialog boxes it supports.
  • CPL_INQUIRE: Prompts CPlApplet( ) to provide information about a specified dialog box. This information is stored in a structure called CPLINFO. We have stored the following values in CPLINFO structure:

cplinfo -> idIcon = IDI_ICON1 ;

cplinfo -> idInfo = IDINFO ;

cplinfo -> lData = 0 ;

cplinfo -> idName = IDNAME ;

idIcon represents the icon we wish to display in Control Panel. We have not specified any value for lData. idName is the name which appears below the icon of the applet in the Control Panel. idInfo contains a string which is displayed in the status bar if icon is selected. Since idInfo and idName take the id of the string rather than the strings themselves, we have to create a string table using Resource Editor. Add the values to string table as given below.

ID

Value

Caption

IDINFO

1

Change Mouse Settings

IDNAME

2

Mouse Settings

  • CPL_NEWINQUIRE: This message also prompts CPlApplet( ) to provide information about the dialog box.. However, MSDN documentation suggests that you should process CPL_INQUIRE and not CPL_NEWINQUIRE.
  • CPL_STOP: Sent once for each dialog box before the controlling application closes. Memory allocated should be freed when this message is received.
  • CPL_EXIT: Sent after the last CPL_STOP message and immediately before the controlling application frees the DLL containing the applet.
  1. Declare CplApplet( ) in ‘mousecpl.h’ as follows:

LONG APIENTRY CPlApplet ( HWND cplhwnd, UINT umsg,LONG lparam1, LONG lparam2 ) ;

  1. Add export statement for CPlApplet( ) in the ‘setmouse.def’ file as given below:

; setmouse.def : Declares the module parameters for the DLL.

LIBRARY “setmouse”

DESCRIPTION ’setmouse Windows Dynamic Link Library’

EXPORTS

; Explicit exports can go here

CPlApplet

Compile and execute the project using Ctrl F5. When we do so the Control Panel window shown below would appear.

Img2

Double click the ‘Mouse Settings’ icon and you will see the dialog that will allow you to change the mouse settings.

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

UserOnline

Improve the web with Nofollow Reciprocity.

Powered by Yahoo! Answers