• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2884
  • Last Modified:

Where to call AllowSetForegroundWindow( ASFW_ANY )

I have an application with different windows. I wanna change the focus of the windows in a loop.
Each active window I wanna set to foreground. The code works well but I can't set the window
to foreground. Where do I have to call AllowSetForegroundWindow ?

      POSITION templPos = this->GetFirstDocTemplatePosition();
      while (templPos) {
            CDocTemplate *templ= GetNextDocTemplate(templPos);
            POSITION docPos = templ->GetFirstDocPosition();
            while (docPos) {
                  CDocument* doc = templ->GetNextDoc(docPos);
                  if (doc->IsKindOf(RUNTIME_CLASS(CJATDoc))) {
                        CJATDoc* mydoc =(CJATDoc*)doc;
                                                      mydoc->doParse();
                        CView* view = mydoc->GetView();
                        view->SetActiveWindow();
                        view->SetFocus();
                        view->SetForegroundWindow();
                  }
            }
      }
Thanks in advance

Martin
0
martinmoessner
Asked:
martinmoessner
  • 6
  • 5
  • 3
  • +1
1 Solution
 
nonubikCommented:
You can call it in the InitInstance method of your CWinApp derived class.
0
 
martinmoessnerAuthor Commented:
Which includes I have to set ?
0
 
nonubikCommented:
#include <windows.h> if you did not have it in your stdafx.h
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
martinmoessnerAuthor Commented:
I've tried this but it seems not declared inside
0
 
nonubikCommented:
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows Me, Windows 2000
0
 
martinmoessnerAuthor Commented:
Cause I'm new in Visual C++ ...
Where to implement User32.lib ? Is it a linker option ?
Martin
0
 
nonubikCommented:
Go to project settings -> linker -> input -> type user32.lib

But what kind of project do you have (wizard generated)? I thought it's a MFC MDI app. What IDE you use, do you have WinME/2000 or above?
0
 
martinmoessnerAuthor Commented:
I have 2000  and have a wizzard generated MFC MDI app using RichEditView
I typed in user32.lib in the linker option but it doesn't help
Got the following error message while compiling ...
C:\DEVELOP\JAT\JAT.cpp(107) : error C2065: 'AllowSetForegroundWindow' : nichtdeklarierter Bezeichner
C:\DEVELOP\JAT\JAT.cpp(107) : error C2065: 'ASFW_ANY' : nichtdeklarierter Bezeichner
Martin
0
 
nonubikCommented:
You need to define _WIN32_WINNT as 0x0500 or greater in your stdafx.h
0
 
martinmoessnerAuthor Commented:
Doesn't help
0
 
rcarlanCommented:
What version of VC++ are you using? If it's VS6, you won't find AllowSetForegroundWindow in the header and library files distributed with it. You need to install the MS SDK and use its WinUser.h header and user32.lib library.

Alternatively, you can add this declaration in your stdafx.h header file:

typedef WINUSERAPI BOOL WINAPI (*AllowSetForegroundWindowFn_t)(DWORD dwProcessId);

and then do this (add error checking):

HMODULE hUser32 = LoadLibrary(_T("user32.lib"));
AllowSetForegroundWindowFn_t AllowSetForegroundWindow = (AllowSetForegroundWindowFn_t)GetProcAddress(hUser32, "AllowSetForegroundWindow");
AllowSetForegroundWindow(pid);  // pid is the process ID that you want to enable for SetForegroundWindow

Radu
0
 
rcarlanCommented:
Actually, on my system, the user32.lib belonging to VS6 does include AllowSetForegroundWindow. However, this may be the result of applying the latest service pack for VS6.

You may want to give this a try, as it's simpler (if it doesn't work - i.e. gives you a linker error - you may revert to the solution described in my previous post):

In stdafx.h add this declaration:

WINUSERAPI BOOL WINAPI AllowSetForegroundWindow(DWORD dwProcessId);

If your user32.lib does include AllowSetForegroundWindow, you can call it directly:

AllowSetForegroundWindow(pid);  // pid is the process ID that you want to enable for SetForegroundWindow


If you get linker errors, it means your user32.lib does not include AllowSetForegroundWindow. In this case you will have to use the LoadLibrary/GetProcAddress approach. Btw, you don't have to specify WINUSERAPI in the typedef - i.e. the following will do:

typedef BOOL (WINAPI *AllowSetForegroundWindowFn_t)(DWORD dwProcessId);

Radu
0
 
martinmoessnerAuthor Commented:
Declaration of AllowSetForeground  seems to work !  But ...                                                                                      

I call AllowSetForeground in the InitInstance method of your CWinApp derived class.

Set view to foreground doesn't still work. What do I make wrong ?
Test gives 0 as result value.

CJATView* view = (CJATView*)mydoc->GetView();
int test = view->SetForegroundWindow();
char buffer[100];
sprintf(buffer,"%i",test);
MessageBox(NULL,buffer,"Test",MB_OK);
view->SetActiveWindow();
view->SetFocus();
mydoc->doParse();


Martin
0
 
rcarlanCommented:
Which process calls AllowSetForegroundWindow?
What are you trying to achieve?

Radu

0
 
AndyAinscowCommented:
CWnd::SetForegroundWindow
BOOL SetForegroundWindow( );

Return Value

Nonzero if the function is successful; otherwise 0.

Remarks

Puts the thread that created the window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The foreground window is the window with which the user is currently working. The foreground window applies only to top-level windows (frame windows or dialogs boxes).

NOTE THE FINAL SENTANCE - top level windows only - you are trying to use it with a CHILD view
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now