Thursday, June 4, 2015

How to give focus to a FoxPro top-level form at start-up

When I tried to  create a form that comes up without the surrounding FoxPro Form/workspace and menu structure, I used these code:
 
In the form properties:
     ThisForm.ShowWindow = 2  && Set the window to As Top-Level Form

In the form Init method:
     _Screen.visible = .F.

The problem is the form lost focus when it runs. Users have to click the icon on the taskbar  to bring the form to the top.

On MSDN LostFocus Event page, there is explanation:
A form loses the focus when the form has no controls, all its controls have their Enabled and Visible properties set to false (.F.), or another form gets the focus.
 
So the second command caused the problem. I tried to solve this issue. I did some search and finally found this article:
http://hexcentral.blogspot.ca/2013/04/how-to-give-focus-to-foxpro-top-level.html
 
===================================================

How to give focus to a FoxPro top-level form at start-up

 By Mike Lewis

This is a question I often see in Visual FoxPro forums. An application needs to show a top-level form (typically a log-in screen) at start-up. But when you launch the form, it
appears behind other windows on the desktop. Even if it is at the front, it's not necessarily the active form. So how do you give the form focus programmatically?


None of the obvious techniques seem to work. These include toggling the form's AlwaysOnTop property, and calling the SetFocus method for one of its controls. However, the following code will always do the trick:
DECLARE INTEGER SetForegroundWindow IN WIN32API INTEGER
SetForegroundWindow(thisform.HWnd)
CLEAR DLLS "SetForegroundWindow"
 
All you have to do is place that code in the form's Init method, and the problem's solved. (The code works in VFP 7.0 and above.)
 
 
 

No comments:

Post a Comment