{"id":426,"date":"2009-05-27T13:27:00","date_gmt":"2009-05-27T05:27:00","guid":{"rendered":""},"modified":"2013-11-17T17:35:40","modified_gmt":"2013-11-17T09:35:40","slug":"delphi%e7%9b%91%e8%a7%86u%e7%9b%98%e7%8a%b6%e6%80%81","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/426.html","title":{"rendered":"Delphi\u76d1\u89c6U\u76d8\u72b6\u6001"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nunit Unit1;\r\n\r\ninterface\r\n\r\nuses\r\n  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,\r\n  Dialogs, StdCtrls;\r\n\r\ntype\r\n  TForm1 = class(TForm)\r\n    Button1: TButton;\r\n    Button2: TButton;\r\n    Label1: TLabel;\r\n    procedure Button1Click(Sender: TObject);\r\n    procedure Button2Click(Sender: TObject);\r\n  private\r\n    { Private declarations }\r\n  public\r\n   procedure WMDEVICECHANGE(var msgx:Tmessage);message WM_DEVICECHANGE;\r\n  end;\r\n\r\nvar\r\n  Form1: TForm1;\r\n\r\nimplementation\r\nuses shlobj,Activex,ComObj,Registry;\r\n\r\n{$R *.dfm}\r\n\r\nfunction GetLastMoveDrive:String;\r\nvar\r\n  Drivetype:Integer;\r\n  root:String;\r\n  i:Integer;\r\nbegin\r\n  Result:='';\r\n  for i:=67 to 91 do \/\/\u4eceC\uff1a\u76d8\u5f00\u59cb\u5230Z\uff1a\u76d8\r\n  begin\r\n    root:=char(i)+':\\';\r\n    Drivetype:=GetDriveType(pChar(root));\r\n    if Drivetype=DRIVE_REMOVABLE then\r\n    begin\r\n      Result:='\u63d2\u5165U\u76d8 '+root;\r\n      Break;\r\n    end;\r\n  end;\r\nend;\r\n\r\nprocedure TFORM1.WMDEVICECHANGE(var msgx: Tmessage);\r\nconst\r\n  DBT_DEVICEARRIVAL=$8000;\r\n  DBT_DEVICEREMOVECOMPLETE=$8004;\r\nbegin\r\ninherited;\r\ncase msgx.WParam of\r\n  DBT_DEVICEARRIVAL:\r\n    Label1.Caption:=GetLastMoveDrive;\r\n  DBT_DEVICEREMOVECOMPLETE:\r\n    Label1.Caption:='U\u76d8\u88ab\u53d6\u8d70';\r\nend;\r\nend;\r\n\r\nprocedure TForm1.Button2Click(Sender: TObject);\r\nvar\r\n  reg:tregistry;\r\nbegin\r\nTry\r\n  reg:=tregistry.Create;\r\n  reg.rootkey:=HKEY_LOCAL_MACHINE;\r\n  reg.DeleteKey('\\SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies'); \/\/\u5220\u9664\u6ce8\u518c\u8868\u9879\r\n  reg.CloseKey;\r\n  reg.Free;\r\n  MessageBox(Handle,'\u89e3\u9664\u7981\u6b62U\u76d8\u62f7\u5165\u529f\u80fd','\u63d0\u793a',MB_OK);\r\nExcept\r\n  MessageBox(Handle,'\u8bbe\u7f6e\u5931\u8d25','\u63d0\u793a',MB_OK);\r\nend;\r\nend;\r\n\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nvar\r\n  reg:TRegistry;\r\nbegin\r\nTry\r\n  reg:=tregistry.create;\r\n  reg.rootkey:=HKEY_LOCAL_MACHINE;\r\n  reg.openkey('\\SYSTEM\\CurrentControlSet\\Control',true);\r\n  if reg.CreateKey('\\SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies') then\r\n  begin\r\n    reg.openkey('\\SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies',true);\r\n    reg.WriteInteger('WriteProtect',1); \/\/\u6dfb\u52a0\u6ce8\u518c\u8868\u9879\u548c\u5efa\u503c\r\n  end;\r\n  reg.CloseKey;\r\n  reg.Free;\r\n  MessageBox(Handle,'\u7981\u6b62U\u76d8\u62f7\u5165\u529f\u80fd','\u63d0\u793a',MB_OK);\r\nExcept\r\n  MessageBox(Handle,'\u8bbe\u7f6e\u5931\u8d25','\u63d0\u793a',MB_OK);\r\nend;\r\nend;\r\n\r\nend.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>unit Unit1; interface uses Windows, Messages, SysUtils, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=426"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":4754,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/426\/revisions\/4754"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}