Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help getting Delphi to call SetFocus Windows API function...

1,119 views
Skip to first unread message

Craig P. Thompson

unread,
May 5, 1996, 3:00:00 AM5/5/96
to

Greetings,

I am trying to get Delphi to call the SetFocus Windows API function but it apparently wants to
treat SetFocus as a method of my form rather than the Windows API function. The following code
results in a compile Error 85: ";" expected with the cursor pointing right after the SetFocus
function but before the (HadFocus) parameter list.

HadFocus := GetFocus;
Show;
PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
SetFocus(HadFocus);

You will notice that the GetFocus API function call works and I suspect this is because there is
no Delphi Object Pascal method for Forms called "GetFocus".

My question is how does one tell Delphi to use the Windows API function instead of a similarly
named object method?

Arno Theron

unread,
May 6, 1996, 3:00:00 AM5/6/96
to tho...@primenet.com

"Craig P. Thompson" <tho...@primenet.com> wrote:
>Greetings,
>
>I am trying to get Delphi to call the SetFocus Windows API function but it apparently wants to
>treat SetFocus as a method of my form rather than the Windows API function. The following code
>results in a compile Error 85: ";" expected with the cursor pointing right after the SetFocus
>function but before the (HadFocus) parameter list.
>
> HadFocus := GetFocus;
> Show;
> PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
> SetFocus(HadFocus);
>
>You will notice that the GetFocus API function call works and I suspect this is because there is
>no Delphi Object Pascal method for Forms called "GetFocus".
>
>My question is how does one tell Delphi to use the Windows API function instead of a similarly
>named object method?


--
Since the function you want to use is in the WinProces Unit ,
do this:

winprocs.setfocus(HadFocus);

Hope this helps.

Arno Theron
PC-Estim/PROJEVAL(7D2C)
Tel(012) 311-1824
082-569-2532
Fax(012) 311-4438
eMAIL : THER...@TELKOM19.TELKOM.CO.ZA

Isaac Hepworth

unread,
May 6, 1996, 3:00:00 AM5/6/96
to

"Craig P. Thompson" <tho...@primenet.com> wrote:

>Greetings,

>I am trying to get Delphi to call the SetFocus Windows API function but it apparently wants to
>treat SetFocus as a method of my form rather than the Windows API function. The following code
>results in a compile Error 85: ";" expected with the cursor pointing right after the SetFocus
>function but before the (HadFocus) parameter list.

> HadFocus := GetFocus;
> Show;
> PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
> SetFocus(HadFocus);

>You will notice that the GetFocus API function call works and I suspect this is because there is
>no Delphi Object Pascal method for Forms called "GetFocus".

>My question is how does one tell Delphi to use the Windows API function instead of a similarly
>named object method?

You use WinProcs.SetFocus(HadFocus);

--
Isaac Hepworth ijh...@cam.ac.uk
Corpus Christi College, Cambridge University, UK


Craig P. Thompson

unread,
May 7, 1996, 3:00:00 AM5/7/96
to

> You use WinProcs.SetFocus(HadFocus);

> Isaac Hepworth ijh...@cam.ac.uk
> Corpus Christi College, Cambridge University, UK

Thanks much for the quick response! Works great!
Craig.

Bengt Richter

unread,
May 8, 1996, 3:00:00 AM5/8/96
to

"Craig P. Thompson" <tho...@primenet.com> wrote:

>Greetings,

>I am trying to get Delphi to call the SetFocus Windows API function but it apparently wants to
>treat SetFocus as a method of my form rather than the Windows API function. The following code
>results in a compile Error 85: ";" expected with the cursor pointing right after the SetFocus
>function but before the (HadFocus) parameter list.

> HadFocus := GetFocus;
> Show;
> PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
> SetFocus(HadFocus);

>You will notice that the GetFocus API function call works and I suspect this is because there is
>no Delphi Object Pascal method for Forms called "GetFocus".

>My question is how does one tell Delphi to use the Windows API function instead of a similarly
>named object method?

If you want to access an ambiguously named thing from a specific
unit, do it explicitly, e.g., SpecificUnit.AmbiguouslyNamedThing,
or in this case perhaps Windows.SetFocus(HadFocus); That way it
can't accidentally be hidden by something in the immediate scope,
and won't be affected by shuffling uses-order.

Regards,
Bengt Richter

Chris

unread,
May 9, 1996, 3:00:00 AM5/9/96
to

In article <318D74...@primenet.com>, tho...@primenet.com says...
>
>Greetings,...

>
> HadFocus := GetFocus;
> Show;
> PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
> SetFocus(HadFocus);
>...

>My question is how does one tell Delphi to use the Windows API function
instead of a similarly
>named object method?

Hello,

Maybe the following will help?

var
HadFocus : string;

{Assuming that some object has the focus,}
begin
HadFocus := Object.Name; //Save the name property
Show; //Show whatever it is you want to show
PaintBox1.Canvas.TextOut(0, 0, SubMsgText); //Do something
with Object as TObject do //Typecast the HadFocus variable you saved
SetFocus; //SetFocus back to it.
end;


I don't know about actually referencing the Win API SetFocus function unless
maybe it uses some unit that is not in your uses list? (ie., adding to uses
would let the compiler know it is not meant to be a form method)

Hope it helps
Chris


Paul Motyer

unread,
May 10, 1996, 3:00:00 AM5/10/96
to

To access the API when the function name has been defined in a unit in
your uses list
what I do is re-import the function from the dll using a new name, eg:

unit MyUnit;// for D2

interface
type
DWORD = Integer; //from windows.pas
BOOL = LongBool;
const
kernel32 = 'kernel32.dll';

function MyBeep(dwFreq, dwDuration:Dword):Bool; stdcall;
// dwFreq:sound frequency, in hertz
// dwDuration:sound duration, in milliseconds
// works in NT as expected, defaults to standard beep in Win95

implementation
function MyBeep; external kernel32 name 'Beep';
end.

beep is defined in SysUtils.pas as:
procedure Beep;
begin
MessageBeep(0);
end;

and defined in windows.pas:
interface:function Beep(dwFreq, dwDuration: DWORD): BOOL; stdcall;
implementation:function Beep; external kernel32 name 'Beep';

(a minor 'bug').
--
Paul, (Fri, 9 May 96, 11:20 EST)
SoftStuff, Croydon, Victoria, Australia, 3136
pa...@linuxserver.pccity.com.au

On Friday, May 10, 1996, Chris wrote...

na...@vivid.net

unread,
May 12, 1996, 3:00:00 AM5/12/96
to

"Craig P. Thompson" <tho...@primenet.com> wrote:
>I am trying to get Delphi to call the SetFocus Windows API function but it apparently wants to
>treat SetFocus as a method of my form rather than the Windows API function. The following code
>results in a compile Error 85: ";" expected with the cursor pointing right after the SetFocus
>function but before the (HadFocus) parameter list.

> HadFocus := GetFocus;


> Show;
> PaintBox1.Canvas.TextOut(0, 0, SubMsgText);
> SetFocus(HadFocus);

>You will notice that the GetFocus API function call works and I suspect this is because there is

>no Delphi Object Pascal method for Forms called "GetFocus".

>My question is how does one tell Delphi to use the Windows API function instead of a similarly
>named object method?

Just change SetFocus(HadFocus); to Windows.SetFocus(HadFocus); for
delphi 2, or for Delphi 1, WinProcs.SetFocus(HadFocus); Easy eh? =)


Dana Kirk

unread,
May 13, 1996, 3:00:00 AM5/13/96
to

Definately a newbie.

I have tried everything I can think of, and perhaps I have no clue, or I'm
missing something simple. However, I am trying to click a button on a
child form of another app from Delphi 2.0 running Win 95. I have
successfully obtained the handle for all Windows, The main frame, the MDI
Client, and all childs. But when I send the external app messages from my
program, I get no visual response (ie, the button does not do its thing.)

Please help, here is the code I'm working with. I have no declarations
other than the standard declarations when selecting the "OnKeyUp" event. I
would be happy to use Delphi "TKey" conventions if I knew how.

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
If ((Shift = [ssCtrl]) and (Key = 13)) then
Begin
testing := 0;
CurrentWindow := GetActiveWindow();
MyWindow := FindWindow('SomeFrame', 'SomeName');
MyMDI := FindWindowEx(MyWindow, testing, 'MDIClient' + #0, '' + #0);
MyChild := FindWindowEx(MyMDI, testing, 'My Child' + #0, 'SomeName' +
#0);
HiddenComboBox := FindWindow('ComboLBox', '');
Button := FindWindowEx(MyChild, Testing, 'SomeName' + #0, '' + #0);
Windows.SendMessage(MyChild, WM_KEYDOWN, VK_RETURN, 0);
Windows.SendMessage(MyChild, WM_KEYUP, VK_RETURN, 0);
Windows.SetForegroundWindow(MyChild)
end;
end;

PS, it appears that switching Focus between my program and the target
external application has each application running their code when they
have focus exclusively. If someone could give me an example of successful
code with a SID external app I believe I can adapt.

Thanks for your time ;-)

Dana Kirkpatrick


Mike Chapin

unread,
May 14, 1996, 3:00:00 AM5/14/96
to

In article <01bb3e12.73429f20$114c...@paulm.pccity>,

Paul Motyer <pa...@linuxserver.pccity.com.au> wrote:
>To access the API when the function name has been defined in a unit in
>your uses list
>what I do is re-import the function from the dll using a new name, eg:
>[snip...]

>beep is defined in SysUtils.pas as:
>procedure Beep;
>begin
> MessageBeep(0);
>end;
>
>and defined in windows.pas:
>interface:function Beep(dwFreq, dwDuration: DWORD): BOOL; stdcall;
>implementation:function Beep; external kernel32 name 'Beep';
>
>(a minor 'bug').

Not a bug at all.

Why go through all that when all you have to do is call it with the unit name
specified, i.e. windows.beep;?

0 new messages