VC++练习之 组合框和列表框同步

.h
// MfcCtlTestDlg.h : header file
//


#pragma once




// CMfcCtlTestDlg dialog
class CMfcCtlTestDlg : public CDialog
{
// Construction
public:
CMfcCtlTestDlg(CWnd* pParent = NULL);// standard constructor
void Init();
// Dialog Data
enum { IDD = IDD_MFCCTLTEST_DIALOG };


//members
CStatic* m_label;
CListBox* m_lstBox;
CComboBox* m_comBox;


protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support



// Implementation
protected:
HICON m_hIcon;


// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnCbnSelchangeCombotest();
afx_msg void OnLbnSelchangeLsttest();
};

.cpp

// MfcCtlTestDlg.cpp : implementation file
//


#include "stdafx.h"
#include "MfcCtlTest.h"
#include "MfcCtlTestDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// CMfcCtlTestDlg dialog








CMfcCtlTestDlg::CMfcCtlTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMfcCtlTestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CMfcCtlTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CMfcCtlTestDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE(IDC_COMBOTEST, &CMfcCtlTestDlg::OnCbnSelchangeCombotest)
ON_LBN_SELCHANGE(IDC_LSTTEST, &CMfcCtlTestDlg::OnLbnSelchangeLsttest)
END_MESSAGE_MAP()




// CMfcCtlTestDlg message handlers


BOOL CMfcCtlTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// 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
this->Init();






return TRUE;  // return TRUE  unless you set the focus to a control
}


// 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 CMfcCtlTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMfcCtlTestDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}






void CMfcCtlTestDlg::Init(){
this->m_comBox = (CComboBox*)GetDlgItem(IDC_COMBOTEST);//获得当前combobox句柄
this->m_lstBox = (CListBox*)GetDlgItem(IDC_LSTTEST);
this->m_label = (CStatic*)GetDlgItem(IDC_SHOWTEXT);
CStringList list ;
list.AddTail(_T("aaa"));
list.AddTail(_T("bbb"));
list.AddTail(_T("ccc"));
list.AddTail(_T("ddd"));
list.AddTail(_T("eee"));
list.AddTail(_T("fff"));


POSITION rPos;
rPos = list.GetHeadPosition();
while (rPos != NULL)
{
   CString s= list.GetNext(rPos);
   this->m_comBox->AddString(s);
}




//




POSITION rPos1;
rPos1 = list.GetHeadPosition();
while (rPos1 != NULL)
{
   CString s= list.GetNext(rPos1);
   this->m_lstBox->AddString(s);
}


//free(list);


}






void CMfcCtlTestDlg::OnCbnSelchangeCombotest()
{
//this->m_comBox = (CComboBox*)GetDlgItem(IDC_COMBOTEST);//获得当前combobox句柄
// this->m_lstBox = (CListBox*)GetDlgItem(IDC_LSTTEST);


 CString strCom;
 this->m_comBox->GetLBText(this->m_comBox->GetCurSel(),strCom);


int i;
for(i = 0;i < this->m_lstBox->GetCount();i ++){
 CString strLst;
 this->m_lstBox->GetText(i,strLst);


 if(strLst.Compare(strCom) == 0){
this->m_lstBox->SetCurSel(i);
设置标签内容   
this->m_label->SetWindowTextW(strCom);
 }
}




// TODO: Add your control notification handler code here
}


void CMfcCtlTestDlg::OnLbnSelchangeLsttest()
{


 CString strLst;
 this->m_lstBox->GetText( this->m_lstBox->GetCurSel(),strLst);


int i;
for(i = 0;i <  this->m_comBox->GetCount();i ++){
 CString strCom;
 this->m_comBox->GetLBText(i,strCom);


 if(strLst.Compare(strCom) == 0){
 this->m_comBox->SetCurSel(i);
设置标签内容   
this->m_label->SetWindowTextW(strLst);
 }
}
// TODO: Add your control notification handler code here
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值