📄 bcgpeditlistbox.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a part of the BCGControlBar Library
// Copyright (C) 1998-2000 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.
//*******************************************************************************
// BCGPEditListBox.cpp : implementation file
//
#include "stdafx.h"
#include "BCGCBPro.h"
#include "BCGPEditListBox.h"
#include "BCGPKeyHelper.h"
#include "BCGPLocalResource.h"
#include "bcgprores.h"
#include "BCGGlobals.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static const int iListId = 1;
static const int nTextMargin = 5;
static const int nBrowseButtonWidth = 20;
/////////////////////////////////////////////////////////////////////////////
// CBCGPEditListEdit
CBCGPEditListEdit::CBCGPEditListEdit()
{
m_bLocked = FALSE;
m_rectBtn.SetRectEmpty ();
m_bIsButtonPressed = FALSE;
m_bIsButtonCaptured = FALSE;
m_pParentList = NULL;
m_bBrowseBtn = FALSE;
}
CBCGPEditListEdit::~CBCGPEditListEdit()
{
}
BEGIN_MESSAGE_MAP(CBCGPEditListEdit, CEdit)
//{{AFX_MSG_MAP(CBCGPEditListEdit)
ON_WM_WINDOWPOSCHANGING()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_NCCALCSIZE()
ON_WM_NCLBUTTONDOWN()
ON_WM_NCPAINT()
ON_WM_NCHITTEST()
ON_WM_WINDOWPOSCHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBCGPEditListEdit message handlers
void CBCGPEditListEdit::LockSize (CBCGPEditListBase* pParent, BOOL bLock)
{
m_pParentList = pParent;
m_bLocked = bLock;
}
//*************************************************************************************
void CBCGPEditListEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
if (m_bLocked)
{
lpwndpos->flags |= SWP_NOSIZE;
}
CEdit::OnWindowPosChanging(lpwndpos);
}
//*************************************************************************************
void CBCGPEditListEdit::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
CEdit::OnWindowPosChanged(lpwndpos);
if (m_bBrowseBtn)
{
CRect rectWindow;
GetWindowRect (m_rectBtn);
m_rectBtn.left = m_rectBtn.right - nBrowseButtonWidth;
}
else
{
m_rectBtn.SetRectEmpty ();
}
}
//*************************************************************************************
void CBCGPEditListEdit::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bIsButtonCaptured)
{
ReleaseCapture ();
m_bIsButtonPressed = FALSE;
m_bIsButtonCaptured = FALSE;
RedrawWindow (NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
CPoint ptScreen = point;
ClientToScreen (&ptScreen);
if (m_rectBtn.PtInRect (ptScreen) && m_pParentList != NULL)
{
ASSERT_VALID (m_pParentList);
m_pParentList->OnBrowse ();
}
return;
}
CEdit::OnLButtonUp(nFlags, point);
}
//*************************************************************************************
void CBCGPEditListEdit::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bIsButtonCaptured)
{
CPoint ptScreen = point;
ClientToScreen (&ptScreen);
BOOL bIsButtonPressed = m_rectBtn.PtInRect (ptScreen);
if (bIsButtonPressed != m_bIsButtonPressed)
{
m_bIsButtonPressed = bIsButtonPressed;
RedrawWindow (NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
}
return;
}
CEdit::OnMouseMove(nFlags, point);
}
//*************************************************************************************
void CBCGPEditListEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
if (!m_bLocked)
{
CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
if (m_bBrowseBtn)
{
lpncsp->rgrc [0].right -= nBrowseButtonWidth;
}
}
}
//*************************************************************************************
void CBCGPEditListEdit::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
if (m_bBrowseBtn && m_rectBtn.PtInRect (point))
{
m_bIsButtonPressed = TRUE;
m_bIsButtonCaptured = TRUE;
SetCapture ();
RedrawWindow (NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
return;
}
CEdit::OnNcLButtonDown(nHitTest, point);
}
//*************************************************************************************
void CBCGPEditListEdit::OnNcPaint()
{
CWindowDC dc (this);
CRect rectWindow;
GetWindowRect (rectWindow);
CRect rect = m_rectBtn;
rect.OffsetRect (-rectWindow.left, -rectWindow.top);
if (m_pParentList == NULL || !m_bBrowseBtn)
{
dc.FillRect (&rect, &globalData.brBtnFace);
return;
}
ASSERT_VALID (m_pParentList);
CRgn rgnClip;
rgnClip.CreateRectRgnIndirect (&rect);
dc.SelectClipRgn (&rgnClip);
m_pParentList->OnDrawBrowseButton (&dc, rect, m_bIsButtonPressed, FALSE);
dc.SelectClipRgn (NULL);
}
UINT CBCGPEditListEdit::OnNcHitTest(CPoint point)
{
if (m_rectBtn.PtInRect (point))
{
return HTCAPTION;
}
return CEdit::OnNcHitTest(point);
}
/////////////////////////////////////////////////////////////////////////////
// CBCGPEditListBase
CBCGPEditListBase::CBCGPEditListBase()
{
m_sizeButton = CSize (0, 0);
m_uiStandardBtns = 0;
m_bNewItem = FALSE;
m_bIsActualDelete = TRUE;
m_bBrowseButton = FALSE;
m_bGrayDisabledButtons = FALSE;
}
//**************************************************************************
CBCGPEditListBase::~CBCGPEditListBase()
{
while (!m_lstButtons.IsEmpty ())
{
delete m_lstButtons.RemoveHead ();
}
}
BEGIN_MESSAGE_MAP(CBCGPEditListBase, CStatic)
//{{AFX_MSG_MAP(CBCGPEditListBase)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_SETFOCUS()
ON_WM_ENABLE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBCGPEditListBase message handlers
void CBCGPEditListBase::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState ();
if (pThreadState->m_pWndInit == NULL)
{
Init ();
}
}
//**********************************************************************************
int CBCGPEditListBase::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
Init ();
return 0;
}
//**********************************************************************************
void CBCGPEditListBase::Init ()
{
if (OnCreateList () == NULL)
{
TRACE0("CBCGPEditListBase::Init (): Can not create list control\n");
return;
}
AdjustLayout ();
}
//**********************************************************************************
BOOL CBCGPEditListBase::SetStandardButtons (UINT uiBtns)
{
if (GetSafeHwnd () == NULL)
{
ASSERT (FALSE);
return FALSE;
}
CBCGPLocalResource locaRes;
CString strButton;
if (uiBtns & BGCEDITLISTBOX_BTN_NEW)
{
strButton.LoadString (IDS_BCGBARRES_NEW);
VERIFY (AddButton (IDB_BCGBARRES_NEW, strButton, VK_INSERT,0,BGCEDITLISTBOX_BTN_NEW_ID));
}
if (uiBtns & BGCEDITLISTBOX_BTN_DELETE)
{
strButton.LoadString (IDS_BCGBARRES_DELETE);
VERIFY (AddButton (IDB_BCGBARRES_DELETE, strButton, VK_DELETE, 0, BGCEDITLISTBOX_BTN_DELETE_ID));
}
if (uiBtns & BGCEDITLISTBOX_BTN_UP)
{
strButton.LoadString (IDS_BCGBARRES_MOVEUP);
VERIFY (AddButton (IDB_BCGBARRES_UP, strButton, VK_UP, FALT, BGCEDITLISTBOX_BTN_UP_ID));
}
if (uiBtns & BGCEDITLISTBOX_BTN_DOWN)
{
strButton.LoadString (IDS_BCGBARRES_MOVEDN);
VERIFY (AddButton (IDB_BCGBARRES_DOWN, strButton, VK_DOWN, FALT, BGCEDITLISTBOX_BTN_DOWN_ID));
}
m_uiStandardBtns |= uiBtns;
return TRUE;
}
//**********************************************************************************
BOOL CBCGPEditListBase::AddButton (UINT uiImageResId,
LPCTSTR lpszTooltip/* = NULL*/,
WORD wKeyAccelerator/* = 0*/,
BYTE fVirt/* = 0*/,
UINT uiButtonID/* = 0*/)
{
if (GetSafeHwnd () == NULL)
{
ASSERT (FALSE);
return FALSE;
}
if (uiButtonID != 0)
{
ASSERT(GetButtonNum(uiButtonID)==-1); // button with this ID still not added
}
CRect rectEmpty;
rectEmpty.SetRectEmpty ();
CBCGPButton* pButton = new CBCGPButton ();
if (!pButton->Create (_T(""), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW,
rectEmpty, this, m_lstButtons.GetCount () + 2))
{
return FALSE;
}
pButton->m_nFlatStyle = CBCGPButton::BUTTONSTYLE_FLAT;
pButton->m_bGrayDisabled = m_bGrayDisabledButtons;
pButton->m_bDrawFocus = FALSE;
pButton->SetImage (uiImageResId);
if (lpszTooltip != NULL)
{
CString strTooltip = lpszTooltip;
if (wKeyAccelerator != 0)
{
ACCEL acccel;
acccel.cmd = 0;
acccel.fVirt = (BYTE) (fVirt | FVIRTKEY);
acccel.key = wKeyAccelerator;
CBCGPKeyHelper helper (&acccel);
CString strAccellKey;
helper.Format (strAccellKey);
strTooltip += _T(" (");
strTooltip += strAccellKey;
strTooltip += _T(")");
}
pButton->SetTooltip (strTooltip);
}
pButton->SizeToContent ();
CRect rectBtn;
pButton->GetWindowRect (rectBtn);
CSize sizeButton = rectBtn.Size ();
if (m_lstButtons.IsEmpty ())
{
m_sizeButton = sizeButton;
}
else
{
ASSERT (m_sizeButton == sizeButton);
}
m_lstButtons.AddTail (pButton);
if (wKeyAccelerator == 0)
{
fVirt = 0;
}
DWORD dwKey = (fVirt << 16) | wKeyAccelerator;
m_lstKeyAccell.AddTail (dwKey);
if (uiButtonID != 0)
{
int iButton = m_lstButtons.GetCount () - 1;
m_mapButtonIDs.SetAt (iButton, uiButtonID);
}
AdjustLayout ();
return TRUE;
}
//**********************************************************************************
void CBCGPEditListBase::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.FillRect (m_rectCaption, &globalData.brBtnFace);
dc.Draw3dRect (m_rectCaption, globalData.clrBtnShadow, globalData.clrBtnHilite);
CRect rectText = m_rectCaption;
rectText.DeflateRect (nTextMargin, 0);
dc.SetBkMode (TRANSPARENT);
dc.SetTextColor (IsWindowEnabled () ? globalData.clrBtnText : globalData.clrGrayedText);
CFont* pOldFont = NULL;
if (m_font.GetSafeHandle () != NULL)
{
pOldFont = dc.SelectObject (&m_font);
}
else
{
CFont* pParentFont = GetParent ()->GetFont ();
if (pParentFont != NULL)
{
pOldFont = dc.SelectObject (pParentFont);
ASSERT (pOldFont != NULL);
}
}
CString strCaption;
GetWindowText (strCaption);
dc.DrawText (strCaption, rectText, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
if (pOldFont != NULL)
{
dc.SelectObject (pOldFont);
}
}
//**********************************************************************************
void CBCGPEditListBase::OnSize(UINT nType, int cx, int cy)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -