📄 ctrldemo2dlg.cpp
字号:
// CtrlDemo2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "CtrlDemo2.h"
#include "CtrlDemo2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString chArrTreeContent[] =
{
"root",
"parent1",
"child11",
"child12",
"parent2",
"child21",
"child22",
"child23",
};
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCtrlDemo2Dlg dialog
CCtrlDemo2Dlg::CCtrlDemo2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCtrlDemo2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCtrlDemo2Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCtrlDemo2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCtrlDemo2Dlg)
DDX_Control(pDX, IDC_TREE_DEMO, m_treeDemo);
DDX_Control(pDX, IDC_SLIDER_PERCENT, m_sldPercent);
DDX_Control(pDX, IDC_SLIDER_INTERVAL, m_sldInterval);
DDX_Control(pDX, IDC_PROGRESS_DEMO, m_progDemo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCtrlDemo2Dlg, CDialog)
//{{AFX_MSG_MAP(CCtrlDemo2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_PROGRESSDEMO, OnButtonProgressdemo)
ON_NOTIFY(TVN_ENDLABELEDIT, IDC_TREE_DEMO, OnEndlabeleditTreeDemo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCtrlDemo2Dlg message handlers
BOOL CCtrlDemo2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//进度条、滑动控件的初始化
m_sldPercent.SetRange( 1, 50 );
m_sldPercent.SetPos( 5 );
m_sldInterval.SetRange( 0, 10 );
m_sldInterval.SetPos( 1 );
m_progDemo.SetRange( 0, 100 );
//ImageList
m_ImageList.Create( 16, 16, FALSE, 3, 0 );
m_ImageList.Add( LoadIcon( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ROOT) ) );
m_ImageList.Add( LoadIcon( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PARENT) ) );
m_ImageList.Add( LoadIcon( AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_CHILD) ) );
//树控件初始化
m_treeDemo.SetImageList( &m_ImageList, TVSIL_NORMAL );
InitialTreeDemo();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCtrlDemo2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCtrlDemo2Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCtrlDemo2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCtrlDemo2Dlg::OnButtonProgressdemo()
{
int nPercent, nInterval, nCurPos = 0;
nPercent = m_sldPercent.GetPos();
nInterval = m_sldInterval.GetPos();
m_progDemo.SetPos( 0 );
while( (nCurPos+=nPercent) < 100 )
{
m_progDemo.OffsetPos( nPercent );
Sleep( 1000 * nInterval / 10 );
}
m_progDemo.SetPos( 100 );
}
void CCtrlDemo2Dlg::InitialTreeDemo()
{
HTREEITEM hItem0, hItem1, hItem2;
//增加根结点:root
TVINSERTSTRUCT tvItem;
tvItem.hParent = NULL;
tvItem.hInsertAfter = TVI_LAST;
tvItem.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[0];
tvItem.item.iImage = tvItem.item.iSelectedImage = 0;
hItem0 = m_treeDemo.InsertItem( &tvItem );
//增加root的两个子结点:parent1,parent2
tvItem.hParent = hItem0;
tvItem.item.iImage = tvItem.item.iSelectedImage = 1;
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[1];
hItem1 = m_treeDemo.InsertItem( &tvItem );
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[4];
hItem2 = m_treeDemo.InsertItem( &tvItem );
//增加parent1的子结点:child11,child12
tvItem.item.iImage = tvItem.item.iSelectedImage = 2;
tvItem.hParent = hItem1;
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[2];
m_treeDemo.InsertItem( &tvItem );
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[3];
m_treeDemo.InsertItem( &tvItem );
//增加parent2的子结点:child21,child22,child23
tvItem.item.iImage = 2;
tvItem.hParent = hItem2;
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[5];
m_treeDemo.InsertItem( &tvItem );
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[6];
m_treeDemo.InsertItem( &tvItem );
tvItem.item.pszText = (LPTSTR)(LPCTSTR)chArrTreeContent[7];
m_treeDemo.InsertItem( &tvItem );
}
void CCtrlDemo2Dlg::OnEndlabeleditTreeDemo(NMHDR* pNMHDR, LRESULT* pResult)
{
TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR; //NMTVDISPINFO
// TODO: Add your control notification handler code here
m_treeDemo.SetItemText( pTVDispInfo->item.hItem,
pTVDispInfo->item.pszText);
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -