找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 273|回复: 1

Windows界面编程第七篇 文件拖拽(文件拖放)

[复制链接]

52

主题

62

回帖

0

积分

版主

积分
0
发表于 2013-10-8 00:21:49 | 显示全部楼层 |阅读模式
本篇Windows界面编程第七篇文件拖拽(文件拖放)》来介绍如何为程序添加文件拖拽(文件拖放)操作,文件拖拽(文件拖放)操作可以归类到Windows程序的界面操作,因此也将这篇文章放到Windows界面编程系列中。
    文件拖拽(文件拖放)功能能有效提高用户体验,在VC++中要让程序支持文件拖拽功能,主要使用三个函数??DragAcceptFilesDragQueryFileDragFinish。下面先来介绍这三个函数(为了更加好学习英语,函数介绍尽可能会使用英语)。

一.DragAcceptFiles
函数功能:Registers whether a window accepts dropped files. An application that calls DragAcceptFiles with the fAccept parameter set to TRUE has identified itself as able to process the WM_DROPFILES message from File Manager.
函数原型:
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
VOIDDragAcceptFiles(
HWNDhWnd,
BOOL fAccept
);
参数说明:
第一个参数hWnd
The identifier of the window that is registering whether it will accept dropped files.
第二个参数fAccept
A value that indicates if the window identified by the hWnd parameter accepts dropped files. This value is TRUE to accept dropped files or FALSE to discontinue accepting dropped files.
对于对话框程序,还可以通过选择其Properties->Extended Styles,点选Accept files选项即可。
一.DragQueryFile
函数功能:Retrieves the names of dropped files that result from a successful drag-and-drop operation.
函数原型:
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
UINTDragQueryFile(
HDROPhDrop,
UINT iFile,
LPTSTRlpszFile,
UINTcch
);
参数说明:
第一个参数hDrop

HDROP标识符,即响应函数中的wParam参数
第二个参数iFile:
待查询的文件索引号,从0开始。可以同时拖拽多个文件,因此就需要一个索引号来进行区分。如果该参数为0xFFFFFFFF,则该函数返回拖拽的文件的个数
第三个参数lpszFile
用于存放文件名的缓冲区首地址
第四个参数cch

缓冲区长度
函数返回值:若iFile0xFFFFFFFF返回拖拽的文件个数,否则返回相应索引号的文件名长度。


第三个DragFinish
函数功能:Releases memory that the system allocated for use in transferring file names to the application.
函数原型:
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
VOIDDragFinish(HDROPhDrop);
  1. // 文件拖拽<a href="http://blog.csdn.net/morewindows/article/details/8634451">http://blog.csdn.net/morewindows/article/details/8634451</a>
  2. //By MoreWindows-(http://blog.csdn.net/MoreWindows)
  3. // 第一步 #include <shellapi.h> #pragma comment(lib, "shell32.lib")
  4. // 第二步 DragAcceptFiles(hwnd, TRUE);
  5. // 第三步 UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);拖曳文件个数
  6. // 第四步 DragQueryFile(hDrop, i, strFileName, MAX_PATH);获得拖曳的文件名
  7. #include "stdafx.h"
  8. #include <vector>
  9. #include <cstring>
  10. using namespace std;
  11. // 文件拖拽第一步  
  12. #include <shellapi.h>
  13. #pragma comment(lib, "shell32.lib")
  14. const char szAppName[] = "文件拖拽_MoreWindows(http://blog.csdn.net/MoreWindows)";
  15. BOOL InitApplication(HINSTANCE hinstance, int nCmdShow);
  16. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  17. int APIENTRY WinMain(HINSTANCE hInstance,
  18.                      HINSTANCE hPrevInstance,
  19.                      LPSTR     lpCmdLine,
  20.                      int       nCmdShow)
  21. {
  22.      // TODO: Place code here.
  23.     MSG     msg;
  24.    
  25.     if (!InitApplication(hInstance, nCmdShow))
  26.     {
  27.         return 0;
  28.     }
  29.    
  30.     while (GetMessage(&msg, NULL, 0, 0))
  31.     {
  32.         TranslateMessage(&msg);
  33.         DispatchMessage(&msg);
  34.     }
  35.     return msg.wParam;
  36.     return 0;
  37. }
  38. BOOL InitApplication(HINSTANCE hinstance, int nCmdShow)
  39. {
  40.     HWND      hwnd;
  41.     WNDCLASS  wndclass;
  42.    
  43.    
  44.     wndclass.style       = CS_HREDRAW | CS_VREDRAW;
  45.     wndclass.lpfnWndProc = WndProc;
  46.     wndclass.cbClsExtra  = 0;
  47.     wndclass.cbWndExtra  = 0;
  48.     wndclass.hInstance   = 0;
  49.     wndclass.hIcon       = LoadIcon(NULL, IDI_APPLICATION);
  50.     wndclass.hCursor     = LoadCursor(NULL, IDC_ARROW);
  51.     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  52.     wndclass.lpszMenuName  = NULL;
  53.     wndclass.lpszClassName = szAppName;
  54.    
  55.     if (!RegisterClass(&wndclass))
  56.     {
  57.         MessageBox(NULL, "Program Need Windows NT!", szAppName, MB_ICONERROR);
  58.         return FALSE;
  59.     }
  60.    
  61.     hwnd = CreateWindow(szAppName,
  62.         szAppName,
  63.         WS_OVERLAPPEDWINDOW,
  64.         CW_USEDEFAULT,
  65.         CW_USEDEFAULT,
  66.         CW_USEDEFAULT,
  67.         CW_USEDEFAULT,
  68.         NULL,
  69.         NULL,
  70.         hinstance,
  71.         NULL);
  72.    
  73.     if (hwnd == NULL)
  74.         return FALSE;
  75.    
  76.     ShowWindow(hwnd, nCmdShow);
  77.     UpdateWindow(hwnd);
  78.    
  79.     return TRUE;
  80. }
  81. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83.     static vector<string> s_vetFileNames;
  84.     switch (message)
  85.     {
  86.     case WM_CREATE:
  87.         // 文件拖拽第二步   DragAcceptFiles
  88.         DragAcceptFiles(hwnd, TRUE);
  89.         return 0;
  90.         // 文件拖拽第三步 DragQueryFile and DragQueryFile
  91.     case WM_DROPFILES:
  92.         {
  93.             HDROP hDrop = (HDROP)wParam;
  94.             UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); // 拖拽文件个数
  95.             char strFileName[MAX_PATH];
  96.             for (int i = 0; i < nFileNum; i++)  
  97.             {
  98.                 DragQueryFile(hDrop, i, strFileName, MAX_PATH);//获得拖曳的文件名
  99.                 s_vetFileNames.push_back(strFileName);
  100.                
  101.             }
  102.             DragFinish(hDrop);      //释放hDrop
  103.    
  104.             InvalidateRect(hwnd, NULL, TRUE);
  105.         }
  106.         return 0;
  107.     case WM_PAINT:
  108.         {
  109.             HDC             hdc;
  110.             PAINTSTRUCT     ps;
  111.             vector<string>::iterator pos;
  112.             int i, y;
  113.             hdc = BeginPaint(hwnd, &ps);
  114.         
  115.             // 显示拖拽的文件名
  116.             y = 0;
  117.             for (pos = s_vetFileNames.begin(); pos != s_vetFileNames.end(); pos++)
  118.             {
  119.                 TextOut(hdc, 20, y, pos->c_str(), strlen(pos->c_str()));
  120.                 y += 30;
  121.             }
  122.             EndPaint(hwnd, &ps);
  123.         }
  124.         return 0;
  125.     case WM_DESTROY:
  126.         PostQuitMessage(0);
  127.         return 0;
  128.     }
  129.     return DefWindowProc(hwnd, message, wParam, lParam);
  130. }
复制代码
运行结果截图如下,在桌面上选取了几个快捷方式然后拖到程序窗口中:
[attachment=1127]



通过本文,可以看出要为程序添加文件拖拽(文件拖放)功能,只要四步即可。
第一步  
#include <shellapi.h> #pragma comment(lib, "shell32.lib")
第二步
DragAcceptFiles(hwnd, TRUE);
第三步
UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);拖曳文件个数
第四步
DragQueryFile(hDrop, i, strFileName, MAX_PATH);获得拖曳的文件名




转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8634451


回复下载源代码!
[attachment=1128]

1

主题

23

回帖

0

积分

新手上路

积分
0
发表于 2013-10-15 09:17:07 | 显示全部楼层
:                 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 05:54 , Processed in 0.142178 second(s), 21 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表