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

📄 extendlg.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// extendlg.cpp : implementation file
//

#include "stdafx.h"
#include "dialogs.h"
#include "extendlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(DExtensionDlg, CDialog)
	//{{AFX_MSG_MAP(DExtensionDlg)
	ON_BN_CLICKED(IDC_ADD, CtlAdd)
	ON_BN_CLICKED(IDC_CLOSE, CtlClose)
	ON_BN_CLICKED(IDC_DELETE, CtlDelete)
	ON_BN_CLICKED(IDC_REPLACE, CtlReplace)
	ON_BN_DOUBLECLICKED(IDC_DELETE, CtlDoubleclickedDelete)
	ON_EN_CHANGE(IDC_DESCRIPTION, CtlChangeDescription)
	ON_EN_CHANGE(IDC_EXTENSION, CtlChangeExtension)
	ON_LBN_DBLCLK(IDC_LIST1, CtlDblclkList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DExtensionDlg dialog


DExtensionDlg::DExtensionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(DExtensionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(DExtensionDlg)
	//}}AFX_DATA_INIT
}

void DExtensionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DExtensionDlg)
	DDX_Control(pDX, IDC_REPLACE, d_bnReplace);
	DDX_Control(pDX, IDC_LIST1, d_listExtensions);
	DDX_Control(pDX, IDC_EXTENSION, d_edExtension);
	DDX_Control(pDX, IDC_DESCRIPTION, d_edDescription);
	DDX_Control(pDX, IDC_DELETE, d_bnDelete);
	DDX_Control(pDX, IDC_ADD, d_bnAdd);
	//}}AFX_DATA_MAP
}

///////////////////////////////////////////////////////////////////////////////
// DExtensionDlg helper functions.

//-----------------------------------------------------------------------------
// Helper function to copy listbox contents to a string list.
void DExtensionDlg::CopyListboxToStringList(CListBox &list, CStringList * psl)
{
    CString csBuffer;
    CString csExt;
    CString csDesc;

    // Loop through strings in listbox.
    int nCount = list.GetCount();
    for (int i = 0; i < nCount; i++)
    {
        // Fetch next item from listbox.
        list.GetText(i, csBuffer);

        // Split string between description and extension.
        SplitAtChar(csBuffer, csExt, csDesc, (TCHAR)'\t');

        // Add to string list.
        psl->AddTail(csDesc);
        psl->AddTail(csExt);
    }
}

//-----------------------------------------------------------------------------
// Helper function to split a CString into two CStrings.
void DExtensionDlg::SplitAtChar(CString& csIn, CString& csLeft, 
                                CString& csRight, TCHAR tch)
{
    // Search for tab character.
    int iChar = csIn.Find(tch);
    int nRightLen = csIn.GetLength() - iChar - 1;

    // Split string at search character.
    csRight = csIn.Right(nRightLen);
    csLeft  = csIn.Left(iChar);
}

//-----------------------------------------------------------------------------
// Helper function to copy contents of edit control to specific listbox item.
void DExtensionDlg::CopyEditToListboxAt(int nIndex)
{
    // Fetch extension string.
    CString csExt;
    d_edExtension.GetWindowText(csExt);

    // Fetch description string.
    CString csDesc;
    d_edDescription.GetWindowText(csDesc);

    // Combine text strings.
    CString csList = csExt;
    csList += _T("\t");
    csList += csDesc;

    // Add text string to listbox.
    nIndex = d_listExtensions.InsertString(nIndex, csList);

    // Make item currently selected.
    d_listExtensions.SetCurSel(nIndex);
 }

/////////////////////////////////////////////////////////////////////////////
// DExtensionDlg message handlers

//-----------------------------------------------------------------------------
// Handle WM_INITDIALOG for File Extensions dialog.
BOOL DExtensionDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

    // Create string list pointer with shorter name.
    CStringList * psl = &d_cslFileType;

    // Make sure string list has even number of items.
    int nCount = psl->GetCount();
    ASSERT(((nCount/2)*2) == nCount);

    // Set tab stop in listbox.
    int nTab[6];
    nTab[0] = 106;  // Hard-coded based on dialog box definition.
    nTab[1] = 116;
    nTab[2] = 126;
    nTab[3] = 136;
    nTab[4] = 146;
    nTab[5] = 156;
    d_listExtensions.SetTabStops(6, nTab);

    // Loop through all strings in CListStrings, adding to listbox.
    POSITION pos = psl->GetHeadPosition();
    while (pos != NULL)
    {
        // Get an empty CString.
        CString csFilter;
        csFilter.Empty();

        // Fetch description from next position.
        CString csTemp;
        csTemp = psl->GetNext(pos);

        // Concatenate file extension and file description, as in:
        //   "*.cpp"  +  "Tab"  +  "C++ source files"
        csFilter += psl->GetNext(pos);
        csFilter += _T("\t");
        csFilter += csTemp;

        // Add to listbox.
        d_listExtensions.AddString(csFilter);
    }

    // Disable Add and Replace buttons.
    d_bnAdd.EnableWindow(FALSE);
    d_bnReplace.EnableWindow(FALSE);

    // Refine controls depending on number of items in listbox.
    if (nCount > 0)
    {
        // Set listbox selection to first item.
        d_listExtensions.SetCurSel(0);
    }
    else
    {
        // Disable Delete button 
        d_bnDelete.EnableWindow(FALSE);
    }

	return TRUE;
}

//-----------------------------------------------------------------------------
// Handle BN_CLICKED notification for Add button.
void DExtensionDlg::CtlAdd() 
{
    CopyEditToListboxAt(-1);

    // Empty contents of both text buffers.
    d_edExtension.SetWindowText(_T(""));
    d_edDescription.SetWindowText(_T(""));

    // Set focus to extensions control.
    d_edExtension.SetFocus();

    // Enable Delete button 
    d_bnDelete.EnableWindow(TRUE);
}

//-----------------------------------------------------------------------------
// Handle BN_CLICKED notification for Close button.
void DExtensionDlg::CtlClose() 
{
    // Empty contents of list.
    d_cslFileType.RemoveAll();

    // Rebuild string list.
    CopyListboxToStringList(d_listExtensions, &d_cslFileType);

    CDialog::OnOK();
    return;
}

//-----------------------------------------------------------------------------
// Handle BN_CLICKED notification for Delete button.
void DExtensionDlg::CtlDelete() 
{
    // Query currently selected item.
    int iItem = d_listExtensions.GetCurSel();
    if (iItem == LB_ERR)
    {
        ::MessageBeep(MB_OK);
        return;
    }

    // Delete listbox item.
    d_listExtensions.DeleteString(iItem);

    // Get number of items in listbox.
    int iLast = d_listExtensions.GetCount() - 1;

    // If Listbox is empty...
    if (iLast == LB_ERR)
    {
        // Disable Delete button when listbox is empty.
        d_bnDelete.EnableWindow(FALSE);

        // Give keyboard focus to extensions edit control.
        d_edExtension.SetFocus();
    }
    else
    {
        // Keep current selection near same spot as previously.
        int iNext = (iItem > iLast) ? iLast : iItem;
        d_listExtensions.SetCurSel(iNext);

        // Give keyboard focus to listbox.
        d_listExtensions.SetFocus();
    }
}

//-----------------------------------------------------------------------------
// Handle BN_CLICKED notification for Replace button.
void DExtensionDlg::CtlReplace() 
{
    // Query currently selected item.
    int iItem = d_listExtensions.GetCurSel();
    if (iItem == LB_ERR)
    {
        return;
    }

    // Delete currently selected string.
    d_listExtensions.DeleteString(iItem);

    // Move edit control data to listbox.
    CopyEditToListboxAt(iItem);

    // Empty contents of both text buffers.
    d_edExtension.SetWindowText(_T(""));
    d_edDescription.SetWindowText(_T(""));

    // Set focus to extensions control.
    d_edExtension.SetFocus();

    // Make new item in listbox the selected item.
    d_listExtensions.SetCurSel(iItem);
}

//-----------------------------------------------------------------------------
// Handle BN_DOUBLECLICKED notification for Delete button.
void DExtensionDlg::CtlDoubleclickedDelete() 
{
    // To handle situation when user is clicking like crazy,
    // interpret a double-click as a single click.
    CtlDelete();
}

//-----------------------------------------------------------------------------
// Handle EN_CHANGE notification for Description edit control.
void DExtensionDlg::CtlChangeDescription() 
{
    // Calculate length of strings in edit controls.
    int nLen1 = d_edDescription.GetWindowTextLength();
    int nLen2 = d_edExtension.GetWindowTextLength();

    // Enable [Add] button when edit controls have text,
    // Otherwise disable it.
    BOOL bEnable = (nLen1 > 0 && nLen2 > 0) ? TRUE : FALSE;
    d_bnAdd.EnableWindow(bEnable);

    // Enable [Replace] button when edit controls have text,
    // AND when an item is currently selected.
    int nCurSel = d_listExtensions.GetCurSel();
    d_bnReplace.EnableWindow(bEnable && nCurSel != LB_ERR);
}

//-----------------------------------------------------------------------------
// Handle EN_CHANGE notification for Extension edit control.
void DExtensionDlg::CtlChangeExtension() 
{
    // Handle changes same as other edit control.
    CtlChangeDescription();
}

//-----------------------------------------------------------------------------
// Handle LBN_DBLCLK double mouse click notification.
void DExtensionDlg::CtlDblclkList1() 
{
    // Query currently selected item.
    int iItem = d_listExtensions.GetCurSel();
    if (iItem == LB_ERR)
    {
        return;
    }

    // Fetch currently selected description string
    CString csBuffer;
    CString csExt;
    CString csDesc;
    d_listExtensions.GetText(iItem, csBuffer);

    // Split string between description and extension.
    SplitAtChar(csBuffer, csExt, csDesc, (TCHAR)'\t');

    // Put strings into respective controls.
    d_edExtension.SetWindowText(csExt);
    d_edDescription.SetWindowText(csDesc);

    // Set focus to extensions control.
    d_edExtension.SetFocus();
}

⌨️ 快捷键说明

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