?
Solved

Prevent window from getting focus

Posted on 1999-12-27
10
Medium Priority
?
660 Views
Last Modified: 2010-04-06
I developed a form which I use as an onscreen keyboard. I never want this form to get the focus since the field to receive the characters must retain the focus.

How can I prevent a form from ever getting the focus. I am able to acomplish some of it with

procedure TFormKeyboard.WMMouseActivate(var Message: TMessage);
begin
  Message.Result := MA_NOACTIVATE;
end;

The keyboard form still gets the focus on FormKeyboard.Show and if the user moves it.
 
0
Comment
Question by:moonrise
  • 5
  • 5
10 Comments
 

Author Comment

by:moonrise
ID: 2308619
Edited text of question.
0
 
LVL 17

Accepted Solution

by:
inthe earned 800 total points
ID: 2308678
Hi,
you need a WmNCHitTest handler as well so your form looks like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin
  DefaultHandler(Msg);
  if Msg.Result = HTCAPTION then
    Msg.Result := HTNOWHERE;
end;

end.


Regards Barry
0
 
LVL 17

Expert Comment

by:inthe
ID: 2308684
i was bit worried if keyboard still get focus so i tested with keyboard component:(maybe same one as you are using)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin
  DefaultHandler(Msg);
  if Msg.Result = HTCAPTION then
    Msg.Result := HTNOWHERE;
end;

procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
showmessage(char);
end;

end.

it seems to work well as the keyboard still get focus correctly but form doesnt.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 

Author Comment

by:moonrise
ID: 2308689
I tried your suggestion. The form still gets the focus when I "Show" it. Also, from what i understand this prevents the form from being moved - the user needs to be able to move the keyborad around so this will not work for me.
Thank you.
0
 

Author Comment

by:moonrise
ID: 2308728
I tried your suggestion. The form still gets the focus when I "Show" it. Also, from what i understand this prevents the form from being moved - the user needs to be able to move the keyborad around so this will not work for me.
Thank you.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2308733
ok now the form only move if mouse is on component:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
   procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
 procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function MouseOverComponent(Msg: TWMNCHitTest; Sender : TControl) : boolean; begin
  Msg.XPos := Msg.XPos - Sender.Parent.Left;
  Msg.YPos := Msg.YPos - Sender.Parent.Top;
  result := true;
  result := result AND (Msg.XPos >= Sender.Left);
  result := result AND (Msg.XPos <=  Sender.Left + Sender.Width);   result := result AND (Msg.YPos >= Sender.Top);
  result := result AND (Msg.YPos <=  Sender.Top + Sender.Height); end;



procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

{procedure TForm1.WmNCHitTest(var Msg: TWMNCHitTest);
begin

  if Msg.Result = HTCLIENT then
    Msg.Result := HTCAPTION
   else if
    }
    procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  inherited;
  DefaultHandler(Msg);
  IF Msg.Result = HTCAPTION then
    Msg.Result := HTCLIENT
    ELSE
  if (htClient = Msg.Result) and (MouseOverComponent(Msg,kEYBOARD1))then
    Msg.Result := htCaption;
end;


procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
showmessage(char);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  L : LongInt;    //stop alt tab keys
begin
  SystemParametersInfo (97, Word (True), @L, 0);
  end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  L : LongInt;    //re-enable alt tab keys
begin
  SystemParametersInfo (97, Word (False), @L, 0);
  end;

end.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2308739
yes form still gets focus when shown though :-( working on that one

just wondering are you calling the form from another form
ie:
form2.showmodal; ?
0
 

Author Comment

by:moonrise
ID: 2308742
Here is a better explanation of what I am trying to do:

I have two forms:

Form1 has a button with Form2.Show when clicked.

Form2 has the keyboard component.

The button Form1 and the buttons used in the keyboard do not get the focus (they are like speedbuttons).

When I click on the button on Form1 I want Form2 to be displayed but I want Form1 to retain the focus so that characters typed on the keyboard on Form2 will be displayed on Form1.
Thank you.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2308800
HI,
 you know you can do this on the keyboard form to send text to form1 editbox for instance:

procedure TForm2.Keyboard1Key(Sender: TObject; Char: Char);
begin
Form1.Edit1.Text := Form1.Edit1.Text + char;
end;

well this was best i got:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Keyboard;

type
  TForm1 = class(TForm)
    Keyboard1: TKeyboard;
    procedure Keyboard1Key(Sender: TObject; Char: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  procedure WMMouseActivate(var Msg: TWMMouseActivate); message WM_MOUSEACTIVATE;
  procedure WmNCHitTest(var Msg :TWMNCHitTest); message WM_NCHITTEST;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

function MouseOverComponent(Msg: TWMNCHitTest; Sender : TControl) : boolean;
 begin
  Msg.XPos := Msg.XPos - Sender.Parent.Left;
  Msg.YPos := Msg.YPos - Sender.Parent.Top;
  result := true;
  result := result AND (Msg.XPos >= Sender.Left);
  result := result AND (Msg.XPos <=  Sender.Left + Sender.Width);
  result := result AND (Msg.YPos >= Sender.Top);
  result := result AND (Msg.YPos <=  Sender.Top + Sender.Height);
 end;

procedure TForm1.WMMouseActivate(var Msg: TWMMouseActivate);
 begin
  Msg.Result := MA_NOACTIVATE;
end;

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
 var
  h : hwnd;
begin
  inherited;
  DefaultHandler(Msg);
  IF Msg.Result = HTCAPTION then
    Msg.Result := HTCLIENT
    ELSE
  if (htClient = Msg.Result) and (MouseOverComponent(Msg,kEYBOARD1))then
    Msg.Result := htCaption;
h := getnextwindow(form1.handle,GW_HWNDFIRST);
setforegroundwindow(h);

end;


procedure TForm1.Keyboard1Key(Sender: TObject; Char: Char);
begin
Form2.Edit1.Text := Form2.Edit1.Text + char;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  L : LongInt;    //stop alt tab keys
begin
  SystemParametersInfo (97, Word (True), @L, 0);
  end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  L : LongInt;    //re-enable alt tab keys
begin
  SystemParametersInfo (97, Word (False), @L, 0);
  end;

procedure TForm1.FormActivate(Sender: TObject);
var
  h : hwnd;
begin
h := getnextwindow(form1.handle,GW_HWNDFIRST);
setforegroundwindow(h);

end;

end.

i know its not perfect :-(
Regards Barry
0
 

Author Comment

by:moonrise
ID: 2309063
Thank you. I was able to work something up from what you gave me.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question