📄 unit_main.pas
字号:
Begin
Result := Nil;
hasnode := False;
For I := 0 To TreeView1.Items.Count - 1 Do
Begin
If (Treeview1.Items[I].Level = 0) And
(Sametext(Treeview1.Items[I].Text, aname)) Then
Begin
Result := Treeview1.Items[I];
hasnode := True;
Break;
End;
End;
If not hasnode Then
Begin
Result := TreeView1.Items.Add(Nil, aname);
End;
End;
function TForm1.EnumAddDevices(ShowHidden: Boolean; DevInfo: hDevInfo; ahd:DWORD): Boolean;
var
i, Status, Problem: DWord;
pszText: PChar;
DeviceInfoData: TSPDevInfoData;
iImage: Integer;
P:PSPDevInfoData;
guid: String;
atype: String;
anode, aparent: TTreeNode;
begin
Treeview1.Items.BeginUpdate;
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Clean off all the items in a TreeView.
Treeview1.Items.Clear;
i := 0;
// Enumerate though all the devices.
while SetupDiEnumDeviceInfo(DevInfo, i, DeviceInfoData) do
begin
inc(i);
// Should we display this device, or move onto the next one.
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <>
CR_SUCCESS) then
begin
break;
end;
if (not (ShowHidden or not(Boolean(Status and DN_NO_SHOW_IN_DM) or
IsClassHidden(DeviceInfoData.ClassGuid)))) then
begin
break;
end;
GetMem(pszText, 256);
try
// Get a friendly name for the device.
ConstructDeviceName(DevInfo, DeviceInfoData, pszText, DWord(nil));
guid:= GUIDToString(DeviceInfoData.ClassGuid);
atype := GetDeviceClassName(DeviceInfoData.ClassGuid);
//If Not (SameText(atype, 'usb')) Then Continue;
// Try to get an icon index for this device.
if (GetClassImageIndex(DeviceInfoData.ClassGuid, @iImage)) then
begin
aparent := GetParentNode(atype);
aparent.ImageIndex := iImage;
aparent.Selectedindex := iImage;
New(P);
P^ := DeviceInfoData;
anode := Treeview1.Items.AddChildObject(aparent, pszText, Tobject(P));
anode.ImageIndex := iImage;
anode.SelectedIndex := iImage;
If DeviceInfoData.DevInst = ahandle Then
anode.Selected := True;
if (Problem = CM_PROB_DISABLED) then // red (X)
begin
anode.OverlayIndex := IDI_DISABLED_OVL - IDI_CLASSICON_OVERLAYFIRST;
end
else
begin
if (Boolean(Problem)) then // yellow (!)
begin
anode.OverlayIndex := IDI_PROBLEM_OVL - IDI_CLASSICON_OVERLAYFIRST;
end;
end;
if (Status and DN_NO_SHOW_IN_DM = DN_NO_SHOW_IN_DM) then // Greyed out
begin
anode.Cut := true;
end;
end;
finally
FreeMem(pszText);
end;
end;
Treeview1.Items.EndUpdate;
if Treeview1.Selected <> Nil Then
Begin
Treeview1.Selected.Focused := True;
Treeview1.Selected.MakeVisible;
End;
Result := true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := Caption +' @ '+ GetPCname;
if (not LoadSetupAPI) then
begin
ShowMessage('Could not load SetupAPI.dll');
exit;
end;
DevInfo := nil;
ShowHidden := false;
GetDevInfo;
// Add the devices to the TreeView window.
EnumAddDevices(ShowHidden, DevInfo, ahandle);
end;
procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
If Node.Data <> Nil Then
Begin
Dispose(PSPDevInfoData(Node.Data));
Node.Data := Nil;
End;
end;
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
N1.Enabled := False;
N2.Enabled := False;
N3.Enabled := False;
N6.Enabled := False;
If (Node <> Nil) And (Node.Data <> Nil) Then
Begin
N6.Enabled := True;
ahandle := PSPDevInfoData(Node.Data)^.DevInst;
if (IsDisabled(ahandle, DevInfo)) then
begin
N1.enabled := true
end
else
begin
if (CheckStatus(ahandle, DevInfo, DN_DISABLEABLE)) then
Begin
N2.enabled := true
End;
end;
if (CheckStatus(ahandle, DevInfo, DN_REMOVABLE)) then
begin
N3.enabled := true;
end;
End;
end;
procedure TForm1.WMDeviceChange(var Msg: TMessage);
const
DBT_DEVNODES_CHANGED = $0007;
begin
Panel1.Caption := 'Please Waite ... ';
Panel1.Visible := True;
Panel1.Update;
try
case Msg.wParam of
DBT_DEVNODES_CHANGED:
begin
GetDevInfo;
EnumAddDevices(ShowHidden, DevInfo, ahandle);
end;
end;
finally
Panel1.Visible := False;
end;
end;
procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Var
anode: TTreenode;
P: Tpoint;
begin
If button = mbright Then
Begin
anode := Treeview1.GetNodeAt(x,y);
anode.Selected := True;
anode.Focused := True;
Getcursorpos(P);
PopupMenu1.Popup(P.X, P.Y);
End;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
if (MessageBox(Handle, 'Enable this Device?', 'Change Device Status',
MB_OKCANCEL) = IDOK) then
begin
try
Panel1.Caption := 'Change Device Status ...';
Panel1.Visible := True;
panel1.Update;
if (StateChange(DICS_ENABLE, PSPDevInfoData(Treeview1.Selected.Data)^, DevInfo)) then
//EnumAddDevices(ShowHidden, DevInfo, ahandle);
finally
Panel1.Visible := False;
end;
end;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
if (MessageBox(Handle, 'Disable this Device?', 'Change Device Status', MB_OKCANCEL) = IDOK) then
begin
try
Panel1.Caption := 'Change Device Status ...';
Panel1.Visible := True;
panel1.Update;
if (StateChange(DICS_DISABLE, PSPDevInfoData(Treeview1.Selected.Data)^, DevInfo)) then
//EnumAddDevices(ShowHidden, DevInfo, ahandle);
finally
Panel1.Visible := False;
end;
end;
end;
procedure TForm1.N3Click(Sender: TObject);
var
DeviceInfoData: TSPDevInfoData;
Status, Problem: DWord;
VetoType: TPNPVetoType;
VetoName: array[0..256] of Char;
begin
if (MessageBox(Handle, 'Eject this Device?', 'Eject Device', MB_OKCANCEL) = IDOK) then
begin
try
Panel1.Caption := 'Eject Device ...';
Panel1.Visible := True;
panel1.Update;
DeviceInfoData := PSPDevInfoData(Treeview1.Selected.Data)^;
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
exit;
end;
VetoName[0] := #0;
case CM_Request_Device_Eject(DeviceInfoData.DevInst, VetoType, @VetoName, SizeOf(VetoName), 0) of
CR_SUCCESS:
begin
//EnumAddDevices(ShowHidden, DevInfo, ahandle);
end;
CR_REMOVE_VETOED:
begin
MessageBox(Handle, PChar('Failed to eject the Device (Veto: ' + VetoName + ')'), 'Vetoed', MB_OK);
end;
else
begin
MessageBox(Handle, PChar('Failed to eject the Device (' + SysErrorMessage(GetLastError) + ')'), 'Failure', MB_OK);
end;
end;
finally
Panel1.Visible := False;
end;
end;
end;
procedure TForm1.N5Click(Sender: TObject);
begin
EnumAddDevices(ShowHidden, DevInfo, ahandle);
end;
procedure TForm1.N6Click(Sender: TObject);
Var
DeviceInfoData: TSPDevInfoData;
S: String;
begin
DeviceInfoData := PSPDevInfoData(Treeview1.Selected.Data)^;
S := Format('GUID %s',[GUIDToString(DeviceInfoData.ClassGuid)]);
Application.MessageBox(Pchar(S), 'Information', 64);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SetupDiDestroyDeviceInfoList(DevInfo);
SetupDiDestroyClassImageList(ClassImageListData);
UnloadSetupApi;
end;
procedure TForm1.N8Click(Sender: TObject);
begin
N8.Checked := Not N8.Checked;
ShowHidden := N8.Checked;
EnumAddDevices(ShowHidden, DevInfo, ahandle);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -