Introduction
Yesterday I published an article about template today custom item class CTodayWindow
. This class provides basic functionality but without option dialog support. This article gives developers the possibility to use CTodayWindow
class together with CTodayOptionsDialog
one which handles basic behavior of option dialog.
Back to CTodayWindow class
Source contains
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 default fonts for text being drawn on today window. These fonts are stored in member variables
m_hNormalTodayFont
and
m_hBoldTodayFont
. Default color for these two fonts should be one defined for today custom items and it's stored in member variable
m_crTodayText
. That's all for
CTodayWindow
class for now.
Background
The purpose of doing this was to write a class which would be similar to MFC CDialog
class. This class wraps basic functionality of option dialog for today custom items and defines basic behavior overridable by developer.
Abstract
Class CTodayOptionsDialog
is defined as follows:
Collapse class CTodayOptionsDialog
{
public:
HWND m_hWnd;
CTodayOptionsDialog();
CTodayOptionsDialog(HINSTANCE hInstance,
CTodayWindow *pToday, BOOL bFullScreen = TRUE);
virtual ~CTodayOptionsDialog();
HINSTANCE GetInstance() {return m_hInstance;};
BOOL GetFullScreen() {return m_bFullScreen;};
int GetTitleHeight() {return m_nTitleHeight;};
void SetInstance(HINSTANCE hInstance) {m_hInstance = hInstance;};
void SetFullScreen(BOOL bFullScreen) {m_bFullScreen = bFullScreen;};
void SetTitle(LPCTSTR lpszTitle, BOOL bRefresh = FALSE);
void SetTitle(UINT nID, BOOL bRefresh = FALSE);
void AssociateWithOptionsDlg(HWND hWnd);
void RefreshWindow();
virtual LRESULT CALLBACK TodayOptionsWndProc(HWND hDlg,
UINT uMsg, WPARAM wParam, LPARAM lParam);
protected :
CTodayWindow *m_pWndTW;
BOOL m_bFullScreen;
int m_nTitleHeight;
LPCTSTR m_lpszTitle;
HINSTANCE m_hInstance;
HFONT m_hTitleFont;
HWND ItemHandleFromID(UINT nID);
BOOL IsButtonChecked(UINT nID);
void CheckButton(UINT nID, BOOL bCheck = TRUE);
void CheckRadionButton(UINT nIDFirst, UINT nIDLast,
UINT nIDToCheck);
virtual void DrawDialogTitle(HDC hDC);
virtual void GetDefaultTitleFont();
virtual BOOL OnInitDialog(TODAYLISTITEM *ptli);
virtual void OnDestroy();
virtual void OnOK();
virtual void OnCancel();
virtual void OnPaint(HDC hDC);
virtual void OnActivate(UINT nState,
HWND hWndPrevious, BOOL bMinimized);
virtual void OnCommand(UINT nID, UINT nNotifyCode, HWND hWndCtrl);
virtual void OnSettingChange(UINT nFlags, LPCTSTR lpszSection);
virtual LRESULT OnNotify(UINT nID, NMHDR* pNMHDR);
virtual LRESULT OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
};
Basic class information
As you can see, I've pre-defined recently used messages into message handlers which are easy-to-use in derived classes. You don't need write anymore huge code in WndProc
and do the same stuff again and again. The main message loop is defined in:
LRESULT CALLBACK TodayOptionsWndProc(HWND hDlg,
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 used messages are:
WM_INITDIALOG
WM_DESTROY
WM_PAINT
WM_NOTIFY
WM_COMMAND
WM_ACTIVATE
WM_SETTINGCHANGE
These messages have their own message handlers. Special behavior have following handlers:
WM_PAINT
which firstly tries to draw dialog title. This title has to be set by SetTitle
method
Using the code
Usage of this class is very simple. Just derive your own class from CTodayOptionsDialog
class and define your own behavior. Then write main application logic as generally known (for example from MSDN). In DLLMain
function, create instance of your class when attaching library to process. In the constructor pass your CTodayWindow
derived class as parameter and set dialog title. Class CTodayOptionsDialog
has reference on current today custom item window stored in member variable m_pWndTW
. This allows the developer to access and work with today custom item window and change its attributes while setting options. The only thing needed is to cast this variable to correct today custom item window class derived from CTodayWindow
class as follows:
BOOL CMyOption::OnInitDialog(TODAYLISTITEM *ptli)
{
BOOL bResult = CTodayOptionsDialog::OnInitDialog(ptli);
CMyToday *pToday = (CMyToday*)m_pWndTW;
CheckButton(IDC_CHECK_SHOW_FIRST, pToday->m_bShowFirst);
CheckButton(IDC_CHECK_SHOW_SECOND, pToday->m_bShowSecond);
CheckButton(IDC_CHECK_SHOW_POWER, pToday->m_bShowPower);
return bResult;
}
void CMyOption::OnOK()
{
CMyToday *pToday = (CMyToday*)m_pWndTW;
pToday->m_bShowFirst = IsButtonChecked(IDC_CHECK_SHOW_FIRST);
pToday->m_bShowSecond = IsButtonChecked(IDC_CHECK_SHOW_SECOND);
pToday->m_bShowPower = IsButtonChecked(IDC_CHECK_SHOW_POWER);
CTodayOptionsDialog::OnOK();
}
Initialization of DLL library and example of connecting today custom item window class to its option dialog class is shown here:
Collapse static CMyToday* myToday;
static CMyOption *myOption;
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH :
myToday = new CMyToday((HINSTANCE)hModule,
_T("MyTodayClass"), _T("MyTodayWnd"));
myToday->SetItemHeight(40);
myToday->SetIcon(IDI_APP_ICON);
myToday->UnregisterTodayClass();
myOption = new CMyOption((HINSTANCE)hModule, myToday, TRUE);
myOption->SetTitle(_T("My Today Options"), FALSE);
break;
case DLL_PROCESS_DETACH :
delete myToday;
delete myOption;
break;
}
return TRUE;
}
HWND InitializeCustomItem(TODAYLISTITEM *ptli, HWND hWndParent)
{
myToday->RegisterTodayClass((WNDPROC)WndProc);
myToday->Create(hWndParent, WS_VISIBLE | WS_CHILD);
myToday->RefreshWindow(TRUE);
return myToday->m_hWnd;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
return myToday->TodayWndProc(uMsg, wParam, lParam);
}
LRESULT WINAPI CustomItemOptionsDlgProc(HWND hDlg,
UINT message, WPARAM wParam, LPARAM lParam)
{
return myOption->TodayOptionsWndProc(hDlg, message, wParam, lParam);
}
Remarks
These two classes co-working together give developers possibility of writing today custom items in a style "close to" MFC. Someone finds this code useful whereas another doesn't. For basic work with today custom items it's, as I think, enough.