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

简介: 本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5128654转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8634451欢迎关注微博:http://weibo.

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5128654

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

欢迎关注微博:http://weibo.com/MoreWindows

 

前面已经有六篇文章介绍了Windows界面编程,目录如下:

1. Windows界面编程第一篇位图背景与位图画刷

2. Windows界面编程第二篇半透明窗体

3. Windows界面编程第三篇异形窗体普通版

4. Windows界面编程第四篇异形窗体高富帅版

5. Windows界面编程第五篇静态控件背景透明化

6.Windows界面编程第六篇动画启动效果(动画效果显示及隐藏窗口)

 

本篇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); 

 

下面是示例程序代码,代码中有详细注释。(下载地址:http://download.csdn.net/detail/morewindows/5128654

[cpp]  view plain copy
  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. // 文件拖拽第一步    
  13. #include <shellapi.h>  
  14. #pragma comment(lib, "shell32.lib")  
  15.   
  16. const char szAppName[] = "文件拖拽_MoreWindows(http://blog.csdn.net/MoreWindows)";  
  17.   
  18. BOOL InitApplication(HINSTANCE hinstance, int nCmdShow);  
  19. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);  
  20.   
  21.   
  22. int APIENTRY WinMain(HINSTANCE hInstance,  
  23.                      HINSTANCE hPrevInstance,  
  24.                      LPSTR     lpCmdLine,  
  25.                      int       nCmdShow)  
  26. {  
  27.     // TODO: Place code here.  
  28.     MSG     msg;  
  29.       
  30.     if (!InitApplication(hInstance, nCmdShow))  
  31.     {  
  32.         return 0;  
  33.     }  
  34.       
  35.     while (GetMessage(&msg, NULL, 0, 0))  
  36.     {  
  37.         TranslateMessage(&msg);  
  38.         DispatchMessage(&msg);  
  39.     }  
  40.     return msg.wParam;  
  41.     return 0;  
  42. }  
  43.   
  44.   
  45. BOOL InitApplication(HINSTANCE hinstance, int nCmdShow)  
  46. {  
  47.     HWND      hwnd;  
  48.     WNDCLASS  wndclass;  
  49.       
  50.       
  51.     wndclass.style       = CS_HREDRAW | CS_VREDRAW;  
  52.     wndclass.lpfnWndProc = WndProc;  
  53.     wndclass.cbClsExtra  = 0;  
  54.     wndclass.cbWndExtra  = 0;  
  55.     wndclass.hInstance   = 0;  
  56.     wndclass.hIcon       = LoadIcon(NULL, IDI_APPLICATION);  
  57.     wndclass.hCursor     = LoadCursor(NULL, IDC_ARROW);  
  58.     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  
  59.     wndclass.lpszMenuName  = NULL;  
  60.     wndclass.lpszClassName = szAppName;  
  61.       
  62.     if (!RegisterClass(&wndclass))  
  63.     {  
  64.         MessageBox(NULL, "Program Need Windows NT!", szAppName, MB_ICONERROR);  
  65.         return FALSE;  
  66.     }  
  67.       
  68.     hwnd = CreateWindow(szAppName,   
  69.         szAppName,  
  70.         WS_OVERLAPPEDWINDOW,  
  71.         CW_USEDEFAULT,  
  72.         CW_USEDEFAULT,  
  73.         CW_USEDEFAULT,  
  74.         CW_USEDEFAULT,  
  75.         NULL,   
  76.         NULL,  
  77.         hinstance,  
  78.         NULL);  
  79.       
  80.     if (hwnd == NULL)  
  81.         return FALSE;  
  82.       
  83.     ShowWindow(hwnd, nCmdShow);  
  84.     UpdateWindow(hwnd);  
  85.       
  86.     return TRUE;  
  87. }  
  88.   
  89.   
  90. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
  91. {  
  92.     static vector<string> s_vetFileNames;  
  93.   
  94.   
  95.     switch (message)  
  96.     {  
  97.     case WM_CREATE:  
  98.         // 文件拖拽第二步   DragAcceptFiles  
  99.         DragAcceptFiles(hwnd, TRUE);  
  100.         return 0;  
  101.   
  102.   
  103.         // 文件拖拽第三步 DragQueryFile and DragQueryFile  
  104.     case WM_DROPFILES:  
  105.         {  
  106.             HDROP hDrop = (HDROP)wParam;  
  107.             UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); // 拖拽文件个数  
  108.             char strFileName[MAX_PATH];  
  109.             for (int i = 0; i < nFileNum; i++)    
  110.             {  
  111.                 DragQueryFile(hDrop, i, strFileName, MAX_PATH);//获得拖曳的文件名  
  112.                 s_vetFileNames.push_back(strFileName);  
  113.                   
  114.             }  
  115.             DragFinish(hDrop);      //释放hDrop  
  116.       
  117.             InvalidateRect(hwnd, NULL, TRUE);  
  118.         }  
  119.         return 0;  
  120.   
  121.   
  122.     case WM_PAINT:  
  123.         {  
  124.             HDC             hdc;  
  125.             PAINTSTRUCT     ps;  
  126.             vector<string>::iterator pos;  
  127.             int i, y;  
  128.             hdc = BeginPaint(hwnd, &ps);  
  129.           
  130.             // 显示拖拽的文件名  
  131.             y = 0;  
  132.             for (pos = s_vetFileNames.begin(); pos != s_vetFileNames.end(); pos++)  
  133.             {  
  134.                 TextOut(hdc, 20, y, pos->c_str(), strlen(pos->c_str()));  
  135.                 y += 30;  
  136.             }  
  137.             EndPaint(hwnd, &ps);  
  138.         }  
  139.         return 0;  
  140.   
  141.     case WM_DESTROY:  
  142.         PostQuitMessage(0);  
  143.         return 0;  
  144.     }  
  145.     return DefWindowProc(hwnd, message, wParam, lParam);  
  146. }  


运行结果截图如下,在桌面上选取了几个快捷方式然后拖到程序窗口中:

 

 

通过本文,可以看出要为程序添加文件拖拽(文件拖放)功能,只要四步即可。

第一步  

#include <shellapi.h> #pragma comment(lib, "shell32.lib")

第二步

DragAcceptFiles(hwnd, TRUE);

第三步

UINT nFileNum = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);拖曳文件个数

第四步

DragQueryFile(hDrop, i, strFileName, MAX_PATH);获得拖曳的文件名

 

欢迎继续参考Windows界面编程第八篇 listbox彩色显示隔行变色

地址是http://blog.csdn.net/morewindows/article/details/8656061

 

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5128654

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


目录
相关文章
|
10天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
存储 安全 数据安全/隐私保护
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
249 0
|
26天前
|
开发框架 数据安全/隐私保护 开发者
HBuilder开发者必备!Windows上传IPA文件的软件分享
HBuilder开发者必备!Windows上传IPA文件的软件分享
21 1
|
2月前
|
存储 安全 Shell
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
65 0
|
2月前
|
安全 Shell 网络安全
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
36 0
|
2月前
|
安全 算法 网络安全
windows 系统 c 盘 .ssh 文件夹里的 id_rsa.pub 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 id_rsa.pub 文件的作用
39 0
|
2天前
|
Windows
解决windows下Qt Creator显示界面过大的问题
解决windows下Qt Creator显示界面过大的问题
|
6天前
|
Windows
【Windows】 手写脚本更快编辑hosts文件
【Windows】 手写脚本更快编辑hosts文件
9 0
|
1月前
|
Windows
Windows 命令提示符(CMD)操作(一):文件和目录管理
Windows 命令提示符(CMD)操作(一):文件和目录管理
40 0