December 19, 2014
Hot Topics:

Windows Mobile Development Made Easier with WTL

  • April 2, 2007
  • By Alex Gusev
  • Send Email »
  • More Articles »

Using WTL Classes

Although using various WTL View classes is quite straightforward, let me illustrate how to use control-like classes your code. There are couple of techniques to do so. You will see them right now.

The simplest case is when you don't need to alter control's behavior; in other words, its WinProc may be left unchanged. For instance, if you need to connect CEdit object to EDIT dialog control, all you need to do is this:

// Attach our object to dialog control
   m_Edit = ::GetDlgItem(hwnd, IDC_EDIT1);
// call some function to illustrate the usage
   m_Edit.SetReadOnly();

As you can see, that's quite simple. This way, you can add an object-oriented style even to a Win32 application.

Another method should be used when you want to change the control's behavior in any way. This is well known as subclassing. As a more complicated sample to illustrate this method, consider the CBitmapButton control, which is originally not supported under Windows CE both in WTL and MFC 8.0. On my opinion, that's a real pity, so I have decided to revive it back.

CBitmapButton Backport

The actual job of adding bitmap button support for Windows Mobile is not that hard, so once you have copied its code from the AtlCtrlx.h header and removed all unnecessary message handlers, that's it. For the purposes of this article, you will see how to use this CBitmapButton in the next paragraphs.

So, back to subclassing, the CBitmapButtonImpl class contains the following overloaded implementation of SubclassWindow:

// overridden to provide proper initialization
BOOL SubclassWindow(HWND hWnd)
{
   #if (_MSC_VER >= 1300)
      BOOL bRet = ATL::CWindowImpl< T, TBase,
                  TWinTraits>::SubclassWindow(hWnd);
   #else // !(_MSC_VER >= 1300)
      typedef ATL::CWindowImpl< T, TBase, TWinTraits>  _baseClass;
      BOOL bRet = _baseClass::SubclassWindow(hWnd);
   endif    // !(_MSC_VER >= 1300)
      if(bRet)
         Init();
      return bRet;
}

The above function calls the base class' implementation and makes an additional initialization. I've added one more simple function to this chain

BOOL AttachToDlgItem(HWND hwndParent, UINT nDlgID)
{
   m_nDlgItem   = nDlgID;
   m_hwndParent = hwndParent;
   return SubclassWindow(::GetDlgItem(hwndParent,nDlgID));
}

to simplify manipulating and further maintaining the CBitmapButton control. All that left is to provide a few images and off you go!

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/,
                               LPARAM /*lParam*/,
                               BOOL& /*bHandled*/)
{
   // Skip irrelevant code here
   ...
      // Attach to OK button
      m_btnOK.AttachToDlgItem(m_hWnd,IDOK);
      m_btnOK.SetImages(0,1);
   // Attach to Cancel button
      m_btnCancel.AttachToDlgItem(m_hWnd,IDCANCEL);
      m_btnCancel.SetImages(0,1);
   // Attach to About button
      m_btnAbout.AttachToDlgItem(m_hWnd,ID_APP_ABOUT);
      m_btnAbout.SetImages(0,1);

      WTL::CBitmap bmp;
      m_ImageList.Create(64, 31, ILC_COLOR, 0, 2);

      bmp.LoadBitmap(IDB_BITMAP_N);
      m_ImageList.Add((HBITMAP)bmp);
      bmp.DeleteObject();

      bmp.LoadBitmap(IDB_BITMAP_F);
      m_ImageList.Add((HBITMAP)bmp);
      bmp.DeleteObject();

      m_btnOK.SetImageList(m_ImageList);
      m_btnCancel.SetImageList(m_ImageList);
      m_btnAbout.SetImageList(m_ImageList);

      return TRUE;
}

Here is the result of your efforts:

Conclusion

The WTL library is still alive. And now, it gets even more fuel with WinCE support. This article gives you a brief introduction to get you started quickly. If you search on the web, you can find many resources to provide even more help than comes with WTL wizards. Here is just one example, but your own hands-on experience is the best way to learn it all. Thus, come on and use it in your applications!

Download

Download the accompanying code's zip file here.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. After working almost a decade for an international retail software company as a team leader of the Windows Mobile R department, he has decided to dive into Symbian OS ™ Core development.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel