Click here to Skip to main content
5,790,650 members and growing! (19,897 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

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

By eXEden

An article on writing custom today items using CTodayOptionsDialog and CTodayWindow classes.
C++, eVC 3.0, eVC, Windows, Mobile, CE 3.0, Win Mobile, Visual Studio, Dev

Posted: 11 Aug 2003
Updated: 11 Aug 2003
Views: 56,791
Bookmarked: 31 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.92 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
3 votes, 17.6%
3
3 votes, 17.6%
4
11 votes, 64.7%
5

Sample Image - TodayOptionsDialog.jpg

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:

class CTodayOptionsDialog  
{
public:
    HWND m_hWnd;

    CTodayOptionsDialog();
    CTodayOptionsDialog(HINSTANCE hInstance, 
       CTodayWindow *pToday, BOOL bFullScreen = TRUE);
    virtual ~CTodayOptionsDialog();

    // Get methods

    HINSTANCE GetInstance() {return m_hInstance;};
    BOOL GetFullScreen() {return m_bFullScreen;};
    int GetTitleHeight() {return m_nTitleHeight;};

    // Set methods

    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);

    // Association with option dialog created by system

    void AssociateWithOptionsDlg(HWND hWnd);

    void RefreshWindow();

    virtual LRESULT CALLBACK TodayOptionsWndProc(HWND hDlg, 
                   UINT uMsg, WPARAM wParam, LPARAM lParam);
protected :
    // Reference to Today plug-in window

    CTodayWindow *m_pWndTW;

    BOOL m_bFullScreen;
    int m_nTitleHeight;

    LPCTSTR m_lpszTitle;

    HINSTANCE m_hInstance;
    HFONT m_hTitleFont;

    // Methods for better work with controls

    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();

    // Message handlers

    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:

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

eXEden



Occupation: Web Developer
Location: Czech Republic Czech Republic

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralAssociateWithOptionsDlgmemberJonathan Rosanowski10:54 20 Nov '08  
Generalhelp me!!!memberAndrew823:12 2 Apr '08  
QuestionPropertysheet question [modified]memberMark Rekveld6:29 5 Aug '07  
QuestionDo you have any sample code for Today plug-in in C# language?memberLukecao23:15 9 Jun '07  
GeneralToday Item disapearingmemberJosh Booth6:13 19 May '05  
GeneralRe: Today Item disapearingmemberzz_smith1:21 4 Jul '06  
GeneralSomething wrong with Today DLL (MFC)memberkenh5991:25 18 May '05  
GeneralGreat demo and 1 QuestionmemberKenh5991:13 18 May '05  
GeneralI want badly to maken a Today item for myself bud it wont work.membertne7laa6:45 16 Apr '05  
GeneralResource ErrormemberHarry Lau15:13 12 Dec '04  
GeneralRe: Resource ErrormemberSang Yam6:02 17 Jan '05  
GeneralDinamicaly Change HeightmemberOscar Carrasco A5:57 28 Aug '04  
GeneralRe: Dinamicaly Change HeightmemberDaniel Jin15:53 31 Mar '05  
GeneralPlease make it Pocket Facelift compatible (themes for PPC2000 and many enhancements)member_ms_4:49 14 Aug '03  
GeneralGreat Article but one questionmembermattfreeman9:17 2 May '04  
GeneralRe: Great Article but one questionmemberDaniel Jin15:51 31 Mar '05  
GeneralGood article for CEmembersanong21:37 12 Aug '03  
GeneralRe: Good article for CEmembereXEden22:45 12 Aug '03  
GeneralRe: Good article for CEmembersanong22:50 12 Aug '03  
GeneralRe: Good article for CEmembereXEden0:20 13 Aug '03  
GeneralRe: Good article for CEmemberting6686:12 20 Jun '04  
GeneralIt don't workmemberglobalnet23:25 26 Oct '04  
GeneralRe: It don't workmemberDaniel Jin15:54 31 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Aug 2003
Editor: Smitha Vijayan
Copyright 2003 by eXEden
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project