您现在的位置:首页 >> 网络通讯 >> 网络通讯 >> 内容

用Delphi实现关闭重启本地网络连接

时间:2011/9/3 15:13:56 点击:

  核心提示:unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,...

unit Unit1;

interface

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

const
  connVerb = '启用';
  discVerb = '停用';

type
  TForm1 = class(TForm)
    shl1: TShell;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
    function ControlEthernet(const EthName, FolderItemVerbsName: string): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{
Component -> Import ActiveX Control -> Microsoft Shell Contolrs And  Automation
然后创建单元文件,之后再新建一个安装包,添加该单元文件,最后是安装该控件,就可以用了(Tshell)
}
procedure TForm1.btn1Click(Sender: TObject);
begin
  ControlEthernet('本地连接', discVerb); //启用本地连接 '停用&'
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  ControlEthernet('本地连接', connVerb); //启用本地连接 '启用&'
end;

function TForm1.ControlEthernet(const EthName,
  FolderItemVerbsName: string): Boolean;
var cpFolder, nwFolder: Folder; //一个外壳文件夹对象
  nVerbs: FolderItemVerbs; //获得上下文相关的菜单信息
  i, j, k: integer;
  Shell1: TShell;
begin
  Result := false;
  Shell1 := TShell.Create(Application);
  cpFolder := Shell1.NameSpace(3); //选择控件面板
  if cpFolder <> nil then
  begin
    for i := 0 to cpFolder.items.Count - 1 do //返回它所包含的外壳对象的集合(文件) 28
    begin
      if cpFolder.Items.Item(i).Name = '网络连接' then //返回的集合的名称
      begin
        nwFolder := cpFolder.items.item(i).GetFolder as Folder; //取得该cpFolder下面的外壳对象
        if nwFolder <> nil then //内容不为空
        begin
          for j := 0 to nwFolder.items.Count - 1 do //历遍cpFolder下面的外壳对象
          begin
            if nwFolder.Items.Item(j).Name = EthName then //若果为'本地连接'
            begin
              nVerbs := nwFolder.Items.Item(j).Verbs; //取得该对象的上下文菜单信息
              for k := 0 to nVerbs.Count - 1 do //历遍所有菜单信息
              begin
                if Pos(FolderItemVerbsName, nVerbs.Item(k).Name) > 0 then //如果菜单名称为 '禁用&' 时,
                                                               //  则执行该菜单命令
                begin
                  nVerbs.Item(k).DoIt; //执行该菜单命令
                  //nwFolder.Items.Item(j).InvokeVerb(nwFolder.Items.Item(j).Verbs.Item(k).Name);
                  Result := true; //效果一致
                end;
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;

end.

作者:jmlei 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2022 版权所有 All Rights Reserved.
  • 沪ICP备05001939号