📄 checkdragdlg.cpp
字号:
// CheckDragDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Controls.h"
#include "CheckDragDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct CHECKBOXDATA
{
bool bCheck;
DWORD dwData;
CString strData;
};
/////////////////////////////////////////////////////////////////////////////
// CCheckDragDlg dialog
CCheckDragDlg::CCheckDragDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCheckDragDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCheckDragDlg)
m_bAuto = TRUE;
m_nCheckBoxType = 0;
//}}AFX_DATA_INIT
m_dwCheckListStyle = BS_AUTOCHECKBOX;
}
void CCheckDragDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCheckDragDlg)
DDX_Control(pDX, IDC_DRAGLIST, m_DragList);
DDX_Control(pDX, IDC_CHECKLIST, m_CheckList);
DDX_Check(pDX, IDC_CHECK_AUTOBOX, m_bAuto);
DDX_Radio(pDX, IDC_CHECK_CHECKBOX, m_nCheckBoxType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCheckDragDlg, CDialog)
//{{AFX_MSG_MAP(CCheckDragDlg)
ON_BN_CLICKED(IDC_CHECK_3STATEBOX, OnCheck3statebox)
ON_BN_CLICKED(IDC_CHECK_CHECKBOX, OnCheckCheckbox)
ON_BN_CLICKED(IDC_CHECK_AUTOBOX, OnCheckAutobox)
ON_LBN_DBLCLK(IDC_CHECKLIST, OnDblclkChecklist)
//}}AFX_MSG_MAP
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCheckDragDlg message handlers
void CCheckDragDlg::OnCancel()
{
}
BOOL CCheckDragDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// CCheckBoxes must be LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE
// CDragList cannot have LBS_SORT or LBS_MULTISELECT
//
// AUTOCHECKBOX provides a box that is either checked
// or unchecked.
// AUTO3STATE provides a three-state checkbox. The first
// click checks it, the second puts into an indeterminate
// state and the third unchecks it.
//
// Fill the lists with file names for sample data.
//
CFont *font = GetFont();
m_DragList.SetFont (font);
m_CheckList.SetFont (font);
m_CheckList.SetCheckStyle (m_dwCheckListStyle);
LoadLists (m_DragList);
LoadLists (m_CheckList);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CCheckDragDlg::LoadLists(CListBox &lb)
{
WIN32_FIND_DATA fd;
HANDLE hFind;
if ((hFind = FindFirstFile (_T("*.*"), &fd)) == NULL)
return;
do
{
if (fd.cFileName[0] == '.')
continue;
lb.AddString (fd.cFileName);
} while (FindNextFile (hFind, &fd));
FindClose (hFind);
}
void CCheckDragDlg::OnCheck3statebox()
{
if (m_bAuto)
m_dwCheckListStyle = BS_AUTO3STATE;
else
m_dwCheckListStyle = BS_3STATE;
m_nCheckBoxType = 1;
RecreateCheckList ();
UpdateData (FALSE);
}
void CCheckDragDlg::OnCheckCheckbox()
{
if (m_bAuto)
m_dwCheckListStyle = BS_AUTOCHECKBOX;
else
m_dwCheckListStyle = BS_CHECKBOX;
m_nCheckBoxType = 0;
RecreateCheckList ();
UpdateData (FALSE);
}
void CCheckDragDlg::OnCheckAutobox()
{
UpdateData (TRUE);
if (m_bAuto)
{
switch (m_dwCheckListStyle)
{
case BS_CHECKBOX:
m_dwCheckListStyle = BS_AUTOCHECKBOX;
break;
case BS_3STATE:
m_dwCheckListStyle = BS_AUTO3STATE;
break;
}
}
else
{
switch (m_dwCheckListStyle)
{
case BS_AUTOCHECKBOX:
m_dwCheckListStyle = BS_CHECKBOX;
break;
case BS_AUTO3STATE:
m_dwCheckListStyle = BS_3STATE;
break;
}
}
RecreateCheckList ();
}
void CCheckDragDlg::RecreateCheckList()
{
CHECKBOXDATA *cbd;
CRect rc;
DWORD dwStyle = m_CheckList.GetStyle ();
DWORD dwStyleEx = m_CheckList.GetExStyle ();
m_CheckList.GetWindowRect (&rc);
ScreenToClient (rc);
CFont *font = GetFont ();
int nCount = m_CheckList.GetCount ();
int nSel = m_CheckList.GetCurSel ();
if (nCount)
{
cbd = new CHECKBOXDATA [nCount];
for (int n = 0; n < nCount; ++n)
{
cbd[n].bCheck = m_CheckList.GetCheck(n) ? true : false;
cbd[n].dwData = m_CheckList.GetItemData (n);
m_CheckList.GetText (n, cbd[n].strData);
}
}
m_CheckList.ResetContent ();
m_CheckList.DestroyWindow ();
m_CheckList.CreateEx (dwStyleEx, "listbox", NULL,
dwStyle, rc, this, IDC_CHECKLIST, NULL);
// m_CheckList.Create (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | WS_VSCROLL,
// rc,
// this,
// IDC_CHECKLIST);
m_CheckList.SetCheckStyle (m_dwCheckListStyle);
m_CheckList.SetFont (font);
m_CheckList.ShowWindow (SW_SHOWNORMAL);
if (nCount)
{
for (int n = 0; n < nCount; ++n)
{
int nIndex = m_CheckList.AddString ((LPCSTR) cbd[n].strData);
m_CheckList.SetCheck (nIndex, cbd[n].bCheck);
m_CheckList.SetItemData (nIndex, cbd[n].dwData);
}
delete [] cbd;
}
m_CheckList.SetCurSel (nSel);
}
//
// If auto is selected, return or we'll just be
// unsetting a just set check.
// By unselecting auto, we can let the user select
// or unselect and item by double clicking anywhere
// on the line.
//
void CCheckDragDlg::OnDblclkChecklist()
{
if (m_bAuto)
return;
int nIndex = m_CheckList.GetCurSel ();
int nCheck = m_CheckList.GetCheck (nIndex);
if (m_dwCheckListStyle == BS_CHECKBOX)
{
m_CheckList.SetCheck (nIndex, nCheck ? FALSE : TRUE);
}
else
{
++nCheck;
if (nCheck > 2)
nCheck = 0;
m_CheckList.SetCheck (nIndex, nCheck);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -