1

I have a custom control derived from TPanel named TTestCtrl. It holds a TImage32 (from Graphics32).

When the user double clicks on the image, I show a message. The problem is that after I close the message, the focus is not returned back to the main application. So, the first click, no matter what I click on in the main app/main form, is lost.

Strange thing: If I call the Mesaj() procedure not from the TTestCtrl but from the main form, it works (the first click is not lost anymore):

unit DerivedControl;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls, Vcl.Dialogs, Vcl.Forms, GR32, GR32_Image;

type
  TTestCtrl = class(TPanel)
  private
    Img: TImage32;
  protected
    procedure ChromaDblClick(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
  published
  end;

procedure Mesaj(const MessageText, Title: string);

implementation

procedure Mesaj(const MessageText, Title: string);
begin
{$IFDEF MSWINDOWS}
   Application.MessageBox(PChar(MessageText), PChar(Title), 0)  { 'Title' will appear in window's caption }
{$ELSE}
   MessageDlg(MessageText, mtInformation, [mbOk], 0);
{$ENDIF}
end;

constructor TTestCtrl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width         := 200;
  Height        := 86;
  Img := TImage32.Create(Self);
  Img.Parent        := Self;
  Img.Align         := alClient;
  Img.OnDblClick    := ChromaDblClick;
end;

procedure TTestCtrl.ChromaDblClick(Sender: TObject);
begin
  Mesaj('Caption', 'From derived control');      // focus lost
end;

end.

The simple/minimal application below is the tester:

unit TesterForm;

interface

uses
  System.SysUtils, System.Classes, Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.Controls, vcl.Forms, DerivedControl;

type
  TfrmTester = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
  end;

var
  frmTester: TfrmTester;

implementation

{$R *.dfm}

var
  Ctrl: TTestCtrl;

procedure TfrmTester.FormCreate(Sender: TObject);
begin
  Ctrl := TTestCtrl.Create(Self);
  Ctrl.Parent := Self;
end;

procedure TfrmTester.Button1Click(Sender: TObject);
begin
  Mesaj('Caption', 'From main form');      // works
end;

end.
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • Give focus to the form before launching the dialog. Or debug TImage32. – Sertac Akyuz Oct 20 '16 at 18:07
  • @Silvester - I meant before. But you can try otherwise... The form that your control is parented with. You can use Parent or GetParentForm or similar. – Sertac Akyuz Oct 20 '16 at 18:09
  • 1
    Important note: I used the word 'focus' but I don't know if it is the correct word. The main form doesn't seem to be lost focus (to be a background window). It will just ignores the first click. – Gabriel Oct 20 '16 at 18:12
  • I have gr32 but I don't have a complete program. I'd have to work to build one. [mcve] – David Heffernan Oct 20 '16 at 18:36
  • Please read this [mcve] – David Heffernan Oct 20 '16 at 19:35
  • Silvester, the page linked by @David does not mention it but off-site downloads are not regarded. I'm not exactly sure what was wrong about your previous case. Maybe it failed to compile, or maybe you should have consolidated all into a unit. ... – Sertac Akyuz Oct 21 '16 at 10:08
  • 1
    I duplicated this with your previous revision. My suggestion is to not to launch a dialog on an image32 with a double click. Mouse handling in gr32 is quite complicated. – Sertac Akyuz Oct 22 '16 at 13:41
  • BTW you don't need a task dialog to observe the problem, message box is fine. Try to minimize the code required to duplicate the problem when preparing a mcve. – Sertac Akyuz Oct 22 '16 at 13:43
  • @SertacAkyuz-Thanks. Got it. – Gabriel Oct 23 '16 at 09:09
  • Good news: Graphics32 has been updated last month (04.2017) – Gabriel May 11 '17 at 10:53

1 Answers1

1

Try this :

procedure TTestCtrl.ChromaDblClick(Sender: TObject);
var F : TcustomForm;
begin
  Mesaj('Caption', 'From derived control');      // focus lost
  F := GetParentForm(Self);
  if Assigned(F) then F.BringToFront;

end;