FileListView

来源:互联网 发布:中国核事故知乎 编辑:程序博客网 时间:2024/06/07 07:39

// FileListView.cpp : implementation file
//
#include "stdafx.h"
#include "iDART_UI.h"
#include "Resource.h"
#include "FileListView.h"


class CShellTreeMenuButton : public CMFCToolBarMenuButton
{
    friend class CFileListView;

    DECLARE_SERIAL(CShellTreeMenuButton)

public:
    CShellTreeMenuButton(HMENU hMenu = NULL) : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
    {
    }

    virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
        BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
    {
        pImages = CMFCToolBar::GetImages();

        CAfxDrawState ds;
        pImages->PrepareDrawImage(ds);

        CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);

        pImages->EndDrawImage(ds);
    }
};
IMPLEMENT_SERIAL(CShellTreeMenuButton, CMFCToolBarMenuButton, 1)
    // CFileListView

    IMPLEMENT_DYNAMIC(CFileListView, CDockablePane)

    CFileListView::CFileListView()
{
    m_nCurrSort = ID_SORTING_GROUPBYTYPE;
    m_curItem  = "";
}

CFileListView::~CFileListView()
{
}


BEGIN_MESSAGE_MAP(CFileListView, CDockablePane)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_CONTEXTMENU()
    ON_COMMAND(ID_FILELISTVIEW_RUN, &CFileListView::OnFilelistviewRun)
    ON_WM_PAINT()
END_MESSAGE_MAP()

 

// CFileListView message handlers

int CFileListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDockablePane::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here
    CRect rectDummy;
    rectDummy.SetRectEmpty();
    // 创建视图:
    const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

    if (!m_FileTreeCtl.Create(dwViewStyle, rectDummy, this, 1))
    {
        TRACE0("未能创建类视图\n");
        return -1;      //未能创建
    }

    // 加载图像:
    m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_SORT);
    m_wndToolBar.LoadToolBar(IDR_SORT, 0, 0, TRUE /* 已锁定*/);
    ChangeVisualStyle();
    m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
    m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));

    m_wndToolBar.SetOwner(this);


    // 所有命令将通过此控件路由,而不是通过主框架路由:
    m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
    CMenu menuSort;
    menuSort.LoadMenu(IDR_POPUP_SORT);
    m_wndToolBar.ReplaceButton(ID_SORT_MENU, CShellTreeMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));

    CShellTreeMenuButton* pButton =  DYNAMIC_DOWNCAST(CShellTreeMenuButton, m_wndToolBar.GetButton(0));

    if (pButton != NULL)
    {
        pButton->m_bText = FALSE;
        pButton->m_bImage = TRUE;
        pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
        pButton->SetMessageWnd(this);
    }
    HTREEITEM ht = m_FileTreeCtl.InsertItem("#RawData",2,3,TVI_ROOT,TVI_LAST);
    m_bIsStartPro = FALSE;
    m_curPos = 0;
    return 0;
}

void CFileListView::OnSize(UINT nType, int cx, int cy)
{
    CDockablePane::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here
    AdjustLayout();
}

void CFileListView::AdjustLayout()
{
    if (GetSafeHwnd() == NULL)
    {
        return;
    }
    CRect rectClient;
    GetClientRect(rectClient);
    int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

    m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
    m_FileTreeCtl.SetWindowPos(NULL, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);

}

void CFileListView::fillFileTree(CString& rawDataPath,HTREEITEM ht)
{
    HTREEITEM ht2;
    CFileFind finder;
    CString str;
    CString name;
    rawDataPath += _T("\\*.*");
    BOOL bWorking = finder.FindFile(rawDataPath);
    while (bWorking)
    {

        bWorking = finder.FindNextFile();
        if (finder.IsDots())
        {
            continue;
        }
        str = finder.GetFilePath();
        name = finder.GetFileName();
        if (finder.IsDirectory())
        {
            ht2 = m_FileTreeCtl.InsertItem(name,2,3,ht,TVI_LAST);
            fillFileTree(str,ht2);
        }else
        {

            m_FileTreeCtl.InsertItem(name,0,1,ht,TVI_LAST);
            m_listMap[name] = str;
        }
        int last=str.ReverseFind('.');
  
    }
    finder.Close();

}

void CFileListView::ChangeVisualStyle()
{
    m_FilelistImages.DeleteImageList();

    UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;

    CBitmap bmp;
    if (!bmp.LoadBitmap(uiBmpId))
    {
        TRACE(_T("无法加载位图: %x\n"), uiBmpId);
        ASSERT(FALSE);
        return;
    }

    BITMAP bmpObj;
    bmp.GetBitmap(&bmpObj);

    UINT nFlags = ILC_MASK;

    nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;

    m_FilelistImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
    m_FilelistImages.Add(&bmp, RGB(255, 0, 0));

    m_FileTreeCtl.SetImageList(&m_FilelistImages, TVSIL_NORMAL);

    m_wndToolBar.CleanUpLockedImages();
    m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* 锁定*/);
}

void CFileListView::OnUpdateFlieList()
{
    m_FileTreeCtl.DeleteAllItems();
    HTREEITEM ht = m_FileTreeCtl.InsertItem("#RawData",2,3,TVI_ROOT,TVI_LAST);
    if (theApp.m_bPrjIsCreated == FALSE)
    {
        return;
    }

    CString filePath = theApp.m_PrjPath+'\\'+"RawData"+'\\';
    m_listMap.clear();//清空
    fillFileTree(filePath,ht);
    m_FileTreeCtl.Expand(ht,TVE_EXPAND);
}

void CFileListView::OnContextMenu(CWnd* pWnd, CPoint point)
{
    // TODO: Add your message handler code here
    CTreeCtrl* pWndTree = (CTreeCtrl*)&m_FileTreeCtl;
    ASSERT_VALID(pWndTree);

    if (pWnd != pWndTree)
    {
        CDockablePane::OnContextMenu(pWnd, point);
        return;
    }

    if (point != CPoint(-1, -1))
    {
        // 选择已单击的项:
        CPoint ptTree = point;
        pWndTree->ScreenToClient(&ptTree);

        UINT flags = 0;
        HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
        if (hTreeItem != NULL)
        {
            pWndTree->SelectItem(hTreeItem);
            m_curItem = pWndTree->GetItemText(hTreeItem);
        }else
        {
            m_curItem = "";
        }
        theApp.m_curFileListPath = m_listMap[m_curItem];
        theApp.m_FileListItem = m_curItem;
        if (theApp.m_curFileListPath == "")
        {
            return;
        }

    }

    pWndTree->SetFocus();
    CMenu menu;
    menu.LoadMenu(IDR_FILELIST_MENU);
    CMenu* pSumMenu = menu.GetSubMenu(0);

    if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
    {
        CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;

        if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
            return;

        ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
        UpdateDialogControls(this, FALSE);
    }
}

 

void CFileListView::OnFilelistviewRun()
{
    // TODO: Add your command handler code here
    CString str = theApp.m_curFileListPath;
    int last=str.ReverseFind('.');
    if(str.Mid(last+1,str.GetLength()-last).MakeLower()=="csv"||str.Mid(last+1,str.GetLength()-last).MakeLower()=="xls")
    {

        theApp.m_pMainWnd->SendMessage(MYWM_CREATEGRID);
    }else
    {
        MessageBox("无法打开文件");
    }

}


CString CFileListView::getcurItem(void)
{
    return m_curItem;
}


void CFileListView::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    // Do not call CDockablePane::OnPaint() for painting messages

    CRect rectTree;
    m_FileTreeCtl.GetWindowRect(rectTree);
    ScreenToClient(rectTree);

    rectTree.InflateRect(1, 1);
    dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
}