Click here to Skip to main content
11,429,687 members (70,930 online)
Click here to Skip to main content
Technical Blog

Tagged as

Taskbar with Window Maximized and WindowState to None in WPF

, 7 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Taskbar with Window Maximized and WindowState to None in WPF

Say you want your WPF application to have no Title bar, and will be maximized to Full Screen, what do you first think of. It's the easiest to do.

  1. Set WindowStyle to Maximized: By this, the Window will not show up any title bar. The window style will be transformed to a Box with border in it.
  2. Set WindowState to None: By this, the window will be maximized to the whole screen and will show only the window and nothing else.

Oh.. hold on... hold on... As I say, it hides the whole screen, it means it even will not show up the Taskbar or any external gadgets applied to your desktop if it is not set to Always on Top attribute to it.

Yes, this is the problem that I faced recently, when people wanted me to show the TaskBar even though the application should be Maximized.

Is It a Bug ?

If I am not wrong, WPF builds application based on the Screen Resolution. It produces DPI independent pixels. If you specify it to be full screen, it first gets the Resolution of the screen, and draws the pixel based on its own algorithm. Hence, when you specify it to Maximized, it takes up the whole screen, as otherwise some portion of the screen will be hidden outside the range of the boundary.

When you restore a WPF screen, it will also recalculate the work area based on the distance between the resolution boundaries and resize itself accordingly. Now do you think Microsoft should really have an alternative state which show up the Taskbar as it does with normal Windows? I think yes.

The Solution

As I needed to do this, I have just tried out a few workarounds to this myself.

Following Lester's Blog on this issue, I have to use:

private static System.IntPtr WindowProc(
        System.IntPtr hwnd,
        int msg,
        System.IntPtr wParam,
        System.IntPtr lParam,
        ref bool handled)
{
    switch (msg)
    {
        case 0x0024:
            WmGetMinMaxInfo(hwnd, lParam);
            handled = true;
            break;
    }

    return (System.IntPtr)0;
}

private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
{            
    MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

    // Adjust the maximized size and position to fit the work area of the correct monitor
    int MONITOR_DEFAULTTONEAREST =0x00000002;
    System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

    if (monitor != System.IntPtr.Zero)
    {
                
        MONITORINFO monitorInfo = new MONITORINFO();
        GetMonitorInfo(monitor, monitorInfo);
        RECT rcWorkArea = monitorInfo.rcWork;
        RECT rcMonitorArea = monitorInfo.rcMonitor;
        mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
        mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
        mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
        mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
    }

    Marshal.StructureToPtr(mmi, lParam, true);
}

The call to API WmGetMinMaxInfo gets you the size of Maximize window for the current desktop.

[DllImport("user32")]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO   lpmi);
[DllImport("User32")]
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

The call to GetMonitorInfo gets you a MONITORINFO object and if you see the code carefully, it actually positions the window in such a way that it absolutely resizes itself to the height and width of the rectangular area.

To call this method, I can use SourceInitialized event which will eventually be called whenever the WindowState is modified.

void win_SourceInitialized(object sender, EventArgs e)
{
    System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
    WinInterop.HwndSource.FromHwnd(handle).AddHook
		(new WinInterop.HwndSourceHook(WindowProc));
}

Sounds good ?
Oh... let's give another easier solution that I have used. It's very simple.

Set properties for your window:

WindowStyle="None"
WindowState="Maximized"
ResizeMode="NoResize"

And go to your code and just resize your window based on PrimaryScreen width and height. Make sure you do set the Left and Top of the window as well.

this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
this.Left = 0;
this.Top = 0;
this.WindowState = WindowState.Normal;

Making WindowState.Normal will ensure that the default behaviour of the window is overridden and also makes the Taskbar Reappear.

I have included my sample application.

Have fun.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhishek Sur
Architect
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionNotr Working Pin
P. P. Piyumal17-Jan-14 20:31
memberP. P. Piyumal17-Jan-14 20:31 
Question[My vote of 1] A simple solution without interop Pin
tbayart6-Jun-13 7:25
membertbayart6-Jun-13 7:25 
Question[My vote of 1] simple but bad idea Pin
chprogmer24-Apr-12 13:20
memberchprogmer24-Apr-12 13:20 
AnswerRe: [My vote of 1] simple but bad idea Pin
Zaibot4-Jul-14 14:19
memberZaibot4-Jul-14 14:19 
GeneralMy vote of 1 Pin
xOOn@live.it27-Sep-10 23:23
memberxOOn@live.it27-Sep-10 23:23 
GeneralRe: My vote of 1 Pin
Vladimir Wrhel21-Oct-10 0:41
memberVladimir Wrhel21-Oct-10 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 7 Sep 2010
Article Copyright 2010 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid