CBitmapButton?

Hi,

Today when i made an application using CBitmapButton?
i met this notification:

uses undefined class 'CBitmapButton'

Visual studio 2005 for pocket pc 2005 isn't supported this class.
What is the purpose of it?

JT

[270 byte] By [JulienT] at [2008-2-12]
# 1
Hi,experts

I tried to make a source code for CBitmapButton like below:
I tested, it run and can paste the Bitmap on the button, can
you test and fix the bug ( i test OK with LoadBitmaps )?
// this is file: x.h

#ifndef _CBitmapButton_H
#define _CBitmapButton_H

#include "windows.h"

#include "stdlib.h"

#include "stdio.h"

#include "afxext.h"

class CBitmapButton; // Bitmap button (self-draw)

/////////////////////////////////////////////////////////////////////////////

// Simple bitmap button

// CBitmapButton - push-button with 1->4 bitmap images

class CBitmapButton : public CButton

{

//DECLARE_DYNAMIC(CBitmapButton)

public:

// Construction

/* JT */

//CBitmapButton();

BOOL LoadBitmaps(LPCTSTR lpszBitmapResource,

LPCTSTR lpszBitmapResourceSel = NULL,

LPCTSTR lpszBitmapResourceFocus = NULL,

LPCTSTR lpszBitmapResourceDisabled = NULL);

BOOL LoadBitmaps(UINT nIDBitmapResource,

UINT nIDBitmapResourceSel = 0,

UINT nIDBitmapResourceFocus = 0,

UINT nIDBitmapResourceDisabled = 0);

BOOL AutoLoad(UINT nID, CWnd* pParent);

// Operations

void SizeToContent();

// Implementation:

public:

#ifdef _DEBUG

// virtual void AssertValid() const;

// virtual void Dump(CDumpContext& dc) const;

#endif

protected:

// all bitmaps must be the same size

CBitmap m_bitmap; // normal image (REQUIRED)

CBitmap m_bitmapSel; // selected image (OPTIONAL)

CBitmap m_bitmapFocus; // focused but not selected (OPTIONAL)

CBitmap m_bitmapDisabled; // disabled bitmap (OPTIONAL)

virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);

};

#endif

// this is file x.c
#include "x.h"

//#include "afx.h"

/////////////////////////////////////////////////////////////////////////////

// CBitmapButton

// LoadBitmaps will load in one, two, three or all four bitmaps

// returns TRUE if all specified images are loaded

BOOL CBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource,

LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,

LPCTSTR lpszBitmapResourceDisabled)

{

// delete old bitmaps (if present)

m_bitmap.DeleteObject();

m_bitmapSel.DeleteObject();

m_bitmapFocus.DeleteObject();

m_bitmapDisabled.DeleteObject();

if (!m_bitmap.LoadBitmap(lpszBitmapResource))

{

// TRACE(traceAppMsg, 0, "Failed to load bitmap for normal image.\n");

return FALSE; // need this one image

}

BOOL bAllLoaded = TRUE;

if (lpszBitmapResourceSel != NULL)

{

if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))

{

// TRACE(traceAppMsg, 0, "Failed to load bitmap for selected image.\n");

bAllLoaded = FALSE;

}

}

if (lpszBitmapResourceFocus != NULL)

{

if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))

bAllLoaded = FALSE;

}

if (lpszBitmapResourceDisabled != NULL)

{

if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))

bAllLoaded = FALSE;

}

return bAllLoaded;

}

/* JT */

// LoadBitmaps will load in one, two, three or all four bitmaps

// returns TRUE if all specified images are loaded

BOOL CBitmapButton::LoadBitmaps(UINT nIDBitmapResource,

UINT nIDBitmapResourceSel,

UINT nIDBitmapResourceFocus,

UINT nIDBitmapResourceDisabled)

{

// delete old bitmaps (if present)

m_bitmap.DeleteObject();

m_bitmapSel.DeleteObject();

m_bitmapFocus.DeleteObject();

m_bitmapDisabled.DeleteObject();

if (!m_bitmap.LoadBitmap(nIDBitmapResource))

{

// TRACE(traceAppMsg, 0, "Failed to load bitmap for normal image.\n");

return FALSE; // need this one image

}

BOOL bAllLoaded = TRUE;

if (nIDBitmapResourceSel != NULL)

{

if (!m_bitmapSel.LoadBitmap(nIDBitmapResourceSel))

{

// TRACE(traceAppMsg, 0, "Failed to load bitmap for selected image.\n");

bAllLoaded = FALSE;

}

}

if (nIDBitmapResourceFocus != NULL)

{

if (!m_bitmapFocus.LoadBitmap(nIDBitmapResourceFocus))

bAllLoaded = FALSE;

}

if (nIDBitmapResourceDisabled != NULL)

{

if (!m_bitmapDisabled.LoadBitmap(nIDBitmapResourceDisabled))

bAllLoaded = FALSE;

}

return bAllLoaded;

}

// SizeToContent will resize the button to the size of the bitmap

void CBitmapButton::SizeToContent()

{

ASSERT(m_bitmap.m_hObject != NULL);

CSize bitmapSize;

BITMAP bmInfo;

VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));

VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight,

SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));

}

// Autoload will load the bitmap resources based on the text of

// the button

// Using suffices "U", "D", "F" and "X" for up/down/focus/disabled

BOOL CBitmapButton::AutoLoad(UINT nID, CWnd* pParent)

{

// first attach the CBitmapButton to the dialog control

if (!SubclassDlgItem(nID, pParent))

return FALSE;

CString buttonName;

GetWindowText(buttonName);

ASSERT(!buttonName.IsEmpty()); // must provide a title

LoadBitmaps(buttonName + _T("U"), buttonName + _T("D"),

buttonName + _T("F"), buttonName + _T("X"));

// we need at least the primary

if (m_bitmap.m_hObject == NULL)

return FALSE;

// size to content

SizeToContent();

return TRUE;

}

// Draw the appropriate bitmap

void CBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)

{

ASSERT(lpDIS != NULL);

// must have at least the first bitmap loaded before calling DrawItem

ASSERT(m_bitmap.m_hObject != NULL); // required

// use the main bitmap for up, the selected bitmap for down

CBitmap* pBitmap = &m_bitmap;

UINT state = lpDIS->itemState;

if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)

pBitmap = &m_bitmapSel;

else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)

pBitmap = &m_bitmapFocus; // third image for focused

else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)

pBitmap = &m_bitmapDisabled; // last image for disabled

// draw the whole button

CDC* pDC = CDC::FromHandle(lpDIS->hDC);

CDC memDC;

memDC.CreateCompatibleDC(pDC);

CBitmap* pOld = memDC.SelectObject(pBitmap);

if (pOld == NULL)

return; // destructors will clean up

CRect rect;

rect.CopyRect(&lpDIS->rcItem);

pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),

&memDC, 0, 0, SRCCOPY);

memDC.SelectObject(pOld);

}

/////////////////////////////////////////////////////////////////////////////

// CBitmapButton diagnostics

#ifdef _DEBUG

#if 0

void CBitmapButton::AssertValid() const

{

CButton::AssertValid();

m_bitmap.AssertValid();

m_bitmapSel.AssertValid();

m_bitmapFocus.AssertValid();

m_bitmapDisabled.AssertValid();

}

#endif

#if 0

void CBitmapButton::Dump(CDumpContext& dc) const

{

CButton::Dump(dc);

dc << "m_bitmap = " << (void*)m_bitmap.m_hObject;

dc << "\nm_bitmapSel = " << (void*)m_bitmapSel.m_hObject;

dc << "\nm_bitmapFocus = " << (void*)m_bitmapFocus.m_hObject;

dc << "\nm_bitmapDisabled = " << (void*)m_bitmapDisabled.m_hObject;

dc << "\n";

}

#endif

#endif

//IMPLEMENT_DYNAMIC(CBitmapButton, CButton)

/////////////////////////////////////////////////////////////////////////////

JulienT at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices Native C++ Development...
# 2
I am not sure what you are asking for but I took your code and implemented it and it worked for me. I had to switch the CButton over to owner draw but other then that it worked great.
On a side note; when I did my own CImgBtn I used one Resource ID with four images within it and used StretchBlt to only draw the btn I wanted. This cleans up the code a little and allows for one bitmap to be loaded instead of 4.
fellobo at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices Native C++ Development...