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

📄 advancedctrldlg.cpp

📁 从学案例入手
💻 CPP
字号:
// AdvancedCtrlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AdvancedCtrl.h"
#include "AdvancedCtrlDlg.h"

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

char *szLabel[2]={"Rectangle","Circle"};
char *szColumn[3]={"Shape","Fore color","Back color"};
char *szData[4][3]=
	{
		{"Rectangle","Blue","Yellow"},
		{"Rectangle","Red","Blue"},
		{"Circle","Blue","Yellow"},
		{"Circle","Red","Blue"}
	};
DWORD nStyle[4]={LVS_ICON,LVS_SMALLICON,LVS_LIST,LVS_REPORT};

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAdvancedCtrlDlg dialog

CAdvancedCtrlDlg::CAdvancedCtrlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAdvancedCtrlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAdvancedCtrlDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAdvancedCtrlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAdvancedCtrlDlg)
	DDX_Control(pDX, IDC_COMBO1, m_ComboBox);
	DDX_Control(pDX, IDC_SPIN1, m_Spin);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDC_SLIDER1, m_Slider);
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Control(pDX, IDC_TREE1, m_Tree);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAdvancedCtrlDlg, CDialog)
	//{{AFX_MSG_MAP(CAdvancedCtrlDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_HSCROLL()
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvancedCtrlDlg message handlers

BOOL CAdvancedCtrlDlg::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_Spin.SetRange(0,200); 
	m_Spin.SetPos(0);

	//初始化轨道条
	m_Slider.SetRange(0,20); 
	m_Slider.SetTicFreq(2);
	m_Slider.SetLineSize(2);
	m_Slider.SetPageSize(4);
	m_Slider.SetPos(0);

	//初始化进度条
	m_Progress.SetRange(0,20); 
	m_Progress.SetPos(0);

	//创建位图序列,用于树形视图和列表视图显示图像
	m_SmallImageList.Create(IDB_BITMAP1,16,0,FALSE); //16*16的位图序列
	m_LargeImageList.Create(IDB_BITMAP2,32,0,FALSE); //32*32的位图序列

	//初始化树形视图
	TV_INSERTSTRUCT tvInsert; 
	HTREEITEM hItem;
	int i,j;
	char buffer[20];
	m_Tree.SetImageList(&m_SmallImageList,TVSIL_NORMAL);
	tvInsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
	for(i=0;i<2;i++)
	{
		tvInsert.hParent=NULL; //指定该项位于最高层
		tvInsert.hInsertAfter=TVI_LAST;
		tvInsert.item.pszText=szLabel[i];
		tvInsert.item.iImage=i*2; //指定表项显示的图象
		tvInsert.item.iSelectedImage=i*2+1; //指定选择状态下应显示的图象
		hItem=m_Tree.InsertItem(&tvInsert);
		for(j=0;j<3;j++)	
		{ 
			tvInsert.hParent=hItem; //指定该项为子项
			tvInsert.hInsertAfter=TVI_SORT;
			sprintf(buffer,"%s%d",szLabel[i],j);
			tvInsert.item.pszText=buffer;
			m_Tree.InsertItem(&tvInsert);
		}
	}

	//初始化列表视图
	LV_COLUMN lvc;
	LV_ITEM lvi;
	m_List.SetImageList(&m_SmallImageList,LVSIL_SMALL);
	m_List.SetImageList(&m_LargeImageList,LVSIL_NORMAL);
	m_ComboBox.SelectString(-1,"Report");
	lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
	lvc.fmt=LVCFMT_LEFT;
	for(i=0;i<3;i++) //插入各列
	{
		lvc.pszText=szColumn[i];
		if(i==0)
			lvc.cx=m_List.GetStringWidth(szColumn[0])+50;
		else
			lvc.cx=m_List.GetStringWidth(szColumn[i])+15;
		lvc.iSubItem=i;
		m_List.InsertColumn(i,&lvc);
	}
	lvi.mask=LVIF_TEXT|LVIF_IMAGE;
	lvi.iSubItem=0;
	for(i=0;i<4;i++) //插入表项
	{
		lvi.pszText=szData[i][0];
		lvi.iItem=i;	
		lvi.iImage=i;
		m_List.InsertItem(&lvi);
		for(j=1;j<3;j++)
			m_List.SetItemText(i,j,szData[i][j]);
		}	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAdvancedCtrlDlg::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 CAdvancedCtrlDlg::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 CAdvancedCtrlDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAdvancedCtrlDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	CSliderCtrl* pSlider=(CSliderCtrl*)pScrollBar;

	//判断是否是m_Slider轨道条发送的消息
	if(&m_Slider!=pSlider) 
		return; 
	m_Progress.SetPos(m_Slider.GetPos());
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CAdvancedCtrlDlg::OnSelchangeCombo() 
{
	// TODO: Add your control notification handler code here
	long lStyle;
	lStyle=GetWindowLong(m_List.GetSafeHwnd(),GWL_STYLE);

	//清除所有与显示格式有关的风格标志
	lStyle&=~(LVS_ICON|LVS_SMALLICON|LVS_LIST|LVS_REPORT);
	lStyle|=nStyle[m_ComboBox.GetCurSel()];

	//设置新的风格
	SetWindowLong(m_List.GetSafeHwnd(),GWL_STYLE,lStyle); 
	m_List.Invalidate(); //刷新	
}

void CAdvancedCtrlDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

⌨️ 快捷键说明

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