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

CTodayWindow - template class for creating custom today items

By eXEden

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

Posted: 10 Aug 2003
Updated: 10 Aug 2003
Views: 82,026
Bookmarked: 34 times
Announcements
Loading...



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

Sample Image - CTodayWindow.jpg

Introduction

When writing a custom today item, we always spend a lot of time on writing code that is repeatable and even less "user-friendly". I've tried to wrap standard today custom item into a class which could be reused and is more comfortable for developers.

Background

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

Abstract

Class CTodayWindow is defined as follows:

class CTodayWindow  
{
public:
    // Member Variables

    HWND m_hWnd;

    // Methods

    CTodayWindow();
    CTodayWindow(HINSTANCE hInstance, 
       LPCTSTR lpszClassName, LPCTSTR lpszWindowName);
    virtual ~CTodayWindow();

    // Main Create method

    BOOL Create(HWND hWndParent, 
       DWORD dwStyle = WS_VISIBLE | WS_CHILD);

    // Update Window

    void RefreshWindow(BOOL bShow = FALSE);

    // Set Methods

    BOOL SetIcon(UINT uID, int xDrawAt = 2, int yDrawAt = 0);
    void SetItemHeight(int nHeight);
    void SetClassInfo(LPCTSTR lpszClassName, LPCTSTR lpszWindowName);
    void SetInstance(HINSTANCE hInstance);

    // Get Methods

    HWND GetParent() {return m_hParentHwnd;};
    int GetItemHeight() {return m_nHeight;};
    HINSTANCE GetInstance() {return m_hInstance;};
    HICON GetIcon() {return m_hIcon;};
    LPCTSTR GetClassName() {return m_lpszClassName;};
    LPCTSTR GetWindowName() {return m_lpszWindowName;};

    // Register/Unregister TodayWindow

    void RegisterTodayClass(WNDPROC wndProc);
    void UnregisterTodayClass();

    // TodayWndProc - main message loop

    virtual LRESULT CALLBACK TodayWndProc(UINT uMsg, 
       WPARAM wParam, LPARAM lParam);
protected:
    BOOL m_bInitialRefresh;

    int m_nHeight;
    POINT m_pointIconPos;

    HWND m_hParentHwnd;
    HICON m_hIcon;
    HINSTANCE m_hInstance;

    LPCTSTR m_lpszClassName;
    LPCTSTR m_lpszWindowName;

    COLORREF m_crTodayText;
    HFONT m_hNormalTodayFont;
    HFONT m_hBoldTodayFont;

    virtual void DrawTodayIcon(HDC hDC, int xPos, int yPos);
    virtual void GetTodayDefaults();

    // Message handlers

    virtual int OnCreate(LPCREATESTRUCT lpCreateStruct);
    virtual void OnDestroy();
    virtual void OnPaint(HDC hDC);
    virtual void OnEraseBkgnd(HDC hDC);
    virtual void OnTodayCustomQueryRefreshCache
        (TODAYLISTITEM *pTodayListItem, BOOL *pResult);
    virtual BOOL OnTodayCustomClearCache(TODAYLISTITEM *pTodayListItem);
    virtual void OnLButtonUp(UINT nFlags, POINT point);
    virtual void OnLButtonDown(UINT nFlags, POINT point);
    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 TodayWndProc(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 message are:

  • WM_CREATE
  • WM_DESTROY
  • WM_PAINT
  • WM_ERASEBKGND
  • WM_LBUTTONDOWN
  • WM_LBUTTONUP
  • WM_TODAYCUSTOM_CLEARCACHE
  • WM_TODAYCUSTOM_QUERYREFRESHCACHE
  • WM_SETTINGCHANGE

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

  • WM_PAINT which firstly tries to draw icon assigned to window. This icon has to be set by SetIcon method
  • WM_TODAYCUSTOM_QUERYREFRESHCACHE which recognizes initialization of today custom item and sets correct item height stored in m_nHeight and set by SetItemHeight method
  • WM_ERASEBKGND message handler OnEraseBkgnd draws transparent background for item. This standard behavior can be overridden by the developer.

Creation of today custom item window is handled in:

BOOL Create(HWND hWndParent, DWORD dwStyle = WS_VISIBLE | WS_CHILD)

method. This method creates a window with following attributes:

  • style passed in dwStyle parameter
  • parent window passed in hWndParent parameter
  • rectangle initially set to left = 0, top = 0, width = screen width, height = 0
  • class and window name passed as attributes in constructor or set by SetClassInfo method

This class provides today custom item window class (un)registration as well.

  • Class registration is provided by void RegisterTodayClass(WNDPROC wndProc) method. Parameter is main window procedure defined in your today custom item application.
  • Class unregistration is provided by void UnregisterTodayClass()

Using the code

Usage of this class is very simple. Just derive your own class from CTodayWindow class and define your today custom item 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. Set common attributes like item height, item icon etc. In InitializeCustomItem function, register your class and create today custom item window by calling Create method. And that's all. Here is the sample code:

// Your derived class

static CMyToday* myToday;

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

        break;
    case DLL_PROCESS_DETACH :
        myToday->UnregisterTodayClass();
        delete myToday;
        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);
}

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 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
GeneralHow to get Today Screen's HWND?memberlifecat921:33 4 Sep '07  
GeneralSelection of the custom today item - without touchscreenmemberMikael Braad Nielsen6:14 12 Feb '07  
GeneralRe: Selection of the custom today item - without touchscreenmembersiri_s2:23 14 Feb '07  
GeneralRe: Selection of the custom today item - without touchscreenmemberMikael Braad Nielsen0:44 16 Feb '07  
GeneralRe: Selection of the custom today item - without touchscreenmembersiri_s18:46 19 Feb '07  
GeneralOther comments from Scott Huntermembersiri_s18:06 20 Feb '07  
AnswerProblem solvedmemberMikael Braad Nielsen10:08 6 Jun '07  
GeneralThanks for your article.memberIain Clarke4:52 8 Feb '07  
GeneralRe: Thanks for your article.membernemanjas23:21 30 Mar '08  
GeneralError in CMyToday::~CMyToday()memberisemenov5:53 4 Dec '06  
GeneralWindow Mobile 2005memberbm_masri8:10 29 Jan '06  
GeneralHandle WM_KILLFOCUSsussBas Spymac3:34 4 Jul '05  
GeneralMFC?membercteel0411:55 3 Apr '05  
GeneralDoes it work????memberWaveFront Technologies, Inc.19:38 1 Oct '04  
GeneralRe: Does it work????memberDaniel Jin15:47 31 Mar '05  
GeneralHow to get Today Item Window Handle ?membersmoh10:43 23 Jul '04  
GeneralRe: How to get Today Item Window Handle ?memberDaniel Jin15:31 31 Mar '05  
GeneralRe: How to get Today Item Window Handle ?memberlifecat917:52 4 Sep '07  
GeneralRe: How to get Today Item Window Handle ?memberjohnwoo92816:33 16 Aug '05  
Generaladd content dynamicallymemberting6682:14 20 Jun '04  
GeneralRe: add content dynamicallymemberDaniel Jin15:27 31 Mar '05  
GeneralOnCreate m_hWnd still NULLmemberR_S11:21 14 Jun '04  
GeneralRe: OnCreate m_hWnd still NULLmemberDaniel Jin15:21 31 Mar '05  
Generalmissing = in if(m_hWnd==NULL)memberR_S11:16 14 Jun '04  
GeneralMistake in ctormemberBugaboo2:21 13 Apr '04  

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

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