zoukankan      html  css  js  c++  java
  • Delphi 自动检测U盘插入、拔出及获取U盘盘符!

    Delphi 自动检测U盘插入、拔出及获取U盘盘符!

    u盘的 插入和删除windows会给所有的程序发出WM_DEVICECHANGE 信息
    //这句放在private里面
    procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;

    procedure TForm1.WMDeviceChange (var Msg: TMessage);
    var
       myMsg : String;
    begin
       Case Msg.WParam of
       32768:
         begin
           myMsg :='U盘插入';
           Label1.Caption:=myMsg
         end;
       32772:
         begin
           myMsg :='U盘拔出';
           Label1.Caption:=myMsg;
         end;
       end;
    end;

    u盘盘符判断

    procedure TForm1.Button1Click(Sender: TObject);
    var
    buf:array [0..max_path-1] of char;
    m_result:integer;
    i:integer;
    str_temp:string;
    begin
    m_result:=getlogicaldrivestrings(max_path,buf);
    for i:=0 to (m_result div 4) do
        begin
          str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
          if getdrivetype(pchar(str_temp)) = drive_removable then
            begin
              showmessage(str_temp+'盘为u盘') ;
              listbox1.items.add(str_temp) ;
            end;
    end;


    //完整源码
    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type
      TForm1 = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        ListBox1: TListBox;

        procedure Button1Click(Sender: TObject);

      private
        { Private declarations }
      //u盘的 插入和删除windows会给所有的程序发出WM_DEVICECHANGE 信息
      procedure WMDeviceChange(var Msg: TMessage);  message WM_DEVICECHANGE;
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    // u盘的 插入和删除windows会给所有的程序发出WM_DEVICECHANGE 信息
    procedure TForm1.WMDeviceChange (var Msg: TMessage);
    var
       myMsg : String;
    begin
       Case Msg.WParam of
       32768:
         begin
           myMsg :='U盘插入';
           Label1.Caption:=myMsg
         end;
       32772:
         begin
           myMsg :='U盘拔出';
           Label1.Caption:=myMsg;
         end;
       end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
    buf:array [0..max_path-1] of char;
    m_result:integer;
    i:integer;
    str_temp:string;
    begin
    m_result:=getlogicaldrivestrings(max_path,buf);
    for i:=0 to (m_result div 4) do
        begin
          str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
          if getdrivetype(pchar(str_temp)) = drive_removable then
            begin
              showmessage(str_temp+'盘为u盘') ;
              listbox1.items.add(str_temp) ;
            end;
    end;
    end;

    end.


    end;

  • 相关阅读:
    vue禁止用户复制文案
    html2canvas.js + jspdf.js 实现html转pdf / html转图片
    Vue.js +pdf.js 处理响应pdf文件流数据,前端转图片预览不可下载
    JavaScript处理后端返回PDF文件流,在线预览下载PDF文件
    多线程并发工具类01-CountDownLatch 线程工具类
    线程池01-线程池基础知识
    网络基础知识01-协议分层与TCP/IP协议簇
    网络基础知识02-HTTP协议
    jquery-i18n 多语言切换
    springboot-01 springboot 启动 enviroment环境加载
  • 原文地址:https://www.cnblogs.com/zyb2016/p/5685232.html
Copyright © 2011-2022 走看看