dbTalk Databases Forums  

Re: SetForegroundWindow API not working

comp.databases.xbase.fox comp.databases.xbase.fox


Discuss Re: SetForegroundWindow API not working in the comp.databases.xbase.fox forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
news.idirect.com
 
Posts: n/a

Default Re: SetForegroundWindow API not working - 10-08-2003 , 04:39 PM






The following is C code to set the foreground window (I know, this is a
foxpro newsgroup, but this is pretty simple C).

You need to attach your message queue to the current foreground window, if
you want to FORCE focus to your app, even if another app is active.
Normally, you are not allowed to "steal" focus, but you may have good reason
to, so this method will always work.

Let me know if you need clarification:

// Force a window to front, ripping them away from any current application
// Even works on Win98 and up
static void ForceWindowToFront(HWND hwndForce)
{
DWORD nMyProcessId, nCurProcessId;
WINDOWPLACEMENT wp;
int nShowType;

// Maintain "maximized" status when showing
ZeroMemory(&wp, sizeof(wp));
wp.length = sizeof(wp);
GetWindowPlacement(hwndForce, &wp);
switch (wp.showCmd) {
case SW_SHOWMAXIMIZED:
nShowType = SW_MAXIMIZE;
break;
default:
ShowWindow(hwndForce, SW_RESTORE);
break;
}

// Get process ID of the window and the foreground window
nMyProcessId = GetWindowThreadProcessId(hwndForce, 0);
nCurProcessId = GetWindowThreadProcessId(GetForegroundWindow(), 0);

// Do I need to do the AttachThreadInput trick?
if (nMyProcessId != nCurProcessId) {
AttachThreadInput(nCurProcessId, nMyProcessId, TRUE);
SetForegroundWindow(hwndForce);
AttachThreadInput(nCurProcessId, nMyProcessId, FALSE);
}

if (GetForegroundWindow() != hwndForce)
SetForegroundWindow(hwndForce);
}


"Andrea Mariottini" <a.mariottini (AT) libero (DOT) it> wrote

Quote:
I'm trying to use SetForegroundWindow API to show the main
window of my application when it is not in foreground.

It doesn't work!

The applications flashes in the taskbar but doesn't go
in foreground.

The call works only if my application is first minimized.

(System: VFP7 SP1 + Win2000 Pro SP3)

--
Andrea Mariottini




Reply With Quote
  #2  
Old   
Cetin Basoz
 
Posts: n/a

Default Re: SetForegroundWindow API not working - 10-11-2003 , 10:16 AM






Yes pretty simple C. Somewhat a similar VFP code was in backyard

DECLARE integer GetForegroundWindow IN win32API
Declare integer GetCurrentThreadId in win32API
declare integer GetWindowThreadProcessId in win32API ;
integer hWnd, integer @lpdwProcessId
DECLARE short SetForegroundWindow IN win32API integer lnHWnd
Declare short AttachThreadInput in win32API ;
integer idAttach, integer idAttachTo, short fAttach


oTmr = CreateObject('myTimer', GetForegroundWindow())
? 'Started countdown'
Read events

Define Class myTimer as Timer
nHWND = 0
Interval = 10000
Procedure Init
Lparameters tnHWND
this.nHWND = tnHWND
endproc
Procedure Timer
This.Enabled = .f.
this.GetFocusBack()
Clear events
endproc
Procedure GetFocusBack
lpdwProcessId = 0
lnThreadID1 = GetCurrentThreadId()

lnThreadID2 = GetWindowThreadProcessId(this.nHWND, @lpdwProcessId)

AttachThreadInput(lnThreadID1, lnThreadID2, 1)
SetForegroundWindow(this.nHWND)
AttachThreadInput(lnThreadID1, lnThreadID2, 0)

endproc
enddefine

"news.idirect.com" <nomail (AT) nomorespam (DOT) nope> wrote

Quote:
The following is C code to set the foreground window (I know, this is a
foxpro newsgroup, but this is pretty simple C).

You need to attach your message queue to the current foreground window, if
you want to FORCE focus to your app, even if another app is active.
Normally, you are not allowed to "steal" focus, but you may have good
reason
to, so this method will always work.

Let me know if you need clarification:

// Force a window to front, ripping them away from any current application
// Even works on Win98 and up
static void ForceWindowToFront(HWND hwndForce)
{
DWORD nMyProcessId, nCurProcessId;
WINDOWPLACEMENT wp;
int nShowType;

// Maintain "maximized" status when showing
ZeroMemory(&wp, sizeof(wp));
wp.length = sizeof(wp);
GetWindowPlacement(hwndForce, &wp);
switch (wp.showCmd) {
case SW_SHOWMAXIMIZED:
nShowType = SW_MAXIMIZE;
break;
default:
ShowWindow(hwndForce, SW_RESTORE);
break;
}

// Get process ID of the window and the foreground window
nMyProcessId = GetWindowThreadProcessId(hwndForce, 0);
nCurProcessId = GetWindowThreadProcessId(GetForegroundWindow(), 0);

// Do I need to do the AttachThreadInput trick?
if (nMyProcessId != nCurProcessId) {
AttachThreadInput(nCurProcessId, nMyProcessId, TRUE);
SetForegroundWindow(hwndForce);
AttachThreadInput(nCurProcessId, nMyProcessId, FALSE);
}

if (GetForegroundWindow() != hwndForce)
SetForegroundWindow(hwndForce);
}


"Andrea Mariottini" <a.mariottini (AT) libero (DOT) it> wrote in message
news:BA926770.7C0C%a.mariottini (AT) libero (DOT) it...
I'm trying to use SetForegroundWindow API to show the main
window of my application when it is not in foreground.

It doesn't work!

The applications flashes in the taskbar but doesn't go
in foreground.

The call works only if my application is first minimized.

(System: VFP7 SP1 + Win2000 Pro SP3)

--
Andrea Mariottini






Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.