📄 coolbtn.cpp
字号:
//
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
// NT ALMOND 020703 1.1 Fix unwanted message
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_bMenuPushed)
{
m_bMenuPushed = FALSE;
Invalidate();
return;
}
if (HitMenuBtn(point))
{
m_bMenuPushed = TRUE;
SetFocus();
Invalidate();
CRect rc;
GetWindowRect(rc);
int x = rc.left;
int y = rc.bottom;
/*a dirty way to update menuitem yanglq 2005-7-23*/
m_pParentWnd = this->GetParent();
if(m_pParentWnd)
m_pParentWnd->PostMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(),1234), (LPARAM) GetSafeHwnd());
m_menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,x,y,
/*m_pParentWnd*/
this->GetParent()
);
m_bMenuPushed = FALSE;
}
else
{
m_bPushed = TRUE;
}
Invalidate();
if (m_bPushed)
CButton::OnLButtonDown(nFlags, point);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: OnLButtonUp
//
// DESCRIPTION: Redraws button in normal state
//
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
// NT ALMOND 020703 1.1 Generate BN_CLICK EVENT
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bPushed)
{
m_bPushed = FALSE;
ReleaseCapture();
Invalidate();
m_pParentWnd = this->GetParent();
if(m_pParentWnd)
m_pParentWnd->PostMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM) GetSafeHwnd());
}
CButton::OnLButtonUp(nFlags, point);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: OnMouseMove
//
// DESCRIPTION: Tracks mouse whilst pressed
//
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bPushed)
{
ClientToScreen(&point);
if (WindowFromPoint(point) != this)
{
m_bPushed = FALSE;
ReleaseCapture();
Invalidate();
}
}
CButton::OnMouseMove(nFlags, point);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: SetButtonImage
//
// DESCRIPTION: Sets the button image, COLORREF crMask specifics the transparency
// color
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
//
////////////////////////////////////////////////////////////////////////////////
BOOL CCoolBtn::SetButtonImage(UINT nResourceId, COLORREF crMask)
{
// The ID must exist also as a bitmap resource!!!
m_btnImage.LoadBitmap(nResourceId);
m_btnImage.GetObject(sizeof m_bm, &m_bm);
m_IL.Create( nResourceId, m_bm.bmWidth, 1, crMask );
m_bLoaded = TRUE;
m_crMask = crMask;
HBITMAP bmTemp;
COLORMAP mapColor;
mapColor.from = crMask;
mapColor.to = RGB(255,255,255);
bmTemp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, nResourceId, IMAGE_BITMAP, &mapColor, 1);
m_hbmpDisabled = (HBITMAP)::CopyImage(bmTemp, IMAGE_BITMAP, m_bm.bmWidth, m_bm.bmHeight, LR_COPYDELETEORG);
return m_bLoaded;
}
void CCoolBtn::OnSetFocus(CWnd* pOldWnd)
{
CButton::OnSetFocus(pOldWnd);
Invalidate();
}
void CCoolBtn::OnKillFocus(CWnd* pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: OnSysColorChange
//
// DESCRIPTION: Called when system colors change, force a button redraw
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::OnSysColorChange()
{
CButton::OnSysColorChange();
Invalidate();
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: HitMenuBtn
//
// DESCRIPTION: Helper function to test for menu button hit...
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
//
////////////////////////////////////////////////////////////////////////////////
BOOL CCoolBtn::HitMenuBtn(CPoint point)
{
if (!m_bMenuLoaded)
return FALSE; // Don't allow menu button drop down if no menu items are loaded
ClientToScreen(&point);
CRect rect;
GetWindowRect(rect);
rect.left = rect.right - nDropBtnWidth;
return rect.PtInRect(point);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: AddMenuItem
//
// DESCRIPTION: Adds a menu item and id to our menu.
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 210100 1.0 Origin
//
////////////////////////////////////////////////////////////////////////////////
BOOL CCoolBtn::AddMenuItem(UINT nMenuId,const CString strMenu, UINT nMenuFlags)
{
BOOL bRet = m_menu.AppendMenu(nMenuFlags | MF_STRING, nMenuId, (LPCTSTR)strMenu);
m_bMenuLoaded |= bRet;
return bRet;
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: PreSubclassWindow
//
// DESCRIPTION: Force BS_OWNERDRAW during subclassing
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::PreSubclassWindow()
{
ModifyStyle(0, BS_OWNERDRAW); // Enforce
CButton::PreSubclassWindow();
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: RemoveMenuItem
//
// DESCRIPTION: Remove a menu item
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
BOOL CCoolBtn::RemoveMenuItem(UINT nMenuId,const CString strMenu, UINT nMenuFlags)
{
BOOL bRet = m_menu.RemoveMenu( nMenuId,nMenuFlags | MF_STRING);
m_bMenuLoaded |= bRet;
return bRet;
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: EnableMenuItem
//
// DESCRIPTION: Enable/Disable a menu item
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
BOOL CCoolBtn::EnableMenuItem(UINT nMenuId, BOOL nEnable)
{
BOOL bRet = m_menu.EnableMenuItem(nMenuId , nEnable);
m_bMenuLoaded |= bRet;
return bRet;
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: AddImage
//
// DESCRIPTION: Adds Image to menu
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::AddImage( int nIndex, CBitmap& bmpEnabled, CBitmap& bmpDisabled )
{
ASSERT( m_menu.SetMenuItemBitmaps( nIndex, MF_BYPOSITION, &bmpEnabled, &bmpDisabled ) );
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: ChangeMenuFlags
//
// DESCRIPTION: Allow menu flags to be changed dynamically
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::ChangeMenuFlags(UINT nMenuId, UINT nMenuFlags)
{
m_menu.CheckMenuItem(nMenuId, nMenuFlags | MF_BYCOMMAND);
}
////////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: SetDefaultButton
//
// DESCRIPTION: Call this to allow the button to a act as a default button
//
// NOTES:
//
// MAINTENANCE:
// Name: Date: Version: Notes:
// NT ALMOND 020703 1.1 Origin
//
////////////////////////////////////////////////////////////////////////////////
void CCoolBtn::SetDefaultButton(BOOL bDefault)
{
m_bDefaultBtn = bDefault;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -