X
xj_zh
Unregistered / Unconfirmed
GUEST, unregistred user!
有两个窗体,分别定义了消息发送和接收函数。窗体一中创建窗体二,并发送消息给窗体二,窗体二接收到消息后进行处理,并将处理结果通
过消息发送给窗体一。问题是窗体二能接收到窗体一的消息,但窗体二接收不到窗体一的消息。
大概的代码如下:
窗体一:
unit Unit_bnzhcx;
interface
uses
...;
type
Tfrm_bnzhcx = class(TForm)
...
private
{ Private declarations }
procedure GetMsg(var Msg: TMessage); message WM_USER + 101; //在这里增加接收的过程
procedure SendUserMsg(var msg: string);
public
{ Public declarations }
end;
var
frm_bnzhcx: Tfrm_bnzhcx;
implementation
uses Unit_Condition;
{$R *.dfm}
procedure Tfrm_bnzhcx.GetMsg(var Msg: TMessage);
var
MapHandle: THandle;
pStr: Pointer;
Str: string;
i: integer;
begin
Memo1.Clear;
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 255, 'con1');
if (MapHandle > 0) then
begin
pStr := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 10); //得到共享内存的指针
Str := Pchar(pStr);
Str := Copy(Str, 2, Length(Str) - 1);
while Pos('$', Str) <> 0 do
begin
Memo1.Lines.Add(Copy(Str, 0, Pos('$', Str) - 1));
Str := Copy(Str, Pos('$', Str) + 1, length(Str) - Pos('$', Str));
end;
end;
CloseHandle(MapHandle);
end;
procedure Tfrm_bnzhcx.SendUserMsg(var msg: string);
var
Win: HWND;
Post: Pointer;
pStr: PShortString;
MapHandle: THandle;
begin
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 250, 'con1');
if (MapHandle > 0) then
begin
Post := MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0);
pStr := Post;
ZeroMemory(pStr, 250);
pStr^ := msg; //写共享内存
end;
Win := FindWindow('Tfrm_condition', nil);
if Win > 0 then
PostMessage(Win, WM_USER + 100, 0, 0); //发消息给接收程序的主窗口...
end;
procedure Tfrm_bnzhcx.AdvStrGrid1ClickCell(Sender: TObject; ARow,
ACol: Integer);
var ls_str: string;
begin
Cur_row := ARow; //记录党前行
if (ACol = 2) then
if (ARow <> 1) and (ARow <> 2) and (ARow <> 3) then
begin
//frm_condition := Tfrm_condition.Create(nil);
Application.CreateForm(Tfrm_condition, frm_condition);
case ARow of
1: ; //微机代码
2: ; //时间起
3: ; //时间止
4: ls_str := 'id_lsgx$name_dmmc$' + AdvStrGrid1.Cells[3, 4] + '$' + AdvStrGrid1.Cells[5, 4];
5: ls_str := 'id_zclx$name_zclx$' + AdvStrGrid1.Cells[3, 5] + '$' + AdvStrGrid1.Cells[5, 5];
6: ls_str := 'id_ysjc$name_ysjc$' + AdvStrGrid1.Cells[3, 6] + '$' + AdvStrGrid1.Cells[5, 6];
7: ls_str := 'id_gbhyml$name_gbhyml$' + AdvStrGrid1.Cells[3, 7] + '$' +AdvStrGrid1.Cells[5, 7];
8: ls_str := 'id_gbhy$name_gbhy$' + AdvStrGrid1.Cells[3, 8] + '$' + AdvStrGrid1.Cells[5, 8];
9: ls_str := 'id_jyhyml$name_jyhyml$' + AdvStrGrid1.Cells[3, 9] + '$' + AdvStrGrid1.Cells[5, 9];
10: ls_str := 'id_jyhy$name_dmmc$' + AdvStrGrid1.Cells[3, 10] + '$' + AdvStrGrid1.Cells[5, 10];
11: ls_str := 'id_dlwzsz$name_dmmc$' + AdvStrGrid1.Cells[3, 11] + '$' + AdvStrGrid1.Cells[5, 11];
end;
ls_str := ls_str + '$';
SendUserMsg(ls_str);
frm_condition.Show;
end;
end;
end.
××××××××××
窗体二:
unit Unit_Condition;
interface
uses
...;
type
Tfrm_condition = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
procedure GetMsg(var Msg: TMessage); message WM_USER + 100; //在这里增加接收的过程
procedure SendUserMsg(var msg: string);
public
{ Public declarations }
end;
var
frm_condition: Tfrm_condition;
implementation
uses Unit_DM1;
{$R *.dfm}
procedure Tfrm_condition.GetMsg(var Msg: TMessage);
var
MapHandle: THandle;
pStr: Pointer;
Str: string;
i: integer;
begin
Memo1.Clear;
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 255, 'con1');
if (MapHandle > 0) then
begin
pStr := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 10); //得到共享内存的指针
Str := Pchar(pStr);
Str := Copy(Str, 2, Length(Str) - 1);
while Pos('$', Str) <> 0 do
begin
Memo1.Lines.Add(Copy(Str, 0, Pos('$', Str) - 1));
Str := Copy(Str, Pos('$', Str) + 1, length(Str) - Pos('$', Str));
end;
end;
CloseHandle(MapHandle);
end;
procedure Tfrm_condition.SendUserMsg(var msg: string);
var
Win: HWND;
Post: Pointer;
pStr: PShortString;
MapHandle: THandle;
begin
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 250, 'result');
if (MapHandle > 0) then
begin
Post := MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0);
pStr := Post;
ZeroMemory(pStr, 250);
pStr^ := msg; //写共享内存
end;
Win := FindWindow('Tfrm_bnzhcx', nil);
if Win > 0 then
PostMessage(Win, WM_USER + 101, 0, 0); //发消息给接收程序的主窗口...
end;
procedure Tfrm_condition.cb_okClick(Sender: TObject);
begin
ls_str := 'fdsa$dsafds$adsf';
SendUserMsg(ls_str);
end;
end.
过消息发送给窗体一。问题是窗体二能接收到窗体一的消息,但窗体二接收不到窗体一的消息。
大概的代码如下:
窗体一:
unit Unit_bnzhcx;
interface
uses
...;
type
Tfrm_bnzhcx = class(TForm)
...
private
{ Private declarations }
procedure GetMsg(var Msg: TMessage); message WM_USER + 101; //在这里增加接收的过程
procedure SendUserMsg(var msg: string);
public
{ Public declarations }
end;
var
frm_bnzhcx: Tfrm_bnzhcx;
implementation
uses Unit_Condition;
{$R *.dfm}
procedure Tfrm_bnzhcx.GetMsg(var Msg: TMessage);
var
MapHandle: THandle;
pStr: Pointer;
Str: string;
i: integer;
begin
Memo1.Clear;
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 255, 'con1');
if (MapHandle > 0) then
begin
pStr := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 10); //得到共享内存的指针
Str := Pchar(pStr);
Str := Copy(Str, 2, Length(Str) - 1);
while Pos('$', Str) <> 0 do
begin
Memo1.Lines.Add(Copy(Str, 0, Pos('$', Str) - 1));
Str := Copy(Str, Pos('$', Str) + 1, length(Str) - Pos('$', Str));
end;
end;
CloseHandle(MapHandle);
end;
procedure Tfrm_bnzhcx.SendUserMsg(var msg: string);
var
Win: HWND;
Post: Pointer;
pStr: PShortString;
MapHandle: THandle;
begin
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 250, 'con1');
if (MapHandle > 0) then
begin
Post := MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0);
pStr := Post;
ZeroMemory(pStr, 250);
pStr^ := msg; //写共享内存
end;
Win := FindWindow('Tfrm_condition', nil);
if Win > 0 then
PostMessage(Win, WM_USER + 100, 0, 0); //发消息给接收程序的主窗口...
end;
procedure Tfrm_bnzhcx.AdvStrGrid1ClickCell(Sender: TObject; ARow,
ACol: Integer);
var ls_str: string;
begin
Cur_row := ARow; //记录党前行
if (ACol = 2) then
if (ARow <> 1) and (ARow <> 2) and (ARow <> 3) then
begin
//frm_condition := Tfrm_condition.Create(nil);
Application.CreateForm(Tfrm_condition, frm_condition);
case ARow of
1: ; //微机代码
2: ; //时间起
3: ; //时间止
4: ls_str := 'id_lsgx$name_dmmc$' + AdvStrGrid1.Cells[3, 4] + '$' + AdvStrGrid1.Cells[5, 4];
5: ls_str := 'id_zclx$name_zclx$' + AdvStrGrid1.Cells[3, 5] + '$' + AdvStrGrid1.Cells[5, 5];
6: ls_str := 'id_ysjc$name_ysjc$' + AdvStrGrid1.Cells[3, 6] + '$' + AdvStrGrid1.Cells[5, 6];
7: ls_str := 'id_gbhyml$name_gbhyml$' + AdvStrGrid1.Cells[3, 7] + '$' +AdvStrGrid1.Cells[5, 7];
8: ls_str := 'id_gbhy$name_gbhy$' + AdvStrGrid1.Cells[3, 8] + '$' + AdvStrGrid1.Cells[5, 8];
9: ls_str := 'id_jyhyml$name_jyhyml$' + AdvStrGrid1.Cells[3, 9] + '$' + AdvStrGrid1.Cells[5, 9];
10: ls_str := 'id_jyhy$name_dmmc$' + AdvStrGrid1.Cells[3, 10] + '$' + AdvStrGrid1.Cells[5, 10];
11: ls_str := 'id_dlwzsz$name_dmmc$' + AdvStrGrid1.Cells[3, 11] + '$' + AdvStrGrid1.Cells[5, 11];
end;
ls_str := ls_str + '$';
SendUserMsg(ls_str);
frm_condition.Show;
end;
end;
end.
××××××××××
窗体二:
unit Unit_Condition;
interface
uses
...;
type
Tfrm_condition = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
procedure GetMsg(var Msg: TMessage); message WM_USER + 100; //在这里增加接收的过程
procedure SendUserMsg(var msg: string);
public
{ Public declarations }
end;
var
frm_condition: Tfrm_condition;
implementation
uses Unit_DM1;
{$R *.dfm}
procedure Tfrm_condition.GetMsg(var Msg: TMessage);
var
MapHandle: THandle;
pStr: Pointer;
Str: string;
i: integer;
begin
Memo1.Clear;
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 255, 'con1');
if (MapHandle > 0) then
begin
pStr := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 10); //得到共享内存的指针
Str := Pchar(pStr);
Str := Copy(Str, 2, Length(Str) - 1);
while Pos('$', Str) <> 0 do
begin
Memo1.Lines.Add(Copy(Str, 0, Pos('$', Str) - 1));
Str := Copy(Str, Pos('$', Str) + 1, length(Str) - Pos('$', Str));
end;
end;
CloseHandle(MapHandle);
end;
procedure Tfrm_condition.SendUserMsg(var msg: string);
var
Win: HWND;
Post: Pointer;
pStr: PShortString;
MapHandle: THandle;
begin
MapHandle := CreateFileMapping(DWord(-1), nil, PAGE_READWRITE, 0, 250, 'result');
if (MapHandle > 0) then
begin
Post := MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0);
pStr := Post;
ZeroMemory(pStr, 250);
pStr^ := msg; //写共享内存
end;
Win := FindWindow('Tfrm_bnzhcx', nil);
if Win > 0 then
PostMessage(Win, WM_USER + 101, 0, 0); //发消息给接收程序的主窗口...
end;
procedure Tfrm_condition.cb_okClick(Sender: TObject);
begin
ls_str := 'fdsa$dsafds$adsf';
SendUserMsg(ls_str);
end;
end.