home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 13
/
mediashare_13.zip
/
mediashare_13
/
ZIPPED
/
PROGRAM
/
WTJ9403.ZIP
/
WILDASS
/
SOURCE
/
ICONBTN.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-12
|
5KB
|
181 lines
// iconbtn.cpp
//
// class behaviour of the icon button class
//
// update: 01-Aug-93 tw
// 10-Aug-93 tw added drag n drop
#include <afxwin.h>
#include <afxext.h>
#include <ctl3d.h>
#include "iconbtn.h"
#include "mainwnd.h"
BEGIN_MESSAGE_MAP( CIconButton, CButton )
ON_WM_DROPFILES()
END_MESSAGE_MAP()
CIconButton::CIconButton()
{ /* does nothing */ }
void
CIconButton::Create( CWnd * pParent, const CPoint& rPos, UINT nID, const char * pszIconFile )
{
m_cxIcon = ::GetSystemMetrics( SM_CXICON );
m_cyIcon = ::GetSystemMetrics( SM_CYICON );
m_cxButton = m_cxIcon+CIconButton::cxFrame;
m_cyButton = m_cyIcon+CIconButton::cyFrame;
VERIFY( CButton::Create( 0, BS_OWNERDRAW | WS_CHILD | WS_VISIBLE,
CRect(rPos.x, rPos.y, rPos.x+m_cxButton, rPos.y+m_cyButton ),
pParent, nID ));
// do we already have an file to attach ?
if ( pszIconFile )
{
m_hIcon = ExtractIcon( AfxGetInstanceHandle(), pszIconFile, 0 );
}
else
{
m_hIcon = 0;
}
}
void
CIconButton::SetIcon( HICON hIcon )
{
ASSERT( hIcon );
m_hIcon = hIcon;
// repaint the button
Invalidate();
}
void
CIconButton::DrawItem( LPDRAWITEMSTRUCT lp )
{
// get a valid device context
CDC *pDC = CDC::FromHandle( lp->hDC );
ASSERT( pDC );
if ( lp->itemAction & ODA_DRAWENTIRE )
{
DrawEntire( pDC );
}
else if ( lp->itemAction & ODA_SELECT )
{
if ( lp->itemState & ODS_SELECTED )
{
DrawDownState( pDC );
}
else
{
DrawUpState( pDC );
}
}
}
void
CIconButton::DrawDownState( CDC * pDC )
{
// blit the image left/down one pixel
pDC->BitBlt( 4, 4, m_cxButton-5, m_cyButton-5, pDC, 3, 3, SRCCOPY );
// paint the shadow
CPen penShadow( PS_SOLID, 0, GetSysColor( COLOR_BTNSHADOW ));
CPen * penOld = pDC->SelectObject( &penShadow );
pDC->MoveTo( 1, m_cyButton-2 );
pDC->LineTo( 1, 1 );
pDC->LineTo( m_cxButton-1, 1);
CPen penFace( PS_SOLID, 0, GetSysColor( COLOR_BTNFACE ));
pDC->SelectObject( &penFace );
pDC->MoveTo( m_cxButton-2, 2);
pDC->LineTo( m_cxButton-2, 4);
pDC->MoveTo( 1, m_cyButton-2);
pDC->LineTo( 3, m_cyButton-2);
pDC->SelectObject( penOld );
}
void
CIconButton::DrawUpState( CDC * pDC )
{
// we could go for some optimizations here, ....
DrawEntire( pDC );
}
void
CIconButton::DrawEntire( CDC * pDC )
{
CBitmap bm;
VERIFY( bm.CreateCompatibleBitmap( pDC, m_cxButton, m_cyButton));
CDC dcMem;
VERIFY( dcMem.CreateCompatibleDC( pDC ));
CBitmap* pBitmapOld = dcMem.SelectObject( &bm );
ASSERT( pBitmapOld );
// draw the buttons shape
CBrush faceBrush( GetSysColor( COLOR_BTNFACE ) );
CRect rectFace;
GetClientRect( &rectFace );
pDC->FillRect( rectFace, &faceBrush );
CPen penBlack( PS_SOLID, 0, RGB(0,0,0) );
CPen * penOld = pDC->SelectObject( &penBlack );
pDC->MoveTo( 1, 0 );
pDC->LineTo( m_cxButton-1, 0);
pDC->MoveTo( m_cxButton-1, 1 );
pDC->LineTo( m_cxButton-1, m_cyButton-1 );
pDC->MoveTo( m_cxButton-2, m_cyButton-1 );
pDC->LineTo( 0, m_cyButton-1 );
pDC->MoveTo( 0, m_cyButton-2 );
pDC->LineTo( 0, 0 );
CPen penHighlight( PS_SOLID, 0, GetSysColor( COLOR_BTNHIGHLIGHT ));
pDC->SelectObject( &penHighlight );
pDC->MoveTo( 1, m_cyButton-2 );
pDC->LineTo( 1, 1 );
pDC->LineTo( m_cxButton-1, 1);
CPen penShadow( PS_SOLID, 0, GetSysColor( COLOR_BTNSHADOW ));
pDC->SelectObject( &penShadow );
pDC->MoveTo( m_cxButton-2, 2 );
pDC->LineTo( m_cxButton-2, m_cyButton-2 );
pDC->LineTo( 1, m_cyButton-2 );
// only draw icon if we have one
if ( m_hIcon )
{
VERIFY( pDC->DrawIcon( 4, 4, m_hIcon ));
}
dcMem.SelectObject( pBitmapOld );
pDC->SelectObject( penOld );
}
/*static */ UINT
CIconButton::Width()
{
return (::GetSystemMetrics( SM_CXICON ) + CIconButton::cxFrame );
}
// just route the dropinfo to the parent window
void
CIconButton::OnDropFiles( HDROP hDrop )
{
TRACE("incobtn drop\n");
((CMainWnd*)GetParent())->DroppedFiles( hDrop, ::GetWindowWord( m_hWnd, GWW_ID ) );
}