Click here to Skip to main content
14,846,832 members
Articles / Mobile Apps
Posted 11 Aug 2003


35 bookmarked

CTodayOptionsDialog - let's continue writing today's custom items in sequence to the CTodayWindow class

Rate me:
Please Sign up or sign in to vote.
4.25/5 (17 votes)
11 Aug 2003CPOL3 min read
An article on writing custom today items using the CTodayOptionsDialog and CTodayWindow classes.

Sample Image - TodayOptionsDialog.jpg


Yesterday, I published an article about a template today custom item class, CTodayWindow. This class provides basic functionality, but without option dialog support. This article gives developers the possibility to use the CTodayWindow class together with CTodayOptionsDialog, one which handles the basic behavior of the option dialog.

Back to CTodayWindow class

The source contains the CTodayWindow class as well. I've made some changes to this class. First of all, message handling in TodayWndProc has been improved as well as getting the default fonts for the text being drawn on the today window. These fonts are stored in the member variables m_hNormalTodayFont and m_hBoldTodayFont. The default color for these two fonts should be one defined for the today custom items, and it's stored in the member variable m_crTodayText. That's all for the CTodayWindow class for now.


The purpose of doing this was to write a class which would be similar to the MFC CDialog class. This class wraps the basic functionality of the option dialog for the today custom items and defines the basic behavior overridable by the developer.


The class CTodayOptionsDialog is defined as follows:

  1  class CTodayOptionsDialog  
  2  {
  3  public:
  4      HWND m_hWnd;
  6      CTodayOptionsDialog();
  7      CTodayOptionsDialog(HINSTANCE hInstance, 
  8         CTodayWindow *pToday, BOOL bFullScreen = TRUE);
  9      virtual ~CTodayOptionsDialog();
 11      // Get methods
 12      HINSTANCE GetInstance() {return m_hInstance;};
 13      BOOL GetFullScreen() {return m_bFullScreen;};
 14      int GetTitleHeight() {return m_nTitleHeight;};
 16      // Set methods
 17      void SetInstance(HINSTANCE hInstance) {m_hInstance = hInstance;};
 18      void SetFullScreen(BOOL bFullScreen) {m_bFullScreen = bFullScreen;};
 19      void SetTitle(LPCTSTR lpszTitle, BOOL bRefresh = FALSE);
 20      void SetTitle(UINT nID, BOOL bRefresh = FALSE);
 22      // Association with option dialog created by system
 23      void AssociateWithOptionsDlg(HWND hWnd);
 25      void RefreshWindow();
 27      virtual LRESULT CALLBACK TodayOptionsWndProc(HWND hDlg, 
 28                     UINT uMsg, WPARAM wParam, LPARAM lParam);
 29  protected :
 30      // Reference to Today plug-in window
 31      CTodayWindow *m_pWndTW;
 33      BOOL m_bFullScreen;
 34      int m_nTitleHeight;
 36      LPCTSTR m_lpszTitle;
 38      HINSTANCE m_hInstance;
 39      HFONT m_hTitleFont;
 41      // Methods for better work with controls
 42      HWND ItemHandleFromID(UINT nID);
 43      BOOL IsButtonChecked(UINT nID);
 44      void CheckButton(UINT nID, BOOL bCheck = TRUE);
 45      void CheckRadionButton(UINT nIDFirst, UINT nIDLast, 
 46                                         UINT nIDToCheck);
 48      virtual void DrawDialogTitle(HDC hDC);
 49      virtual void GetDefaultTitleFont();
 51      // Message handlers
 52      virtual BOOL OnInitDialog(TODAYLISTITEM *ptli);
 53      virtual void OnDestroy();
 54      virtual void OnOK();
 55      virtual void OnCancel();
 56      virtual void OnPaint(HDC hDC);
 57      virtual void OnActivate(UINT nState, 
 58              HWND hWndPrevious, BOOL bMinimized);
 59      virtual void OnCommand(UINT nID, UINT nNotifyCode, HWND hWndCtrl);
 60      virtual void OnSettingChange(UINT nFlags, LPCTSTR lpszSection);
 61      virtual LRESULT OnNotify(UINT nID, NMHDR* pNMHDR);
 62      virtual LRESULT OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
 63  };

Basic class information

As you can see, I've pre-defined the recently used messages into message handlers which are easy-to-use in the derived classes. You don't need to write anymore code in WndProc and do the same stuff again and again. The main message loop is defined in:

  1  LRESULT CALLBACK TodayOptionsWndProc(HWND hDlg, 
  2                 UINT uMsg, WPARAM wParam, LPARAM lParam)

This method handles some basic messages, and I've defined some virtual methods you can override. From my point of view, the most commonly used messages are:


These messages have their own message handlers. Special behavior have the following handlers:

  • WM_PAINT which firstly tries to draw the dialog title. This title has to be set by the SetTitle method.

Using the code

Using this class is very simple. Just derive your own class from the CTodayOptionsDialog class and define your own behavior. Then, write the main application logic as generally known (for example, from MSDN). In the DLLMain function, create an instance of your class when attaching a library to process. In the constructor, pass your CTodayWindow derived class as the parameter and set the dialog title. The class CTodayOptionsDialog has a reference on the current today custom item window stored in the member variable m_pWndTW. This allows the developer to access and work with the today custom item window and change its attributes while setting options. The only thing needed is to cast this variable to correct the today custom item window class derived from the CTodayWindow class, as follows:

  1  BOOL CMyOption::OnInitDialog(TODAYLISTITEM *ptli)
  2  {
  3      BOOL bResult = CTodayOptionsDialog::OnInitDialog(ptli);
  5      CMyToday *pToday = (CMyToday*)m_pWndTW;
  7      CheckButton(IDC_CHECK_SHOW_FIRST, pToday->m_bShowFirst);
  8      CheckButton(IDC_CHECK_SHOW_SECOND, pToday->m_bShowSecond);
  9      CheckButton(IDC_CHECK_SHOW_POWER, pToday->m_bShowPower);
 11      return bResult;
 12  }
 14  void CMyOption::OnOK()
 15  {
 16      CMyToday *pToday = (CMyToday*)m_pWndTW;
 18      pToday->m_bShowFirst = IsButtonChecked(IDC_CHECK_SHOW_FIRST);
 19      pToday->m_bShowSecond = IsButtonChecked(IDC_CHECK_SHOW_SECOND);
 20      pToday->m_bShowPower = IsButtonChecked(IDC_CHECK_SHOW_POWER);
 22      CTodayOptionsDialog::OnOK();
 23  }

Initialization of the DLL library and an example of connecting the today custom item window class to its option dialog class is shown here:

  1  static CMyToday* myToday;
  2  static CMyOption *myOption;
  4  BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
  5  {
  6      switch (dwReason)
  7      {
  8      case DLL_PROCESS_ATTACH :
  9          myToday = new CMyToday((HINSTANCE)hModule, 
 10                     _T("MyTodayClass"), _T("MyTodayWnd"));
 11          myToday->SetItemHeight(40);
 12          myToday->SetIcon(IDI_APP_ICON);
 13          myToday->UnregisterTodayClass();
 15          myOption = new CMyOption((HINSTANCE)hModule, myToday, TRUE);
 16          myOption->SetTitle(_T("My Today Options"), FALSE);
 18          break;
 19      case DLL_PROCESS_DETACH :
 20          delete myToday;
 21          delete myOption;
 22          break;
 23      }
 25      return TRUE;
 26  }
 28  HWND InitializeCustomItem(TODAYLISTITEM *ptli, HWND hWndParent)
 29  {
 30      myToday->RegisterTodayClass((WNDPROC)WndProc);
 31      myToday->Create(hWndParent, WS_VISIBLE | WS_CHILD);
 32      myToday->RefreshWindow(TRUE);
 34      return myToday->m_hWnd;
 35  }
 38                        WPARAM wParam, LPARAM lParam)
 39  {
 40      return myToday->TodayWndProc(uMsg, wParam, lParam);
 41  }
 43  LRESULT WINAPI CustomItemOptionsDlgProc(HWND hDlg, 
 44              UINT message, WPARAM wParam, LPARAM lParam)
 45  {
 46      return myOption->TodayOptionsWndProc(hDlg, message, wParam, lParam);
 47  }


These two classes working together gives developers the possibility of writing today custom items in a style "close to" that of MFC. Someone might find this code useful whereas another might not. For basic working with today custom items, this, I think, is enough.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Czech Republic Czech Republic
No Biography provided

Comments and Discussions

GeneralAssociateWithOptionsDlg Pin
Jonathan Rosanowski20-Nov-08 9:54
MemberJonathan Rosanowski20-Nov-08 9:54 
Generalhelp me!!! Pin
Andrew822-Apr-08 2:12
MemberAndrew822-Apr-08 2:12 
QuestionPropertysheet question [modified] Pin
Mark Rekveld5-Aug-07 5:29
MemberMark Rekveld5-Aug-07 5:29 
QuestionDo you have any sample code for Today plug-in in C# language? Pin
Lukecao9-Jun-07 22:15
MemberLukecao9-Jun-07 22:15 
GeneralToday Item disapearing Pin
Josh Booth19-May-05 5:13
MemberJosh Booth19-May-05 5:13 
GeneralRe: Today Item disapearing Pin
zz_smith4-Jul-06 0:21
Memberzz_smith4-Jul-06 0:21 
GeneralSomething wrong with Today DLL (MFC) Pin
Member 195646018-May-05 0:25
MemberMember 195646018-May-05 0:25 
GeneralGreat demo and 1 Question Pin
Member 195646018-May-05 0:13
MemberMember 195646018-May-05 0:13 
GeneralI want badly to maken a Today item for myself bud it wont work. Pin
tne7laa16-Apr-05 5:45
Membertne7laa16-Apr-05 5:45 
GeneralResource Error Pin
Harry Lau12-Dec-04 14:13
MemberHarry Lau12-Dec-04 14:13 
GeneralRe: Resource Error Pin
Sang Yam17-Jan-05 5:02
MemberSang Yam17-Jan-05 5:02 
GeneralDinamicaly Change Height Pin
Oscar Carrasco A28-Aug-04 4:57
MemberOscar Carrasco A28-Aug-04 4:57 
GeneralRe: Dinamicaly Change Height Pin
Daniel Jin31-Mar-05 14:53
MemberDaniel Jin31-Mar-05 14:53 
GeneralPlease make it Pocket Facelift compatible (themes for PPC2000 and many enhancements) Pin
_ms_14-Aug-03 3:49
Member_ms_14-Aug-03 3:49 
GeneralGreat Article but one question Pin
mattfreeman2-May-04 8:17
Membermattfreeman2-May-04 8:17 
GeneralRe: Great Article but one question Pin
Daniel Jin31-Mar-05 14:51
MemberDaniel Jin31-Mar-05 14:51 
GeneralGood article for CE Pin
sanong12-Aug-03 20:37
Membersanong12-Aug-03 20:37 
GeneralRe: Good article for CE Pin
eXEden12-Aug-03 21:45
MembereXEden12-Aug-03 21:45 
GeneralRe: Good article for CE Pin
sanong12-Aug-03 21:50
Membersanong12-Aug-03 21:50 
GeneralRe: Good article for CE Pin
eXEden12-Aug-03 23:20
MembereXEden12-Aug-03 23:20 
GeneralRe: Good article for CE Pin
ting66820-Jun-04 5:12
Memberting66820-Jun-04 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.