How do I put a full screen window on top in C# after screen is cloned?

0

I am working on a C# WPF application that uses two screens. In the application the user is able to clone or extend the screen depending on what the user want to do. This is done in windows 7 and is using the following code:

[DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern long SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint    numModeArrayElements, IntPtr modeArray, uint flags);

UInt32 SDC_TOPOLOGY_INTERNAL = 0x00000001;
UInt32 SDC_TOPOLOGY_CLONE = 0x00000002;
UInt32 SDC_TOPOLOGY_EXTEND = 0x00000004;
UInt32 SDC_TOPOLOGY_EXTERNAL = 0x00000008;
UInt32 SDC_APPLY = 0x00000080;

public void CloneDisplays()
{
    SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_CLONE));
}
public void ExtendDisplays()
{
    SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_EXTEND));
}

Now to my problem. When using the above code I manage to clone/extend the screen. However, after this is done the taskbar at the bottom of the screen is in front of the full screen application which should not be the case. How do i put the application window back at the top?

Additional information: When I start the application it starts in fullscreen with the taskbar behind the application. This is done by setting the following:

WindowState="Maximized"
WindowStyle="None"

And this is what I want after the clone/extend has been done. Thanks

Edit: I have noticed that after I clone/extend the screen and sleep for say 5 seconds everything works as it should. However, as soon as the 5 seconds is over and the function exits the taskbar gets on top. Therefore it seems that I can not change something right after the clone/extend because the taskbar will always get on top in the end. So somehow I have to figure out how to stop the taskbar to behave like this, instead of changing the property of the window.

c#
wpf
asked on Stack Overflow Nov 24, 2014 by FewWords • edited Nov 24, 2014 by FewWords

3 Answers

0

Try setting the width and height of the WPF window as follows, You could set this within window constructor.

Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Height = System.Windows.SystemParameters.PrimaryScreenWidth;

To hide the taskbar try setting,

Width = System.Windows.SystemParameters.FullPrimaryScreenWidth;
Height = System.Windows.SystemParameters.FullPrimaryScreenHeight;
answered on Stack Overflow Nov 24, 2014 by Kurubaran • edited Nov 24, 2014 by Kurubaran
0

I'm already doing full-screen mode within my winforms applications, but i think you can do it more or less the same within WPF:

(this has to be different but similar in WPF):

form.WindowState = FormWindowState.Normal;
form.FormBorderStyle = FormBorderStyle.None;
form.Bounds = Screen.GetBounds(form);

Then the next step is to hide the task-bar if your application is on the primary screen:

if (Screen.PrimaryScreen.Equals(Screen.FromRectangle(Screen.GetBounds(form))))
{
    ShowWindowsToolbar(false);
}

And the method ShowWindowsToolbar() is implemented as follows:

[DllImport("user32.dll")]
private static extern int FindWindow(string lpszClassName, string lpszWindowName);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;

public void WindowsToolbar(bool visible)
{
    int hWnd = FindWindow("Shell_TrayWnd", "");
    ShowWindow(hWnd, visible ? SW_SHOW : SW_HIDE);
}

That's the way, how most of the tools out there support this kind of stuff. Also note, that this mode can mostly entered/leaved by pressing F11. So it would be good, if you also support this keystroke.

answered on Stack Overflow Nov 24, 2014 by Oliver
0

Turns out all I have to do is update the dispatcher queue and force it to do the update right after the clone/extend has been done. Then I can update the window properties.

public void ExtendDisplays()
{
    SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_EXTEND));
    this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); //Force update
    current_window.hide();
    current_window.show();
}
answered on Stack Overflow Nov 24, 2014 by FewWords

User contributions licensed under CC BY-SA 3.0