Советы по Delphi. Версия 1.4.3 от 1.1.2001
Шрифт:
Управление Program Manager в Win95 с помощью DDE
Delphi 1
Для управления программными группами в Program Manager с помощью DDE мною был использован следующий модуль. За основу был взят код Steve Texeira (sp) из руководства Dephi Developers Guide.
Работает под Win 3.1 и '95.
unit Pm;
interface
uses SysUtils, Classes, DdeMan;
type
EProgManError = class(Exception);
TProgMan = class(TComponent)
private
FDdeClientConv: TDdeClientConv;
procedure InitDDEConversation;
function ExecMacroString(Macro: String): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Procedure CreateGroup(GroupName: String; ShowGroup:Boolean);
procedure DeleteGroup(GroupName: String);
procedure DeleteItem(ItemName: String);
procedure AddItem(CmdLine, ItemName: String);
end;
implementation
uses Utils;
const
{ DDE-макростроки
для Program Manager }
SDDECreateGroup = '[CreateGroup(%s)]';
SDDEShowGroup = '[ShowGroup(%s, 1)]';
SDDEDeleteGroup = '[DeleteGroup(%s)]';
SDDEDeleteItem = '[DeleteItem(%s)]';
SDDEAddItem = '[AddItem(%s, "%s", %s)]';
constructor TProgMan.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
InitDDEConversation;
end;
destructor TProgMan.Destroy;
begin
if Assigned(FDDEClientConv) then FDdeClientConv.CloseLink;
inherited Destroy;
end;
function TProgMan.ExecMacroString(Macro: String): Boolean;
Begin
StringAsPchar(Macro);
Result := FDdeClientConv.ExecuteMacro(@Macro[1], False);
End;
Procedure TProgMan.InitDDEConversation;
begin
FDdeClientConv := TDdeClientConv.Create(Self);
If NOT FDdeClientConv.SetLink('PROGMAN', 'PROGMAN') then
raise EProgManError.Create('Не
могу установить DDE Link');
end;
Procedure TProgMan.CreateGroup(GroupName: String; ShowGroup:Boolean);
Begin
{ Удаляем группу, если она существует }ExecMacroString(Format(SDDEDeleteGroup, [GroupName]));
If NOT ExecMacroString(Format(SDDECreateGroup, [GroupName])) then
raise EProgManError.Create('Не могу создать группу ' + GroupName);
If ShowGroup then
If not ExecMacroString(Format(SDDEShowGroup, [GroupName])) then
raise EProgManError.Create('Не могу показать группу ' + GroupName);
End;
Procedure TProgMan.DeleteGroup(GroupName: String);
Begin
if NOT ExecMacroString(Format(SDDEDeleteGroup, [GroupName])) then
raise EProgManError.Create('Не могу удалить группу ' + GroupName);
End;
Procedure TProgMan.DeleteItem(ItemName: String);
Begin
if NOT ExecMacroString(Format(SDDEDeleteGroup, [ItemName])) then
raise EProgManError.Create('Не могу удалить элемент ' + ItemName);
End;
Procedure TProgMan.AddItem(CmdLine, ItemName: String);
Var
P: PChar;
PSize: Word;
Begin
PSize := StrLen(SDDEAddItem) + (Length(CmdLine) *2) + Length(ItemName) + 1;
GetMem(P, PSize);
try
StrFmt(P, SDDEAddItem, [CmdLine, ItemName, CmdLine]);
if NOT FDdeClientConv.ExecuteMacro(P, False) then
Поделиться с друзьями: