关于dde入门的一个简单例子(50分)

  • 主题发起人 主题发起人 晨空
  • 开始时间 开始时间

晨空

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了个dde的程序,客气端‘发送宏’的代码如下,
procedure TForm2.Button2Click(Sender: TObject);
VAR str:string;
begin
ddeclientconv1.SetLink(edit2.Text,edit3.Text);
if ddeclientconv1.OpenLink=true then
begin
ddeclientconv1.ServiceApplication:=edit4.Text;
if (ddeclientconv1.ExecuteMacro(PCHAR('SHOW1'),false)=false) then
showmessage('wrong');
ddeclientconv1.CloseLink;
end
else showmessage('failure');
end;

为什么执行了这个过程后
服务器端执行宏程序并没有执行?下面这段代码程序根本就没有执行,这是为什么,哪里做
得不对吗?高手请教
procedure TForm1.DdeServerConv1ExecuteMacro(Sender: TObject;
Msg: TStrings);
VAR Macro:string;
begin
if Msg.Count<>0 then
begin
macro:=msg.Strings[0];
if macro='show1' then
image1.Picture.LoadFromFile('1.bmp')
else if macro='show2' then
image1.Picture.LoadFromFile('2.bmp')
else showmessage('not support the command');
end;
end;
 
看看例子吧,你可能没有连接客户和服务:

在Win95中用DDE和程序管理器交互
Question:Is there a special trick to getting DDE to establish a link with program Manger using DDE, when uisng Win95 (V1.0 of delphi)?

Answer:I've been using the following unit to manage program groups using dde to progman. It's an adaption of Steve Texeira's(sp) code in Dephi Developers Guide.I have had success in 3.1 and '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
{ Program Manager DDE macro strings }
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('Failed to establish DDE Link');
end;

Procedure TProgMan.CreateGroup(GroupName: String;ShowGroup:Boolean);
Begin
{ Delete the group if it exists }
ExecMacroString(Format(SDDEDeleteGroup, [GroupName]));
If NOT ExecMacroString(
Format(SDDECreateGroup,[GroupName]))
then raise EProgManError.Create(
'Could not create group ' + GroupName);
If ShowGroup then
If not ExecMacroString(
Format(SDDEShowGroup, [GroupName])) then
raise EProgManError.Create(
'Could not show group ' + GroupName);
End;

Procedure TProgMan.DeleteGroup(GroupName: String);
Begin
if NOT ExecMacroString(Format(
SDDEDeleteGroup, [GroupName])) then
raise EProgManError.Create(
'Could not delete group ' + GroupName);
End;

Procedure TProgMan.DeleteItem(ItemName: String);
Begin
if NOT ExecMacroString(
Format(SDDEDeleteGroup, [ItemName])) then
raise EProgManError.Create(
'Could not delete item ' + 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
raise EProgManError.Create(
'Could not add item ' + ItemName);
finally
FreeMem(P, PSize);
end;
End;
end.

 

Similar threads

I
回复
0
查看
560
import
I
I
回复
0
查看
550
import
I
I
回复
0
查看
487
import
I
I
回复
0
查看
775
import
I
后退
顶部