⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttonssl.cpp

📁 实现Huffman编码统计压缩算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// CButtonSSL.cpp : implementation file
//

#include "stdafx.h"
#include "ButtonSSL.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define DROP_BUTTON_WIDTH	16
#define BS_TYPEMASK			SS_TYPEMASK

/////////////////////////////////////////////////////////////////////////////
// CButtonSSL

/////////////////////////////////////////////////////////////////////////////
// Construction

CButtonSSL::CButtonSSL () : COddButton () {
	m_bImageLoaded = FALSE;
	m_bMenuLoaded = FALSE;
	m_bMenuPushed = FALSE;
	m_bBtnPushed = FALSE;
	m_bMouseOnBtn = FALSE;
	
	m_nStyle = SSL_BS_FLAT;
	m_nTextAlign = SSL_TEXT_CENTER | SSL_TEXT_VCENTER;
	m_nImageAlign = SSL_IMAGE_LEFT | SSL_IMAGE_TOP;
	m_nImageWidth = 0;
	m_nImageHeight = 0;

	m_ToolTip.m_hWnd = NULL;
	m_hCursor = NULL;
	SetSSLDefaultColors (FALSE);

	ZeroMemory(&m_bmp, sizeof m_bmp);
	ZeroMemory(&m_szURL, sizeof(m_szURL));

	m_bsState = SSL_STATE_UP;
	m_nCheck = 0;
}

/////////////////////////////////////////////////////////////////////////////
// Destruction

CButtonSSL::~CButtonSSL () {
	m_font.DeleteObject ();
}

BEGIN_MESSAGE_MAP(CButtonSSL, COddButton)
	//{{AFX_MSG_MAP(CButtonSSL)
	ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SETFOCUS()
	ON_WM_SETCURSOR()
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
	ON_MESSAGE (BM_GETCHECK, OnGetCheck)
	ON_MESSAGE (BM_SETCHECK, OnSetCheck)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtonSSL message handlers

void CButtonSSL::PreSubclassWindow() {
	// Determine what type of button this is
	COddButton::PreSubclassWindow ();

	if (NULL == (HFONT)m_font) {
		// If a font isn't already set, get the default font
		CFont* pFont = GetFont();
		if (!pFont) {
			HFONT hFont = (HFONT)GetStockObject (DEFAULT_GUI_FONT);
			if (NULL == hFont) {
				hFont = (HFONT) GetStockObject (ANSI_VAR_FONT);
			}
			if (hFont) {
				pFont = CFont::FromHandle (hFont);
			}
		}
		ASSERT (pFont->GetSafeHandle ());
		LOGFONT lf;
		pFont->GetLogFont (&lf);
		m_font.CreateFontIndirect (&lf);
	}

}

// Thanks to Tomasz Sowinski for helping me with the DDX problems
LRESULT CButtonSSL::OnGetCheck () {
	UINT nType = GetControlType ();
	if (BS_CHECKBOX == (BS_CHECKBOX & nType) || 
		BS_AUTOCHECKBOX == (BS_AUTOCHECKBOX & nType) ||
		BS_RADIOBUTTON == (BS_RADIOBUTTON & nType) || 
		BS_AUTORADIOBUTTON == (BS_AUTORADIOBUTTON & nType)) {
		if (0 == m_nCheck) {
			return BST_UNCHECKED;
		}
		else {
			return BST_CHECKED;
		}
	}
	return 0;
}

// Thanks to Tomasz Sowinski for helping me with the DDX problems
LRESULT CButtonSSL::OnSetCheck (WPARAM fCheck) {
	if (BST_CHECKED == fCheck) {
		m_nCheck = 1;
		m_bBtnPushed = TRUE;
	}
	else if (BST_UNCHECKED == fCheck) {
		m_nCheck = 0;
		m_bBtnPushed = FALSE;
	}

	Invalidate ();

	return 0;
}

BOOL CButtonSSL::OnClicked () {
	// Update the check state for radio buttons and check boxes
	UINT nType = GetControlType ();
	if (BS_CHECKBOX == (BS_CHECKBOX & nType) || BS_AUTOCHECKBOX== (BS_AUTOCHECKBOX & nType) /*||
		BS_RADIOBUTTON & nType || BS_AUTORADIOBUTTON & nType*/) {
		m_nCheck = !m_nCheck;
		m_bBtnPushed = !m_bBtnPushed;
		Invalidate ();
	}
	else if (BS_RADIOBUTTON == (BS_RADIOBUTTON & nType) ||
		BS_AUTORADIOBUTTON == (BS_AUTORADIOBUTTON & nType)) {
        // Send BM_SETCHECK (BST_CHECKED) to this button nad
        // BM_SETCHECK (BST_UNCHECKED) to all other buttons in this group
        SendMessage (BM_SETCHECK, BST_CHECKED, 0);
        // Start by searching forwards
        HWND hWndDlg = this->GetParent ()->m_hWnd;
        CWnd* pWnd = FromHandle (::GetNextDlgGroupItem (hWndDlg, this->m_hWnd, FALSE));
        while (NULL != pWnd && this != pWnd) {
            pWnd->SendMessage (BM_SETCHECK, BST_UNCHECKED, 0);
            pWnd = FromHandle (::GetNextDlgGroupItem (hWndDlg, pWnd->m_hWnd, FALSE));
        }
        // Then search backwards
        pWnd = FromHandle (::GetNextDlgGroupItem (hWndDlg, this->m_hWnd, TRUE));
        while (NULL != pWnd && this != pWnd) {
            pWnd->SendMessage (BM_SETCHECK, BST_UNCHECKED, 0);
            pWnd = FromHandle (::GetNextDlgGroupItem (hWndDlg, pWnd->m_hWnd, TRUE));
        }
	}
	else {
		// Handle the URL (if any)
		if (::lstrlen(m_szURL) > 0) {
			::ShellExecute(NULL, _T("open"), m_szURL, NULL, NULL, SW_SHOWMAXIMIZED);
		}
	}
	
	return FALSE; // Allow the parent to handle the click event
}

void CButtonSSL::OnMouseMove (UINT nFlags, CPoint point) {
	CWnd*	pWnd;		// Active window
	CWnd*	pParent;	// Window that owns the button

	CButton::OnMouseMove (nFlags, point);

	// Ignore if left button pressed on entering button
	if ((nFlags & MK_LBUTTON) && FALSE == m_bMouseOnBtn) {
		return;
	}

	pWnd = GetActiveWindow();
	pParent = GetOwner();

	if ((GetCapture () != this) && (pParent != NULL)) {
		m_bMouseOnBtn = TRUE;
		SetCapture ();
		Invalidate ();
	}
	else {
		ClientToScreen (&point);
		CWnd* wndUnderMouse = WindowFromPoint (point);
		if (NULL != wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd) {
			// Redraw only if mouse goes out
			if (TRUE == m_bMouseOnBtn) {
				m_bMouseOnBtn = FALSE;
				Invalidate ();
			}
			// Release capture if the left button is not pressed
			if (!(nFlags & MK_LBUTTON)) {
				ReleaseCapture ();
			}
		}
	}
}

void CButtonSSL::OnLButtonDown(UINT nFlags, CPoint point) {
	if (TRUE == m_bMenuPushed) {
		m_bMenuPushed = FALSE;
		Invalidate();
		return;
	}

	// Show the menu if the click is on the drop-arrow
	// and the SSL_BS_MENU_BTN style is set
	if (TRUE == HitMenuButton(point) && (SSL_BS_MENU_BTN & m_nStyle)) {
		m_bMenuPushed = TRUE;
		Invalidate();

		CRect rect;
		GetWindowRect (rect);

		int x = rect.right;
		int y = rect.bottom;

		CMenu* pMenu = m_menu.GetSubMenu (0);
		pMenu->TrackPopupMenu (TPM_RIGHTALIGN | TPM_LEFTBUTTON, x, y, GetParent ());

		m_bMenuPushed = FALSE;
	}
	else {
		m_bBtnPushed = TRUE;
	}

	Invalidate();
	CButton::OnLButtonDown (nFlags, point);
}

void CButtonSSL::OnLButtonUp(UINT nFlags, CPoint point) {
	CButton::OnLButtonUp (nFlags, point);

	// Check box state updating done in Clicked handler
	UINT nType = GetControlType ();
	if (BS_CHECKBOX != (BS_CHECKBOX & nType) &&
		BS_AUTOCHECKBOX != (BS_AUTOCHECKBOX & nType) &&
		BS_RADIOBUTTON != (BS_RADIOBUTTON & nType) &&
		BS_AUTORADIOBUTTON != (BS_AUTORADIOBUTTON & nType)) {
		if (TRUE == m_bBtnPushed) {
			m_bBtnPushed = FALSE;
			ReleaseCapture();
			Invalidate();
		}
	}
}

void CButtonSSL::OnSetFocus(CWnd* pOldWnd) {
	CButton::OnSetFocus (pOldWnd);
	
	Invalidate ();
}

BOOL CButtonSSL::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) {
	// Use the specifed cursor if there is one
	if (NULL != m_hCursor) {
		::SetCursor (m_hCursor);
		return TRUE;
	}
	
	return CButton::OnSetCursor(pWnd, nHitTest, message);
}

HBRUSH CButtonSSL::CtlColor (CDC* pDC, UINT nCtlColor) {
	return (HBRUSH)::GetStockObject (NULL_BRUSH); 
}

void CButtonSSL::DrawBackground (CDC* pDC, LPCRECT pRect) {
	COLORREF crColor = ::GetSysColor (COLOR_BTNFACE);

	if (SSL_STATE_UP == m_bsState) {
		crColor = m_crColors[SSL_UP_BK_COLOR];
	}
	if (SSL_STATE_OVER == m_bsState) {
		crColor = m_crColors[SSL_OVER_BK_COLOR];
	}
	if (SSL_STATE_DOWN == m_bsState) {
		crColor = m_crColors[SSL_DOWN_BK_COLOR];
	}

	CBrush		brBackground(crColor);

	pDC->FillRect(pRect, &brBackground);
}

void CButtonSSL::DrawBorder (CDC* pDC, LPRECT lpRect) {
	if (TRUE == m_bBtnPushed) {
		pDC->Draw3dRect (lpRect, ::GetSysColor (COLOR_BTNSHADOW), ::GetSysColor (COLOR_BTNHIGHLIGHT));
		return;
	}
	if (!(SSL_BS_FLAT & m_nStyle)) {
		if (TRUE == m_bBtnPushed) {
			pDC->DrawFrameControl (lpRect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
		}
		else {
			pDC->DrawFrameControl (lpRect, DFC_BUTTON, DFCS_BUTTONPUSH);
		}
		return;
	}
	if (TRUE == m_bMouseOnBtn && FALSE == m_bBtnPushed) {
		pDC->Draw3dRect (lpRect, ::GetSysColor (COLOR_BTNHIGHLIGHT), ::GetSysColor (COLOR_BTNSHADOW));
	}
}

void CButtonSSL::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
	if (lpDrawItemStruct->CtlType != ODT_BUTTON) {
		return;
	}

	CDC cdcMem;
	CBitmap bmp;

	CRect btnRect (lpDrawItemStruct->rcItem);
	CRect trueRect (btnRect);

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

	// Check to see if it's a checked checkbox or radio button
	UINT nType = GetControlType ();
	if ((BS_AUTORADIOBUTTON == (BS_AUTORADIOBUTTON & nType) ||
		BS_RADIOBUTTON == (BS_RADIOBUTTON & nType) ||
		BS_AUTOCHECKBOX == (BS_AUTOCHECKBOX & nType) || 
		BS_CHECKBOX == (BS_CHECKBOX & nType)) && 
		FALSE == m_bBtnPushed) {
		if (0 == m_nCheck) {
			m_bBtnPushed = FALSE;
		}
		else {
			m_bBtnPushed = TRUE;
		}
	}

	// Check to see if the button is disabled
	m_bDisabled = (ODS_DISABLED == (ODS_DISABLED & lpDrawItemStruct->itemState));

	// Update the button state
	if (TRUE == m_bDisabled) {
		m_bsState = SSL_STATE_DISABLED;
	}
	else {
		m_bsState = SSL_STATE_UP;
		if (TRUE == m_bMouseOnBtn) {
			m_bsState = SSL_STATE_OVER;
		}
		if (TRUE == m_bBtnPushed) {
			m_bsState = SSL_STATE_DOWN;
		}
	}

#ifdef _TRACESTATE
	TraceState (m_bsState);
#endif
	
	if (TRUE == IsDefault ()) {
		btnRect.DeflateRect (1, 1);
	}

	// Button Background and Border
	DrawBackground (pDC, &trueRect);
	DrawBorder (pDC, &btnRect);

	CRect rectFocus (btnRect);	// Focus rectangle
	rectFocus.DeflateRect (3, 3);

	if (SSL_BS_MENU_BTN & m_nStyle) {
		rectFocus.right -= DROP_BUTTON_WIDTH;	// Exclude drop button from focus rectangle
	}
	// Default Button State
	if (TRUE == IsDefault () && FALSE == m_bDisabled && !(SSL_BS_FLAT & m_nStyle)) {
		pDC->FrameRect (&lpDrawItemStruct->rcItem, 
			CBrush::FromHandle ((HBRUSH)GetStockObject (BLACK_BRUSH)));
		if (TRUE == m_bBtnPushed && FALSE == m_bMenuPushed) {
			pDC->FrameRect (&btnRect, CBrush::FromHandle (GetSysColorBrush (COLOR_3DSHADOW))); 
		}
	}

	// State Focus
	if ((lpDrawItemStruct->itemState & ODS_FOCUS || TRUE == m_bBtnPushed) && !(SSL_BS_FLAT & m_nStyle) &&
		BS_AUTOCHECKBOX != (BS_AUTOCHECKBOX & nType) && BS_CHECKBOX != (BS_CHECKBOX & nType)) {
		if (FALSE == m_bMenuPushed) {
			pDC->DrawFocusRect (&rectFocus);
		}
	}

	// Action Focus
	if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && !(SSL_BS_FLAT & m_nStyle)) {
		if (FALSE == m_bMenuPushed) {
			pDC->DrawFocusRect (&rectFocus);
		}
	}

	// Draw the image on the button.
	// Extract the image as an icon regardless of what was added originally
	if (TRUE == m_bImageLoaded) {
		HICON hIcon = m_imageList.ExtractIcon (m_bsState);
		// Defer to the Up image. This should only happen if there isn't a disabled image
		if (NULL == hIcon) {
			hIcon = m_imageList.ExtractIcon (SSL_STATE_UP);
			pDC->DrawState (GetImagePoint (trueRect),
				CSize (m_nImageWidth, m_nImageHeight), hIcon, 
				DST_ICON | (m_bsState == SSL_STATE_DISABLED ? DSS_DISABLED : DSS_NORMAL), (CBrush*)NULL);
		}
		else {
			pDC->DrawState (GetImagePoint (trueRect),
				CSize (m_nImageWidth, m_nImageHeight), hIcon, 
				DST_ICON | DSS_NORMAL, (CBrush*)NULL);
		}
	}

	// Draw out text
	pDC->SelectObject (&m_font);

⌨️ 快捷键说明

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