Dan Rigsby - Coding Up Style

.Net, C#, & Wcf Development

Archive for the 'WPF' Category


Remove Icon from WPF Window

Posted by Dan Rigsby on 26th May 2008

In general, a window should always have an icon because it helps give context to the window.  However, there are occasions when you may want a window with icon such as a custom tool window or some other dialog. In Windows Forms if you want a window without an icon, you could just simply set:
 
this.ShowIcon = false;
 
In WPF, there is no simple way to just remove the icon.  If you try to just set the Icon to null, it will display the default windows icon.  The only way to disable the icon is to use some interop code to remove the icon directly through the windows API:
 
using System;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;

public partial class WindowWithoutIcon : Window
{
    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hwnd, int index);

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

    const int GWL_EXSTYLE = -20;
    const int WS_EX_DLGMODALFRAME = 0x0001;
    const int SWP_NOSIZE = 0x0001;
    const int SWP_NOMOVE = 0x0002;
    const int SWP_NOZORDER = 0x0004;
    const int SWP_FRAMECHANGED = 0x0020;
    const uint WM_SETICON = 0x0080;

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);

        // Get this window's handle
        IntPtr hwnd = new WindowInteropHelper(this).Handle;

        // Change the extended window style to not show a window icon
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);

        // Update the window's non-client area to reflect the changes
        SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    }
}
 
 

Posted in Common Libraries, WPF | 3 Comments »

Margins and Padding in WPF

Posted by Dan Rigsby on 20th May 2008

Margins and padding WPF work much like margins and padding other paradigms such as windows forms or CSS.  Margins give you space outside of the control, and padding gives you space inside the control.  The amount of spacing is set by the number of pixels you want to represent.

boxdim

Note: The padding always occurs inside the border. So if you have a border of 2 and padding of 10, the actual content of the control will be 12 pixels in.

In WPF, when you represent the spacing for these, you can set the values in line.  For instance, if you want to place a 2 pixel margin around a control you could write it as Margin=”2″, Margin=”2, 2, 2, 2″, or Margin=”2 2 2 2″.  This is very similar to CSS, but there is a big difference.  In CSS, the values represent “Top, Right, Bottom, Left”, but in WPF, the values represent”Left, Top, Right, Bottom”.  Why did WPF break the mold on this one?   Even if the order made more sense (which it doesn’t), I still can’t agree with this change.  CSS is a standard and has set a precedence.  Designers and developers who are using WPF have probably worked with CSS in some context.  This is going to throw a lot of people off.  Especially if the are going back and forth between CSS and WPF. What about developers who are writing silverlight controls as well as the web pages that contains them?

ClockThe CSS format makes sense. What time is represented by the clock to the right?  With no numbers visible, your eyes will probably start at the top (in the 12 position) and move around in clockwise motion to interpret each number.  Our minds have been trained to look at the top and work around clockwise.  There are exceptions to this, but it is a pattern that makes sense, and one that the CSS guys though was important enough to use in the order of sides. 

Its too late to change WPF now, but I would really like to know what the rational behind this was.  Maybe they chose to start with the Left side because we read Right to Left in most Western cultures.  However, many cultures read Right to Left or Top to Bottom or some other format.  It’s not a reliable paradigm to be based on.

Posted in WPF | 8 Comments »