2015-02-23 92 views
0

我試圖用幾個子控件設計一個TPopup,然後將事件處理程序分配給那些需要它們的控件(主要是按鈕)。我正在使用TPopup.IsOpen:= True。 當使用TPopup.popup(True)時,檢測到輸入並且所有鼠標事件都很好,但我不希望用戶做任何事情,而不是從彈出窗口「點擊」來關閉它。TPopup子控件不接受鍵盤/鼠標輸入 - (Delphi XE7 FireMonkey)

在這裏發現了非常類似的問題,但除了使用模態彈出窗口之外並沒有真正合適的答案。 Delphi XE5 FireMonkey TstringGrid cells don't accept keyboard input

而且,這也有一個可以接受的答案,但我的風格有不透明的區域,呈現無邊界形式的黑色。我會設置表單的透明度,但這會導致我希望在另一天處理的性能問題。 Allowing keyboard input to a FireMonkey TEdit nested inside a FireMonkey TPopup

從開始全部過程完成: 1.集TPopup.StyleLookup:= 'myStyle的'; 2.分配事件處理程序到子控件 3.設置TPopup.IsOpen:= True; 4.嘗試任何TNumberBox /編輯(無鍵盤輸入檢測),按Tab鍵 5.嘗試點擊使用分配處理任何按鈕(無鼠標輸入檢測)

編輯

很多後測試我能夠獲得按鍵的鼠標事件,但我仍然無法獲得用戶的鍵盤輸入。我從打開

  1. 如果只是右鍵點擊鼠標右鍵,彈出我的測試應用程序,附加的示例代碼將打開的ButtonStyle標準彈出應用
  2. 如果點擊右鍵並移位,將打開的ButtonStyle應用
  3. 模式彈出
  4. 如果右鍵和alt,打開應用了memostyle標準彈出(這不是工作的部分)

我們的目標是讓用戶在彈出的輸入。在表單上有一個TMemo用於測試如果彈出窗口的「TMemo」在單擊彈出窗口後將獲得焦點,並驗證標準TMemo的樣式名稱。另外,tmemo還是一個小孩。我用這個來創建一個可以應用於我的TPopup的基本風格。 (請原諒任何不好的變量或未使用的代碼...我已經嘗試了很多不同的東西,運氣不佳..我不確定從哪裏開始以及要拋哪些東西)

單元1代碼:

unit Unit1; 

interface 

uses 
    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
    FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,System.Rtti, 
    FMX.Styles.Objects, FMX.Layouts, FMX.Memo; 

type 
    TForm1 = class(TForm) 
    Memo1: TMemo; 
    StyleBook1: TStyleBook; 
    Layout1: TLayout; 
    Memo2: TMemo; 
    Popup1: TPopup; 
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; 
     Shift: TShiftState; X, Y: Single); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    procedure DoButtonClick(Sender:TObject); 
    procedure DoMemoClick(Sender:TObject); 
    function FindRootStyleResource(const AObject: TFmxObject; const AStyleLookup: string):TFmxObject; 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.fmx} 

procedure TForm1.DoButtonClick(Sender: TObject); 
begin 
    showmessage('WoooHooo!'); 
end; 

procedure TForm1.DoMemoClick(Sender: TObject); 
begin 
    if Sender is TMemo then 
    Begin 
    Tmemo(Sender).SetFocus; 
    with FindRootStyleResource(TFmxObject(Sender),'background') as TActiveStyleObject do 
    Begin 
     CanFocus:=True; 
     HitTest:=True; 
     Locked:=False; 
     SetFocus; 
    End; 
    Self.Focused:=nil;//Removes the focus from the current form to TPopup (TCommonCustomForm) 
    End; 
end; 

function TForm1.FindRootStyleResource(const AObject: TFmxObject; 
    const AStyleLookup: string): TFmxObject; 
var 
    SearchResult,Child:TFmxObject; 
begin 
    Result:=nil; 
    //No object get out 
    if AObject=nil then 
    exit; 
    //No Style lookup, get out 
    if AStyleLookup='' then 
    exit; 
    //If Current object is what we're looking for, set result 
    if AObject.StyleName.ToLower=AStyleLookup.ToLower then 
    Result:=AObject; 
    //if Object has children need to check lower levels 
    if AObject.ChildrenCount>0 then 
    Begin 
    //Now Recurse the children 
    for Child in AObject.Children do 
    Begin 
     SearchResult:=FindRootStyleResource(Child,AStyleLookup); 
     if SearchResult<>nil then 
     Result:=SearchResult 
    End; 
    End; 
end; 

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Single); 
Var 
O:TFmxObject; 
begin 
    if (Button=TMouseButton.mbRight) and not ((ssShift in Shift) or (ssAlt in Shift)) then 
    Begin 
    Popup1.Width:=100; 
    Popup1.Height:=100; 
    Popup1.StyleLookup:='buttonstyle'; 
    ApplyStyleLookup; 
    (* 
     O:= FindRootStyleResource(popup1,'background'); 
     TButtonStyleObject(O).OnClick:=DoButtonClick; 
     TButtonStyleObject(O).HitTest:=True; 
     TButtonStyleObject(O).Locked:=False; 
    *) 
    Popup1.StylesData['background.OnClick']:=TValue.From<TNotifyEvent>(DoButtonClick); 
    Popup1.StylesData['background.HitTest']:=True; 
    Popup1.StylesData['background.Locked']:=False; 
    Popup1.IsOpen:=True; 
    End 
    else if (Button=TMouseButton.mbRight) and (ssShift in Shift) then 
    Begin 
    Popup1.Width:=100; 
    Popup1.Height:=100; 
    Popup1.StyleLookup:='buttonstyle'; 
    ApplyStyleLookup; 
(* 
     O:= FindRootStyleResource(popup1,'background'); 
     TButtonStyleObject(O).OnClick:=DoButtonClick; 
     TButtonStyleObject(O).HitTest:=True; 
     TButtonStyleObject(O).Locked:=False; 
*) 
    Popup1.StylesData['background.OnClick']:=TValue.From<TNotifyEvent>(DoButtonClick); 
    Popup1.StylesData['background.HitTest']:=True; 
    Popup1.StylesData['background.Locked']:=False; 
    Popup1.Popup(True); 
    End 
    else if (Button=TMouseButton.mbRight) and (ssAlt in Shift) then 
    Begin 
    Popup1.Width:=100; 
    Popup1.Height:=100; 
    Popup1.StyleLookup:='MemoPopupStyle'; 
    ApplyStyleLookup; 
    Popup1.StylesData['content.OnClick']:=TValue.From<TNotifyEvent>(DoMemoClick); 
    Popup1.StylesData['content.HitTest']:=True; 
    Popup1.StylesData['content.Locked']:=False; 

    //Popup1.StylesData['background.TabStop']:=True; 
    //Popup1.StylesData['background.Enabled']:=True; 
    Popup1.IsOpen:=True; 
    End; 

end; 

end. 

項目來源:

program Project1; 

uses 
    System.StartUpCopy, 
    FMX.Forms, 
    Unit1 in 'Unit1.pas' {Form1}; 

{$R *.res} 

begin 
    Application.Initialize; 
    Application.CreateForm(TForm1, Form1); 
    Application.Run; 
end. 

同樣,任何幫助非常感謝,謝謝!

回答

0

決定只是去與這個答案在這裏: Allowing keyboard input to a FireMonkey TEdit nested inside a FireMonkey TPopup

對於透明度,我添加了一個孩子TPanel在名爲Content fmPopup形式。之後,我設置了Transparency:= True,並將我的自定義樣式應用於「內容」面板。不完全是我想要的,因爲我必須編寫自己的TPopup已有的定位/隱藏過程,但我現有的「初始化樣式」過程沒有任何修改就能正常工作。我當然歡迎有更好的解決方案。

相關問題