win32 - WM_DROPFILES的用法

WM_DROPFILES : 当用户将文件拖放到已注册为丢弃文件的接收者的应用程序窗口中时发送该消息

我们可以利用这个消息获取文件名称,并将它们保存到容器里。

LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    if (uMsg == WM_DROPFILES)
    {
       HDROP hDrop = reinterpret_cast<HDROP>(wParam);

       // extract files here
       vector<string> files;
       char filename[MAX_PATH];

       UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
       for(UINT i = 0; i < count; ++i)
       {
          if (DragQueryFileA(hDrop, i, filename, MAX_PATH))
              files.push_back(filename);
       }

       DragFinish(hDrop);
       return 0;
    }

    return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

或者,

LRESULT CALLBACK StaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    if (uMsg == WM_DROPFILES)
    {
       HDROP hDrop = reinterpret_cast<HDROP>(wParam);

       // extract files here
       vector<string> files;
       string filename;

       UINT count = DragQueryFileA(hDrop, -1, NULL, 0);
       for(UINT i = 0; i < count; ++i)
       {
          UINT size = DragQueryFileA(hDrop, i, NULL, 0);
          if (size > 0)
          {
              filename.resize(size);
              DragQueryFileA(hDrop, i, &filename[0], size+1);
              files.push_back(filename);
          }
       }

       DragFinish(hDrop);
       return 0;
    }

    return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

 

win32 - WM_DROPFILES的用法

上一篇:C#类型检查和强制转换


下一篇:CKEditor粘贴图片自动上传到服务器(C#版)