線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1750
推到 Plurk!
推到 Facebook!

如何由B.Form 控制A.Form ??

答題得分者是:careychen
TTS
初階會員


發表:72
回覆:66
積分:27
註冊:2003-05-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-09-20 00:01:52 IP:122.100.xxx.xxx 訂閱

[code delphi]
unit Unit1;

type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
protected
procedure WndProc(var Message : TMessage); override;
public
{ Public declarations }
end;

procedure TForm1.WndProc(var Message : TMessage);
begin
with Message do
if Msg = iPAOne then
Enabled:= True
else
inherited Wndproc(Message);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Enabled:= False;
Form2.Execute;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Form2:= TForm2.Create(Self);
Form2.hParent:= Handle;
end;

//***********************************

unit Unit2;


const
_sPAOne = 'MsgPAOne';

type
TForm2 = class(TForm)
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
procedure CheckBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure CheckBox2MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
hParent : HWND;
{ Public declarations }
function Execute: Integer;
end;

var
Form2: TForm2;
iPAOne: Integer;

function TForm2.Execute: Integer;
begin
FormStyle:= fsStayOnTop;
Show;
end;

procedure TForm2.CheckBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if CheckBox1.Checked then
begin
FormStyle:= fsNormal;
PostMessage(hParent, iPAOne, 0, 0);
end;
end;

procedure TForm2.CheckBox2MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if CheckBox2.Checked then
begin
FormStyle:= fsNormal;
end;
end;

Initialization
iPAOne := RegisterWindowMessage(PChar(_sPAOne));

[/code]

以上我的目的是要讓Form2都是用Show, 但是控制Form1.Enabled 來達到讓Form2有類似ShowModal的功用, 但又能在Check 但又能在CheckBox1按下時將Focus回到Fom1
但是我在Form2.PostMessage後 , Form1上的 Message.Msg 一直沒有接收到iPAOne
不知道是哪裡有問題? 煩請各位高手指點一下!

=Tks=
careychen
尊榮會員


發表:41
回覆:580
積分:959
註冊:2004-03-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-09-20 09:52:33 IP:218.210.xxx.xxx 訂閱
HI, 我試了您的程式,單就您 po 的那一段是可用的,但我有測到一個地方

就是在 *.dpr 的地方,您看一下你的 專案 Source 檔

看看他是不是像下面這樣

Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2); // 把這一行刪掉
Application.Run;

刪掉那一行後,程式就會正常了
------
價值的展現,來自於你用哪一個角度來看待它!!
系統時間:2024-04-26 22:07:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!