Link to home
Start Free TrialLog in
Avatar of nguyenn
nguyenn

asked on

Application lose focused

I have 2 applications (app1 and app2) run the same time. From app1, I call a DLL to display a report on the screen. After print out report, close it, it doesnt focus back to the app1, but app2

I'm using VB6, Crystal Report 7

Thanks in advance
nguyenn
Avatar of WolfgangKoenig
WolfgangKoenig

In VB you are working with a Form object just call:
call Form1.Show()

to invoke the Form1 of your Vb exe project.

;O)
WoK
The api function to focus an specific window (app) is in VB:

Public Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

hWnd
[in] Handle to the window
nCmdShow
[in] Specifies how the window is to be shown
SW_SHOW  = 5
Activates the window and displays it in its current size and position

WoK

To get a window handle (hWnd) for your application call:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

For instance:

Dim hWnd As Long
hWnd = FindWindow ("","App_Window_Titel")
call ShowWindow (hWnd,5)

Thats was it
WoK
Avatar of nguyenn

ASKER

Hi Wok,

The first comment cannot apply to my app, since when calling report, i'm using vbModal

i.e. frmReport.Show vbModal

Could you give me a sample code of your sencond comment

Thanks
ASKER CERTIFIED SOLUTION
Avatar of WolfgangKoenig
WolfgangKoenig

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have tested the 'nCmdShow' and only 9 works but
the MSDN says following about the 'nCmdShow' parameters:

SW_FORCEMINIMIZE Windows 2000: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread.
SW_HIDE Hides the window and activates another window.
SW_MAXIMIZE Maximizes the specified window.
SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW Activates the window and displays it in its current size and position.  
SW_SHOWDEFAULT Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.  
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE Displays the window as a minimized window.
This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
 
SW_SHOWNA Displays the window in its current size and position.
This value is similar to SW_SHOW, except the window is not activated.
 
SW_SHOWNOACTIVATE Displays a window in its most recent size and position.
This value is similar to SW_SHOWNORMAL, except the window is not actived.
 
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.



Here are the defines for the vars:
define SW_HIDE             0
#define SW_SHOWNORMAL       1
#define SW_NORMAL           1
#define SW_SHOWMINIMIZED    2
#define SW_SHOWMAXIMIZED    3
#define SW_MAXIMIZE         3
#define SW_SHOWNOACTIVATE   4
#define SW_SHOW             5
#define SW_MINIMIZE         6
#define SW_SHOWMINNOACTIVE  7
#define SW_SHOWNA           8
#define SW_RESTORE          9
#define SW_SHOWDEFAULT      10
#define SW_FORCEMINIMIZE    11
#define SW_MAX              11


Normally SW_SHOWNORMAL should do the right but it don't work normally ... That the reason i have choose SW_SHOWNA for the above example.

WoK

Avatar of nguyenn

ASKER

Hi Wok,

I tried both ways, but it still doesnt work. After closing report form, it focus to the app2.

I'm very appreciated for your help
nguyenn
You must take the windows title of app2:

Dim hWnd As Long
hWnd = FindWindow ("","App2_Windows_Titel")
'hWnd must be <> 0 ! after this (debug it)

call ShowWindow (hWnd,1) 'try on of these out
call ShowWindow (hWnd,5) 'try on of these out
call ShowWindow (hWnd,9) 'try on of these out

(The windows titel you can found at the windows tray bar when you move the nouse over it ...)

Ops i means App2 -> App1
Avatar of nguyenn

ASKER

Hi Wok,

I passed my app1's title to function called (app.Title), and tried all of possible ways above but it doesnt help me out. One thing I could guess, my app2 collect data and save to the database all the time, that why eventhough we set focus to app1, but app2 is running and it setfocus to app2. I'm curious it doesnt happen to other reports (report file, not from report dll)

Avatar of nguyenn

ASKER

Although my problem hasnt solved, but I would like to give all my points to you for your gracious helped. Thanks again Wok,

Have a nice day :)
nguyenn