现在的位置: 首页 > 综合 > 正文

对CEdit进行继承,支持DropFile得到文件名

2013年10月02日 ⁄ 综合 ⁄ 共 2546字 ⁄ 字号 评论关闭

对CEdit类进行继承,新类CDropFileEdit支持文件的拖拽得到文件名。使用的时候添加头文件到具体程序中,再使用类向导与资源文件关联起来就可以使用。具体代码如下

DropFileEdit.h

  1. #if
    !defined(AFX_DROPFILEEDIT_H__412BC2B6_D7BF_4CBE_9EFB_FCC77B59A21F__INCLUDED_)
  2. #define
    AFX_DROPFILEEDIT_H__412BC2B6_D7BF_4CBE_9EFB_FCC77B59A21F__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. // DropFileEdit.h : header file
  7. //
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CDropFileEdit window
  10. class
    CDropFileEdit :
    public
    CEdit

  11. {
  12. // Construction
  13. public
    :

  14.      CDropFileEdit();
  15. // Attributes
  16. public
    :

  17. // Operations
  18. public
    :

  19. // Overrides
  20.     
    // ClassWizard generated
    virtual function overrides
  21.     
    //{{AFX_VIRTUAL(CDropFileEdit)
  22.     
    //}}AFX_VIRTUAL
  23. // Implementation
  24. public
    :

  25.     
    virtual

    ~CDropFileEdit();

  26.     
    // Generated message map
    functions
  27. protected
    :

  28.     
    //{{AFX_MSG(CDropFileEdit)
  29.      afx_msg
    void

    OnDropFiles(

    HDROP

    hDropInfo);

    //添加消息处理
  30.     
    //}}AFX_MSG
  31.      DECLARE_MESSAGE_MAP()
  32. };
  33. /////////////////////////////////////////////////////////////////////////////
  34. //{{AFX_INSERT_LOCATION}}
  35. // Microsoft Visual C++ will insert additional
    declarations immediately before the previous line.
  36. #endif //
    !defined(AFX_DROPFILEEDIT_H__412BC2B6_D7BF_4CBE_9EFB_FCC77B59A21F__INCLUDED_)

DropFileEdit.cpp

  1. // DropFileEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DropFileEdit.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static

    char
    THIS_FILE[] = __FILE__;

  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CDropFileEdit
  12. CDropFileEdit::CDropFileEdit()
  13. {
  14. }
  15. CDropFileEdit::~CDropFileEdit()
  16. {
  17. }
  18. BEGIN_MESSAGE_MAP(CDropFileEdit, CEdit)
  19.     
    //{{AFX_MSG_MAP(CDropFileEdit)
  20.      ON_WM_DROPFILES()                            
    //消息处理
  21.     
    //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDropFileEdit message handlers
  25. void

    CDropFileEdit::OnDropFiles(

    HDROP

    hDropInfo)
  26. {
  27.     
    // TODO: Add your message
    handler code here and/or call default
  28.     
    //取得被拖动文件的数目     
  29.     
    int

    DropCount=DragQueryFile(hDropInfo,-1,NULL,0);
  30.      
    for
    (
    int
    i=0;i<DropCount;i++)

  31.       {
  32.         
  33.         
    int

    NameSize=DragQueryFile(hDropInfo,i,NULL,0);

  34.         
    HANDLE

    hHeap=GetProcessHeap();

  35.         
  36.         
    char

    *pName=(

    LPSTR
    )HeapAlloc(hHeap,HEAP_ZERO_MEMORY,NameSize++);

  37.         
    if
    (pName==NULL)

  38.          {
  39.              MessageBox(
    "给文件名分配暂存空间时出错!"
    ,
    "错误信息"
    ,MB_ICONERROR);

  40.             
    return
    ;

  41.          }
  42.         
    //取得第i个拖动文件名所占字节数
  43.          DragQueryFile(hDropInfo,i,pName,NameSize);
  44.     
    //把文件名拷贝到缓冲区
  45.     
    //  
    m_Dialog_List.AddString(pName);
  46.     
    //文件名加入listbox中显示
  47.         
    this
    ->SetWindowText(pName);

  48.          HeapFree(hHeap,HEAP_ZERO_MEMORY,pName);
  49.     
    //释放缓冲区
  50.      }
  51.      CEdit::OnDropFiles(hDropInfo);
  52. }

抱歉!评论已关闭.