×
Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 415808, Entry date: 07/31/2018
(4)
Rate

In WinCC / WinCC Professional Runtime, how do you bring the window of another program into the foreground?

  • Entry
  • Associated product(s)
You can use the Windows API function "FindWindow" to bring a window to the foreground with a C script.

1) Bring a window into the foreground (C script)

The task switching function is often blocked on operating units on which a WinCC PC Runtime is running. When you open programs in the Runtime, clicking the Runtime window causes the program that is still open to go to the background behind the Runtime window and you can no longer call it. Using the C script presented below you can bring windows from the background into the foreground.

1.1) The function "FindWindow"

You use the Windows API function "FindWindow" to bring a window into the foreground:

HWND WINAPI FindWindow(LPCSTR lpClassName , LPCSTR lpWindowName);

1.2) Parameters of the "FindWindow" function

  • The first parameter "LPCSTR lpClassName" is the name of theclass of the window.
  • The second parameter "LPCSTR lpWindowName" is the title of the window (title bar).

You can either specify one of the two parameters or both together.

1.3) Example 1: Bring Microsoft Access window to the foreground

1.3.1) A Microsoft Access window is brought into the foreground using the class:

HWND Handle= NULL;
Handle= FindWindow("OMAIN",NULL);
if(Handle)
{
ShowWindow(Handle, SW_MINIMIZE);
ShowWindow (Handle, SW_SHOWNORMAL);
SetForegroundWindow(Handle);
}
else
ProgramExecute("C:\\MSOffice.97\\Office\\ACCESS.EXE");

If the window is not found, then Microsoft Access is restarted.

1.3.2) A Microsoft Access window is brought into the foreground using the title:

HWND Handle= NULL;
Handle=FindWindow(NULL,"Microsoft Access -");
if(Handle)
{
ShowWindow(Handle, SW_MINIMIZE);
ShowWindow (Handle, SW_SHOWNORMAL);
SetForegroundWindow(Handle);
}
else
ProgramExecute("C:\\MSOffice.97\\Office\\ACCESS.EXE");

If the window is not found, then Microsoft Access is restarted.

1.4) Example 2: Bring the WinCC Professional Runtime window back into the foreground

HWND Handle = NULL;
Handle = FindWindow("PDLRTisAliveAndWaitsForYou",NULL);
if(Handle)
{
ShowWindow(Handle, SW_MINIMIZE);
ShowWindow ( Handle, SW_SHOWNORMAL );
SetForegroundWindow(Handle);
}


1.5) Example 3: Bring WinCC Explorer into the foreground (WinCC 5.x / WinCC V6.x / WinCC V7.x)

HWND Handle= NULL;
Handle= FindWindow("WinCCExplorerFrameWndClass",NULL);
if(Handle)
{
ShowWindow(Handle, SW_MINIMIZE);
ShowWindow ( Handle, SW_SHOWNORMAL );
SetForegroundWindow(Handle);
}


2) The classes of frequently used programs

2.1) Microsoft applications

Application

Name of the class in

Windows 2000/XP

Name of the class in

Windows 7

Name of the class in
Windows 8 /
Server 2008 R2
Name of the class in
Windows 10 /
Server 2012 R2

Access

OMAIN

OMAINOMAINOMAIN

Internet Explorer

IEFrame

IEFrameIEFrameIEFrame

Microsoft Excel

XLMAIN

XLMAINXLMAINXLMAIN

Microsoft Word

OpusApp

OpusAppOpusAppOpusApp

MS-DOS input prompt

ConsoleWindowClass

ConsoleWindowClassConsoleWindowClassConsoleWindowClass

Notepad (editor)

Notepad

NotepadNotepadNotepad

Paint

MSPaintApp

MSPaintAppMSPaintAppMSPaintApp

Calculator

SciCalc

CalcFrameCalcFrameCalcFrame
Windows ExplorerExploreWClassCabinetWClassCabinetWClassCabinetWClass

WordPad

WordPadClass

WordPadClassWordPadClassWordPadClass

Table 01

2.2) SIMATIC software

SIMATIC software

Name of the class

SIMATIC Managers7tgtopx

WinCCV4.x Control Center

MCPFrameWndClass

WinCC V5.x / V6.x / V7.x WinCC Explorer

WinCCExplorerFrameWndClass

WinCC Runtime Advanced V11..14

HmiRTm

WinCC Runtime Professional V11..14

PDLRTisAliveAndWaitsForYou

Table 02


3) Determine the class / Title of a window

If you want to bring an unlisted program into the foreground, you must first determine the name of the class of the window or the title of the window.

Note that when using the title of a window it might change when opening documents. Furthermore, with some programs the title is language-specific and so the window might not be found in a different language.

The class is independent of the language. That is why it is better to use the class of a window (parameter "lpClassName").

3.1) Determine the title

You can easily determine the title of a window by opening the corresponding program (without document). The window title is then shown in the title bar.

3.2) Determine the class

You can determine the class of a window using a compiler (for example: Spy from Visual C++), or by using a C script.

The C script below determines the name of the window class from the window name.
You can link the script to the event of a button, for example, to determine the class by using the WinCC Runtime / WinCC Runtime Professional.

Example 4: Determine the class of the "Calculator" application window with WinCC

To receive an output of the "printf()" function you can use a "GSC - Diagnostics" application window in the WinCC screen or start the "apdiag.exe" program.

#include "apdefap.h"
void ShowWindowClass()
{
char WindowClass[256];
HWND Handle = NULL;
Handle = FindWindow(NULL, "Computer");
GetClassName(Handle, WindowClass, sizeof (WindowClass)-1);
printf ("Window Class Name: %s \r\n", WindowClass);
}

Example 5: Determine the class of the window of multiple applications with WinCC Professional
To receive an output of the "printf()" function you can use a "Print job/Script diagnostics" control in the WinCC screen. The control can be found in the toolbox under "Controls".

The function has the transfer parameter "WindowTitle". Via the transfer parameter you can determine the class of any number of windows provided that the title is known in each case.
You can also work without transfer parameters, but then the title of the window must be specified in the code as in Example 4.

#include "GlobalDefinitions.h"
void ShowWindowClass(char* WindowTitle)
{
char WindowClass[256];
HWND Handle = NULL;
Handle = FindWindow(NULL, "WindowTitle");
GetClassName(Handle, WindowClass, sizeof (WindowClass)-1);
printf ("Window Title: %s", WindowTitle);
printf (",Window Class Name: %s \r\n", WindowClass);
}


4) Bring window permanently into the foreground

The following setting is required in Windows 7 to keep the application in the foreground:


Fig. 01

The setting can be found under "Start > Control Panel > Ease of Access Center > Make the mouse easier to use > Make it easier to manage windows". There you select the option "Prevent windows from being automatically arranged when moved to the edge of the screen".

Additional Keywords

Programming tips, 2522765, 108819546, TIA Portal PC Runtime, Script, on top, Macro
Rate entry
no rating
Requests and feedback
What do you want to do?
Note: The feedback always relates to the current entry / product. Your message will be forwarded to our technical editors working in the Online Support. In a few days, you will receive a response if your feedback requires one. If we have no further questions, you will not hear from us.