簡體   English   中英

如何在 WM_DROPFILES 期間根據文件擴展名更改 cursor

[英]How can I change the cursor depending on the file extension during WM_DROPFILES

WM_DROPFILES似乎很容易處理文件拖放。 但它總是在懸停開始時將 cursor 更改為crDrag WM_DROPFILES處理程序僅由 drop 事件激活。 如果用戶想要將文件拖到具有不受支持的擴展名的表單上,則會出現問題。

如何在拖動過程中根據文件擴展名更改 cursor? 如果沒有文件具有可接受的擴展名,則應設置為crNoDrop

我寫了這個過濾 function ,它工作正常:

function isAcceptableFileName(fileName_ : string; fileExts_ : array of string ) : boolean

我使用 Delphi 10.3。

您可以執行一個返回 drop 事件的過程

    procedure DragDropFile2Form(var Msg: TMessage); message WM_DROPFILES;

它的實現可能是這樣的。

procedure DragDropFile2Form(var Msg: TMessage);
var
  extension: string;
  number: Integer;
  path: array [0 .. MAX_COMPUTERNAME_LENGTH + MAX_PATH] of Char;
begin
    DragQueryFile(Msg.WParam, number, path, 275);
    {
      if the index value is between zero and the total number of dropped files,
      the return value is the required size, in characters.
    }
    if (FileExists(path)) then
    begin
      extension := ExtractFileExt(path);
      // Extracts the extension part of path like [.jpg, .png, .txt]
      if (extension = '.jpg') or (extension = '.png') or (extension = '.txt') then
      begin
        // ACCEPTED CODE TO DO WHAT YOU WANT WITH THE FILES
      end
      else 
      begin
        // BLOCKS THE FILE AND SHOWS A MESSAGE
        MessageBox(Form1.Handle, PChar('The file is not a image or text'),
          PChar('Drag & Drop'), MB_ICONWARNING);
      end;

    end;

  end;
  DragFinish(Msg.WParam);
  // This frees the resources used to store information about the drop.
end;

您還需要傳遞接收 WM_DROPFILES 消息的表單句柄

procedure FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Handle, True);
end;

不要忘記將 Winapi.ShellAPI 添加到您的使用中

您可以在此處找到更多詳細信息: http://swepc.se/blog/2018/08/29/how-to-drag-drop-files-delphi-10/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM