我想实现当在目录窗口或资源管理器中选中文件时,得到此文件的文件名.我用HOOK CALLWNDPROC拦截LVN_ITEMCHANGED,然后用SendMessage发送了LVM_GETITEMTEXT来得到LIST VIEW中的文本,可这只能得到文件名,却得不到文件路径,有什么办法可得到选中文件的文件路径?
还有,我在钩子函数加了一条MessageBox,用来显示得到的文件名,可每次我在资源管理器中选中文件,MessageBox都会弹出三次,前两次显示上次选中的文件名,第三次显示本次选中的文件名,这是为什么?

解决方案 »

  1.   

    一,你应该拦截LVN_ITEMCHANGING,并且
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    if(pNMLV->uNewState==0 && pNMLV->uOldState==1)
    {
    ...
    }
    二。取得了文件名,再调用GetFullPathName不就行了么?
      

  2.   

    牙科医生我的是英文的而我英文有不好能否帮忙讲一下NMLISTVIEW这个结构,
    还有reinterpret_cast  <LPNMLISTVIEW  >这是什么意思?是取指针么?
      

  3.   

    1.GetFullPathName
    2.在钩子函数里你用的是什么钩子呢?
      

  4.   

    WH_CALLWNDPROC
    顺便问一下,CSDN的BLOG怎么用?为什么总是提示必须输入正文,可我已经输入了?
      

  5.   

    reinterpret_cast  <LPNMLISTVIEW  >,是强制类型转换。在些内容在MSDN中有。
    NMLISTVIEW Structure--------------------------------------------------------------------------------Contains information about a list-view notification message. This structure is the same as the NM_LISTVIEW structure but has been renamed to fit standard naming conventions. Syntaxtypedef struct tagNMLISTVIEW {
        NMHDR hdr;
        int iItem;
        int iSubItem;
        UINT uNewState;
        UINT uOldState;
        UINT uChanged;
        POINT ptAction;
        LPARAM lParam;
    } NMLISTVIEW, *LPNMLISTVIEW;
    Membershdr
    NMHDR structure that contains information about this notification message. 
    iItem
    Identifies the list-view item, or -1 if not used. 
    iSubItem
    Identifies the subitem, or zero if none. 
    uNewState
    New item state. This member is zero for notification messages that do not use it. 
    uOldState
    Old item state. This member is zero for notification messages that do not use it. 
    uChanged
    Set of flags that indicate the item attributes that have changed. This member is zero for notifications that do not use it. Otherwise, it can have the same values as the mask member of the LVITEM structure. 
    ptAction
    POINT structure that indicates the location at which the event occurred. This member is undefined for notification messages that do not use it. 
    lParam
    Application-defined value of the item. This member is undefined for notification messages that do not use it. 
    Structure InformationMinimum DLL Version comctl32.dll version 4.70 or later 
    Header commctrl.h 
    Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 3.0, Windows 98, Windows 95 with Internet Explorer 3.0 
    --------------------------------------------------------------------------------&copy; 2003 Microsoft Corporation. All rights reserved
    MSDN中只有E文的。
      

  6.   

    GetFullPathName只能得到当前目录里的文件的长文件名而我需要判断的文件并不在当前目录里.解决不了问题,期待高手出现!
      

  7.   

    拦到了LVN_ITEMCHANGED,应该可以从参数中找到该Item, 一般信息都会保存在LVITEM里面啊
      

  8.   

    不好意思,偶是自己定义了一个扩展的LVITEM,然后把LVITEM.里面的lparam指向我的扩展可以获得,不过前提是列表是我创建的.这边可能没太大用
      

  9.   

    可以用GetCurrentDirectory,看看是否是想要的路径
      

  10.   

    用FindFirstFile有一个问题,那就是如果在不同目录下有同名文件,则无法区分,我的想法,既然当我们在目录窗口或资源管理器中选中文件时,系统已经知道了有关这个文件的的信息,哪我们也一定能通过一些方法从系统中得到想得到有关这个文件的路径,只是我找不到方法,郁闷哪.
      

  11.   

    有个位图预览对话框的例子,你可一看一下
    /*************************************************************************BmpDlg.h header fileCBmpDialog: A bitmap open dialog with preview. 
                by The GremlinCompatibility: Visual C++ 4.0 and up.**************************************************************************/#ifndef _BMPDIALOG_H_
    #define _BMPDIALOG_H_
    // CBmpDialog dialogclass CBmpDialog : public CFileDialog
    {
    DECLARE_DYNAMIC(CBmpDialog)public:
    CBmpDialog(BOOL bOpenFileDialog = TRUE, // TRUE for FileOpen, FALSE for FileSaveAs. Who would want a save with preview?
    LPCTSTR lpszDefExt = NULL,
    LPCTSTR lpszFileName = NULL,
    DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    LPCTSTR lpszFilter = NULL,
    CWnd* pParentWnd = NULL);    static BOOL m_showpreview;  // Store this variable in the registry if you want the "Show Preview" setting to be persistent    static HBITMAP hpreview;protected:
    //{{AFX_MSG(CBmpDialog)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
    #endif
      

  12.   

    // ***********************************************************************************
    //
    // BmpDlg.cpp : implementation file
    //
    // ************************************************************************************#include "stdafx.h"
    #include "BmpDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif// In order to ease use, these values have been hard coded in bmpdlg.rc
    // This avoids the need for another header file.#define IDC_PREVIEW         (5000)
    #define IDC_PREVIEWBTN     (5001)
    #define IDC_WIDTH           (5002)
    #define IDC_HEIGHT          (5003)
    #define IDC_DEPTH           (5004)
    #define IDC_FSIZE           (5005)
    #define IDC_SHOWPREVIEW     (5006)HBITMAP CBmpDialog::hpreview = NULL;
    BOOL CBmpDialog::m_showpreview = TRUE;// Proprietary Hook function for open dialogUINT APIENTRY OFNHookProc( HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam )
    {
        
    LPDRAWITEMSTRUCT lpdis;
    BITMAP bm;
    LPNMHDR pnmh;
    char filename[1024],str[255];
    int height,height2,width,width2;
    NMHDR nmh;
        
        switch (uiMsg)
        {
            case WM_COMMAND:
                
                if (LOWORD(wParam) == IDC_SHOWPREVIEW)
                {
                    CBmpDialog::m_showpreview = IsDlgButtonChecked(hdlg,IDC_SHOWPREVIEW);                if (!CBmpDialog::m_showpreview)
                    {                    
                        if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                    CBmpDialog::hpreview = NULL;                    HWND wnd = GetDlgItem(hdlg,IDC_PREVIEWBTN);                    InvalidateRect(wnd,NULL,TRUE);                        SetDlgItemText(hdlg,IDC_WIDTH,"");
                        SetDlgItemText(hdlg,IDC_HEIGHT,"");
                        SetDlgItemText(hdlg,IDC_DEPTH,"");
                        SetDlgItemText(hdlg,IDC_FSIZE,"");                }
                    else
                    {
                        
                        nmh.code = CDN_SELCHANGE;                    OFNHookProc(hdlg, WM_NOTIFY, 0, (LPARAM)&nmh);
                    }
                }            break;        case WM_DRAWITEM:            if (CBmpDialog::hpreview)
                {
                    lpdis = (LPDRAWITEMSTRUCT)lParam;
                            
                    GetObject(CBmpDialog::hpreview,sizeof(BITMAP),&bm);                CPoint size(bm.bmWidth,bm.bmHeight);
                                                    
                    HDC dcmem = CreateCompatibleDC(lpdis->hDC);
          
                    HBITMAP old = (HBITMAP)SelectObject(dcmem,CBmpDialog::hpreview);                if (bm.bmWidth > bm.bmHeight)
                    {                    height = lpdis->rcItem.bottom - lpdis->rcItem.top;                    float ratio = (float)bm.bmHeight/(float)bm.bmWidth;                 
                        
                        lpdis->rcItem.bottom  = (long) (lpdis->rcItem.top + (lpdis->rcItem.right-lpdis->rcItem.left)*ratio);                    height2 = (height - (lpdis->rcItem.bottom - lpdis->rcItem.top))/2;                    lpdis->rcItem.top += height2;
                        lpdis->rcItem.bottom += height2;                }
                    else
                    {                    width = lpdis->rcItem.right - lpdis->rcItem.left;                    float ratio = (float)bm.bmWidth/(float)bm.bmHeight;                 
                        
                        lpdis->rcItem.right  = (long) (lpdis->rcItem.left + (lpdis->rcItem.bottom-lpdis->rcItem.top)*ratio);                    width2 = (width - (lpdis->rcItem.right - lpdis->rcItem.left))/2;                    lpdis->rcItem.left += width2;
                        lpdis->rcItem.right += width2;                }
                        
                    StretchBlt(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,lpdis->rcItem.right-lpdis->rcItem.left,lpdis->rcItem.bottom-lpdis->rcItem.top,dcmem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);                SelectObject(dcmem,old);                DeleteDC(dcmem);
                }            break;        case WM_NOTIFY:
            
                pnmh = (LPNMHDR) lParam;
      

  13.   

    if (pnmh->code == CDN_FILEOK)
                {
                    if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                // This avoids an assert                _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
                pThreadState->m_pAlternateWndInit = NULL;
                    return 0;
                }
                if (pnmh->code ==  CDN_INITDONE)
                {
                   if (CBmpDialog::hpreview)
                        DeleteObject(CBmpDialog::hpreview);                CheckDlgButton(hdlg,IDC_SHOWPREVIEW,CBmpDialog::m_showpreview);
                    return 0;
                }            if (pnmh->code == CDN_SELCHANGE)
                {                if (!IsDlgButtonChecked(hdlg,IDC_SHOWPREVIEW))
                    {                
                        if (CBmpDialog::hpreview)
                            DeleteObject(CBmpDialog::hpreview);                    CBmpDialog::hpreview = NULL;                    return 0;
                    }
                    
                    SendMessage(GetParent(hdlg),CDM_GETFILEPATH ,1024,(LPARAM)filename);                                if (CBmpDialog::hpreview)
                        DeleteObject(CBmpDialog::hpreview);
                   
                    CBmpDialog::hpreview = (HBITMAP)LoadImage(AfxGetInstanceHandle(),filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);                HWND wnd = GetDlgItem(hdlg,IDC_PREVIEWBTN);                InvalidateRect(wnd,NULL,TRUE);                if (CBmpDialog::hpreview)
                    {
                        GetObject(CBmpDialog::hpreview,sizeof(BITMAP),&bm);
            
                        wsprintf(str,"Width: %d pixels",bm.bmWidth);
                        SetDlgItemText(hdlg,IDC_WIDTH,str);                    wsprintf(str,"Height: %d pixels",bm.bmHeight);
                        SetDlgItemText(hdlg,IDC_HEIGHT,str);
                        
                        switch (bm.bmBitsPixel)
                        {
                            case 1:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 2 (monocromatic)");
                                break;
                            case 4:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 16");
                                break;                        case 8:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 256");
                                break;                        case 16:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 65,536");
                                break;
                            case 24:
                                SetDlgItemText(hdlg,IDC_DEPTH,"Colors: 16 millions");
                                break;                        default:
                                SetDlgItemText(hdlg,IDC_DEPTH,"");                    }                    OFSTRUCT o;                    HFILE f = OpenFile(filename,&o,OF_READ);                    wsprintf(str,"Size: %ld Kb",GetFileSize((HANDLE)f,NULL)/1024);                    SetDlgItemText(hdlg,IDC_FSIZE,str);
                        
                        _lclose(f);
                    }
                    else
                    {
                        SetDlgItemText(hdlg,IDC_WIDTH,"");
                        SetDlgItemText(hdlg,IDC_HEIGHT,"");
                        SetDlgItemText(hdlg,IDC_DEPTH,"");
                        SetDlgItemText(hdlg,IDC_FSIZE,"");
                    }
                                }    }
        return 0;
    }/////////////////////////////////////////////////////////////////////////////
    // CBmpDialogIMPLEMENT_DYNAMIC(CBmpDialog, CFileDialog)CBmpDialog::CBmpDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
    DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
    CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
    {    m_ofn.lpstrFilter = "Bitmaps (*.bmp)\0*.bmp\0"\
                            "All Files (*.*)\0*.*\0\0";                                m_ofn.Flags |= (OFN_HIDEREADONLY |OFN_ENABLEHOOK| OFN_EXPLORER  |OFN_ENABLETEMPLATE);    m_ofn.hInstance = AfxGetInstanceHandle();
        m_ofn.lpTemplateName = MAKEINTRESOURCE(IDC_PREVIEW);
        m_ofn.lpfnHook = OFNHookProc;          
    }
    BEGIN_MESSAGE_MAP(CBmpDialog, CFileDialog)
    //{{AFX_MSG_MAP(CBmpDialog)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()