Control Panel Applet using MFC

Environment: Wnidows NT4 SP5, Visual C++ 6 SP2

Ever wanted to make a Control Panel Applet using MFC but couldent get it to work?
Simlpy folow these steps:

1. Make a MFC DLL using the project wizard.

2. In the CWinApp header file define the folowing members:

//////////////////////////////////////////////////////
// PanelApp.h

class CPanelApp : public CWinApp
{
public:
 CPanelApp();

 // Static data
 static CPanelApp* m_pThis;

 // Static member functions (callbacks)
 static LONG APIENTRY CPlApplet(HWND hWnd, 
  UINT uMsg, LONG lParam1, LONG lParam2);

 LONG OnDblclk(HWND hWnd, UINT uAppNum, LONG lData); 
 LONG OnExit();
 LONG OnGetCount();
 LONG OnInit();
 LONG OnInquire(UINT uAppNum, CPLINFO* pInfo);
 LONG OnNewInquire(UINT uAppNum, NEWCPLINFO* pInfo); 
 LONG OnStop(UINT uAppNum, LONG lData);
};

2. In the CWinApp source file add the folowing code:

//////////////////////////////////////////////////////
// PanelApp.cpp


// Static data initialization
CPanelApp* CPanelApp::m_pThis = NULL;

// CPanelApp construction
CPanelApp::CPanelApp()
{
	m_pThis = this;
}

// Callback members
LONG APIENTRY CPanelApp::CPlApplet(HWND hWnd, UINT uMsg, 
 LONG lParam1, LONG lParam2)
{
 // Avoids state problems in MFC extensions using 
 // shared MFC libs.
 AFX_MANAGE_STATE(AfxGetStaticModuleState()); 

 CPanelApp* pApplet = m_pThis;
 ASSERT(pApplet);

 switch (uMsg) 
 {
  case CPL_DBLCLK:
  return pApplet->OnDblclk(hWnd, lParam1, lParam2);

  case CPL_EXIT:
  return pApplet->OnExit();

  case CPL_GETCOUNT:
  return pApplet->OnGetCount();

  case CPL_INIT:
  return pApplet->OnInit();

  case CPL_INQUIRE:
  return pApplet->OnInquire(lParam1, (CPLINFO*)lParam2);
  
  case CPL_NEWINQUIRE:
  return pApplet->OnNewInquire(lParam1, 
   (NEWCPLINFO*)lParam2);

  case CPL_STOP:
  return pApplet->OnStop(lParam1, lParam2);

  case CPL_STARTWPARMS:
  return pApplet->OnDblclk(hWnd, lParam1, lParam2);

  default:
  break;
 }

 return 1;
}

// Default command handlers

LONG CPanelApp::OnNewInquire(UINT uAppNum, 
 NEWCPLINFO* pInfo) 
{    
 // Fill in the data
 pInfo->dwSize		  = sizeof(NEWCPLINFO);
 pInfo->dwFlags		  = 0;
 pInfo->dwHelpContext = 0;
 pInfo->lData		  = 0;
 pInfo->hIcon		  = LoadIcon(IDI_MAINFRM);

 CString sCplName, sCplInfo;
 sCplName.LoadString(IDS_CPL_NAME);
 sCplInfo.LoadString(IDS_CPL_INFOSTRING);

 _tcscpy(pInfo->szName, sCplName);
 _tcscpy(pInfo->szInfo, sCplInfo);
 _tcscpy(pInfo->szHelpFile, _T(""));

 return 0;
}

LONG CPanelApp::OnInquire(UINT uAppNum, CPLINFO* pInfo)
{
 pInfo->idIcon	= IDI_MAINFRM;
 pInfo->idName	= IDS_CPL_NAME;
 pInfo->idInfo	= IDS_CPL_INFOSTRING;
 pInfo->lData	= 0;

 return 0;
}

LONG CPanelApp::OnDblclk(HWND hWnd, UINT uAppNum, 
 LONG lData)
{
 // Insert your Control Panel Applet dialog (property 
 // sheet) code here

 return 0;
}

LONG CPanelApp::OnExit()
{
 return 0; // OK
}

LONG CPanelApp::OnGetCount()
{
 return 1; // only one applet
}

LONG CPanelApp::OnInit()
{
 return 1; // OK
}

LONG CPanelApp::OnStop(UINT uAppNum, LONG lData) 
{
 return 1; // not handled
}

3. Finaly add the folowing to the projects *.def file

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

LIBRARY      "Panel"
DESCRIPTION  'Panel Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
	
	CPlApplet

Thats all there is too it!



Comments

  • Question: How to interface CPIApplet in VB

    Posted by Legacy on 02/11/2002 12:00am

    Originally posted by: EduardoS

    Hello People ...

    I wanna know how to interfaces this code with VB ... I need create an Control Panel Applet in VB but I dont know C ...

    Thanks!

    Reply
  • Use RUNDLL32 to Debug Control Panel Applets

    Posted by Legacy on 01/10/2001 12:00am

    Originally posted by: Ernest L.

    You may want to debug a control panel application
    using this tip:
    Executable for debug session:
    C:\winnt\system32\rundll32.exe (or put your own path)
    Program arguments:
    shell32.dll,Control_RunDLL <cpl application path>

    or bookmark this faqs:
    http://support.microsoft.com/support/kb/articles/Q166/1/68.ASP

    Reply
  • See a book "Professioal NT Services' by Kevin Miller

    Posted by Legacy on 03/25/2000 12:00am

    Originally posted by: Andrei Romanov

    Exactly the same code was published in chapter 11 of "Professioal NT Services' by Kevin Miller, WROX, ISBN 1-861001-30-4.

    Reply
  • Not bad

    Posted by Legacy on 03/01/2000 12:00am

    Originally posted by: Paul Wardle

    Thanks for the code. Works well.

    Reply
  • Please post an example source code project

    Posted by Legacy on 02/04/2000 12:00am

    Originally posted by: Ed E. Sutton

    A nice article.

    Please post an example project. Examples always get me started much faster.

    -Ed

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds