ccebutton.cpp

来自「WinCE 下实现Listctrl自画」· C++ 代码 · 共 231 行

CPP
231
字号
/////////////////////////////////////////////////////////////////////////////
// CeButton
#include "stdafx.h"
#include "CCeButton.h"

CCeButton::CCeButton()
{
	m_change=true;
	m_state=true;
	m_hMenu = NULL;
	m_hParentWndMenu = NULL;
	m_bMenuDisplayed = FALSE;
	m_gray=false;
	m_transparent=false;

}

CCeButton::~CCeButton()
{
	DeleteObject(brush1);
	DeleteObject(brush2);
}


BEGIN_MESSAGE_MAP(CCeButton, CButton)
	//{{AFX_MSG_MAP(CeButton)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CeButton message handlers

void CCeButton::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect rect;
	CDC mdc;
	mdc.CreateCompatibleDC(&dc);	
	COLORREF m_transpCREF= RGB(0,0,0);
	
	if(m_transparent)//透明方式
	{
		CWnd *	pParent;
		pParent = this->GetParent();
		GetWindowRect(&rect);
		pParent->ScreenToClient(&rect);

		mdc.SelectObject(&BkImage);
		dc.BitBlt(0,0, rect.Width(), rect.Height(), &mdc, 
						rect.left,rect.top,SRCCOPY);

		dc.SetBkMode(TRANSPARENT);

		GetClientRect(&rect);
		if(m_state)
		{
			mdc.SelectObject(&fImage);
			TransparentImage(dc, rect.left,rect.top, rect.Width(), rect.Height(), 
			mdc, rect.left,rect.top, rect.Width(), rect.Height(), m_transpCREF);
		}
		else
		{
			mdc.SelectObject(&eImage);
			TransparentImage(dc, rect.left,rect.top, rect.Width(), rect.Height(), 
			mdc, rect.left,rect.top, rect.Width(), rect.Height(), m_transpCREF);
		}
		if(m_text.GetLength()!=0)
		{
			dc.SetTextColor(m_TextColor);
			dc.DrawText(m_text,rect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
		}
	}
	else//画刷填充方式
	{
		GetClientRect(&rect);
		if(m_state)
		{
			 
			dc.FillRect(rect,&brush1);
		
		}
		else
		{
		
			dc.FillRect(rect,&brush2);
		}
	}

	//delete mdc;
//	mdc.DeleteDC();
	DeleteDC(mdc);
}
void CCeButton::SetImage(int BkID,int fID,int eID,bool change)
{
	m_change=change;
	BkImage.LoadBitmap(BkID);
	fImage.LoadBitmap(fID);
	eImage.LoadBitmap(eID);
	brush1.CreatePatternBrush(&fImage);
	brush2.CreatePatternBrush(&eImage);
}

void CCeButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_gray)return ;
	if(m_change)
	{
		m_state=false;
	}
	else
	{
		if(m_state)
			m_state=false;
		else m_state=true;
	}

	Invalidate();
	UpdateWindow();

	CButton::OnLButtonDown(nFlags, point);
}

void CCeButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_gray)return ;
	if(m_change)
	{	
		//if(m_state)
	//		m_state=false;
		//else
		m_state=true;
		
	}
	Invalidate();
	UpdateWindow();	
//	MessageBox(L"");
	CButton::OnLButtonUp(nFlags, point);
}
BOOL CCeButton::OnClicked() 
{
	// Handle the menu (if any)
	if (m_hMenu)
	{
		HMENU	hSubMenu = NULL;
		CRect	rWnd;

		hSubMenu = ::GetSubMenu(m_hMenu, 0);
		
		GetWindowRect(rWnd);

		
		m_bMenuDisplayed = TRUE;
		
		Invalidate();

		::TrackPopupMenuEx(	hSubMenu, 
#ifdef UNDER_CE
							TPM_LEFTALIGN,
#else
							TPM_LEFTALIGN, | TPM_LEFTBUTTON,
#endif
							rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);

		m_bMenuDisplayed = FALSE;
		Invalidate();
	} // if

	return FALSE;
} // End of OnClicked
DWORD CCeButton::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
{
	HINSTANCE	hInstResource	= NULL;

	// Destroy any previous menu
	if (m_hMenu)
	{
		::DestroyMenu(m_hMenu);
		m_hMenu = NULL;
		m_hParentWndMenu = NULL;
		m_bMenuDisplayed = FALSE;
	} // if

	// Load menu
	if (nMenu)
	{
		// Find correct resource handle
		hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
		// Load menu resource
		m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
		m_hParentWndMenu = hParentWnd;
		// If something wrong
		if (m_hMenu == NULL) return 1;
	} // if

	// Repaint the button
	if (bRepaint) Invalidate();

	return 0;
} // End of SetMenu
void CCeButton::SetText(CString text)
{
	m_text=text;
}
void CCeButton::SetState(bool state)
{
	m_state=state;
	Invalidate();
	UpdateWindow();
}
void CCeButton::SetGray(bool gray)
{
	m_gray=gray;
}

void CCeButton::SetTextColor(COLORREF	TextColor)
{
	m_TextColor=TextColor;
}
void CCeButton::SetTransparent(bool tran)
{
	m_transparent=tran;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?