VC组合框的使用示例

来源:互联网 发布:淘宝仅退款规则 编辑:程序博客网 时间:2024/06/05 06:34

示例实现的功能

  • 自动查找当前目录下的所有txt文件,并显示在组合框中;
  • 用户可以自己选择其他目录下的txt文件

效果

工具启动后的样子:

run1

组合框的下拉效果:

run2

点击按钮,自己选择任意位置的txt文件:

run3

选择的txt文件显示在第一个位置:

run4

代码

对话框设计

IDD_COMBOBOXTEST_DIALOG DIALOGEX 0, 0, 240, 65STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENUEXSTYLE WS_EX_APPWINDOWCAPTION "ComboBoxTest"FONT 8, "MS Sans Serif"BEGIN    COMBOBOX        IDC_COMBO_FILES,16,18,199,53,CBS_DROPDOWN |                     CBS_AUTOHSCROLL | CBS_SORT | WS_VSCROLL | WS_TABSTOP    PUSHBUTTON      "...",IDC_BUTTON_SELECT_FILE,219,17,14,13    DEFPUSHBUTTON   "OK",IDOK,39,44,50,14    PUSHBUTTON      "Cancel",IDCANCEL,109,44,50,14END

UI:

dialog1

dialog2

注意要在组合框右边的下拉箭头的地方按住,然后会出现一个上下调整的箭头,往下拉即可。这个往下拉的空间,就是运行后显示的空间。否则,运行的时候,下拉后没有反应。

App

在app中,实现一个小函数,获取程序的当前目录:

.h文件:

public:    CString GetCurDir() const;private:    void InitCurrentDirectory();private:    CString m_currentDirectory; // No back slash at the end.

实现文件:

void CComboBoxTestApp::InitCurrentDirectory(){    char currentDirectory[MAX_PATH] = {0};    DWORD nBufferLength = MAX_PATH;    GetCurrentDirectory (nBufferLength, currentDirectory);    m_currentDirectory = CString(currentDirectory);}CString CComboBoxTestApp::GetCurDir() const{    return m_currentDirectory;}

另外需要在构造函数中初始化成员变量,在InitInstance()中调用InitCurrentDirectory()初始化当前路径。

Dialog

全贴:

class CComboBoxTestDlg : public CDialog{// Constructionpublic:    CComboBoxTestDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data    //{{AFX_DATA(CComboBoxTestDlg)    enum { IDD = IDD_COMBOBOXTEST_DIALOG };    CComboBox   m_ctrlFiles;    //}}AFX_DATA    // ClassWizard generated virtual function overrides    //{{AFX_VIRTUAL(CComboBoxTestDlg)    protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    //}}AFX_VIRTUAL// Implementationprotected:    HICON m_hIcon;    // Generated message map functions    //{{AFX_MSG(CComboBoxTestDlg)    virtual BOOL OnInitDialog();    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);    afx_msg void OnPaint();    afx_msg HCURSOR OnQueryDragIcon();    afx_msg void OnButtonSelectFile();    virtual void OnOK();    //}}AFX_MSG    DECLARE_MESSAGE_MAP()protected:    BOOL FindAllTxtFiles(CStringArray& txtFiles);};

实现文件:

实现定义全局变量,因为要调用app中的方法:

extern CComboBoxTestApp theApp;

数据/消息映射:

void CComboBoxTestDlg::DoDataExchange(CDataExchange* pDX){    CDialog::DoDataExchange(pDX);    //{{AFX_DATA_MAP(CComboBoxTestDlg)    DDX_Control(pDX, IDC_COMBO_FILES, m_ctrlFiles);    //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CComboBoxTestDlg, CDialog)    //{{AFX_MSG_MAP(CComboBoxTestDlg)    ON_WM_SYSCOMMAND()    ON_WM_PAINT()    ON_WM_QUERYDRAGICON()    ON_BN_CLICKED(IDC_BUTTON_SELECT_FILE, OnButtonSelectFile)    //}}AFX_MSG_MAPEND_MESSAGE_MAP()

对话框初始化:

BOOL CComboBoxTestDlg::OnInitDialog(){    CDialog::OnInitDialog();    // ....    // TODO: Add extra initialization here    CStringArray txtFiles;    if (FindAllTxtFiles(txtFiles)) {        for (int i = 0; i < txtFiles.GetSize(); i++) {            m_ctrlFiles.AddString((LPCTSTR)txtFiles[i]);        }    }    return TRUE;  // return TRUE  unless you set the focus to a control}

事件处理:

void CComboBoxTestDlg::OnButtonSelectFile() {    char szFilter[] = "Text Files(*.txt)|*.txt||";    CFileDialog dlg(TRUE, NULL, NULL, NULL, szFilter);     dlg.m_ofn.lpstrTitle = _T("Select Text File ...");    if (dlg.DoModal() != IDOK) {        return;    }    CString fileName = dlg.GetPathName();    m_ctrlFiles.InsertString(0, (LPCTSTR)fileName);    m_ctrlFiles.SetCurSel(0);}void CComboBoxTestDlg::OnOK() {    // TODO: Add extra validation here    CDialog::OnOK();}BOOL CComboBoxTestDlg::FindAllTxtFiles(CStringArray& txtFiles){    WIN32_FIND_DATA wfd;    CString curDir = theApp.GetCurDir() + "\\";    CString configFileName = curDir + "*.txt";    HANDLE hFind = FindFirstFile((LPCTSTR)configFileName, &wfd);    if (INVALID_HANDLE_VALUE == hFind) return FALSE;    for (;;) {        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {            txtFiles.Add(curDir + CString(wfd.cFileName));        }        if (!FindNextFile(hFind, &wfd)) break;    }    FindClose(hFind);    return TRUE;}

本文的的pdf版本

http://download.csdn.net/detail/u013344915/9371766

0 0