ChiaFong

Posts Tagged ‘WPF’

Maximizing a borderless/no-caption window

In Programming on 5 February, 2009 at 7:00 am

When you choose to have a WPF application without border or caption, you need to provide your own maximize button and your own code to maximize the window.  Normally you can just do:

    this.WindowState = WindowState.Maximized;

and the window will be maximized.  But this call will cause the window to cover up the taskbar.  Thus we will need to set the maximum height and width of the window to a size where the window will not cover the taskbar before you call the above stage change.

    MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
    MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;

This will work at first glance until you test the application on a system with an extended secondary display that has a higher resolution than your primary display.  When you drag your application to the secondary display and maximize it, the height and width will be limited to the primary display’s height and width.  Thus you will see a blank area not cover by your application while it is maximized.  Another thing will happen is that if you try to expand your application while it is on the secondary display, the height and width will also be limited by that of the primary display.

To solve this I’ve immediately thought of finding a way to get the height and width of the secondary display and also to detect whether the application is on a secondary display.  This is all quite complicated.  Then something hit me, this problem only occur on an extended secondary display and on this kind of display there isn’t any taskbar, thus a much easier way is to detect whether the current display is a primary display and then set or reset the MaxHeight and MaxWidth accordingly.  Thus I implement 2 methods RestoreWindow() and MaximizeWindow().

    private void RestoreWindow()
    {
        // reset the max width and height so that you can expand
        // the window properly while on a secondary window
        MaxHeight = double.PositiveInfinity;
        MaxWidth = double.PositiveInfinity;
 
        // set the window back to normal state
        this.WindowState = WindowState.Normal;
    }
 
    private void MaximizeWindow()
    {
        // Get the current display, whether it is on primary or 
        // secondary
        System.Drawing.Point pt = 
                System.Windows.Forms.Cursor.Position;
        System.Windows.Forms.Screen currentScreen;
        currentScreen = 
                System.Windows.Forms.Screen.FromPoint(pt);
 
        // test whether the screen is a primary display
        if (currentScreen.Primary) 
        {
            // if it is a primary display, set the max width and
            // height so that the maximize window will not cover 
            // the taskbar
            MaxHeight = 
                SystemParameters.MaximizedPrimaryScreenHeight;
            MaxWidth = 
                SystemParameters.MaximizedPrimaryScreenWidth;
        }
        else
        {
            // if it is not a primary display, reset the max 
            // width and height
            MaxHeight = double.PositiveInfinity;
            MaxWidth = double.PositiveInfinity;
        }
 
        // set window to become maximized
        this.WindowState = WindowState.Maximized;
    }

Slow resizing?

In Programming on 8 October, 2008 at 7:00 pm

Ever feels that the WPF application that you are working on, seems to maximize or resize quite slowly?  If your system is not using a top end graphic card (e.g. on board graphic chip or Nvidia 9300 GS) the likely culprit might be the bitmap effect “dropshadow”.  Since bitmap effect is software rendered, turning on bitmap effect “dropshadow” on a large piece of UI graphic will be quite taxing on the graphic engine, thus resulting in somewhat slow performance when maximizing or resizing.  Turning these off will let you get a instantaneous performance boost.

If you have installed the Windows SDK and choose to install the WPF Performance Suite as well, you can make use of the Perforator which will let you see what part of your WPF app is rendered in software (if you’re using Vista, just do a Instant search for WPFPerf.exe).  If you have some performance issue with WPF application, this tool will be your friend, make full use of it!

Implementing that flat button but get into a clicking problem?

In Programming on 1 October, 2008 at 3:15 pm

Ever try to implement an IE or Firefox kind of flat button (it look like just a image of a toolbar but if the cursor hover over it a square will appear around the image and make it look like a clickable button) in WPF but get into problem because it turn out that clicking the button becomes difficult as you will need to point accurately on the button’s image to activate a hit.  This is most likely because you are using x:Null for the background so that when the mouse cursor is not hovering above the button, the button will appear like a normal image.  Alpha channel to the rescue.  Instead of using “No Brush” (i.e. x:Null), you should actually play with the alpha channel.  Color definition in WPF is more than just RGB, it is actually ARGB with “A” representing the alpha channel.  Think of alpha channel as a opacity value, thus in hexadecimal, FF is full opaque and 00 is totally transparent.  Thus the background of the intended flat button should be declare as #00FFFFFF which is asking WPF to use a transparent color (when A is declare as 00, the value of RGB is actually not that important, as any value there will still mean that the resulted color is a transparent color).  This way even when the mouse cursor is on a transparent background (non-image area), it will still score a hit on the flat button.

To bind or not to bind (Part 2)

In Programming on 25 July, 2008 at 7:00 am

To expand on what I’ve mentioned in my previous post.  The list box declared in XAML will look something like this:

<ListBox
    x:Name="CheckList"
    ItemTemplate="{DynamicResource CheckListTemplate}"
    ItemsSource="{Binding Source={StaticResource CheckedSource},
                  Mode=Default}">
</ListBox>

Under the <Windows.Resources>, the data template will be declared as:

<DataTemplate x:Key="CheckListTemplate">
  <StackPanel Margin="0,3,0,2" Orientation="Horizontal">
    <CheckBox IsChecked="{Binding Path=IsShown}"/>
    <TextBlock Text="{Binding Path=Name}"/>
  </StackPanel>
</DataTemplate>

At the same place or inside a style XAML’s ResourceDictionary, the data source is declared as:

<source:SColorList x:Key="CheckedSource"/>

Inside the C# file the classes are declared as:

    public class SColor : INotifyPropertyChanged
    {
        private string displayName;
        private Color clrShow;
        private bool permanent;
        private bool show;
 
        public bool IsShown
        {
            set
            {
                show = value;
                OnPropertyChanged("IsShown");
            }
            get
            {
                return show;
            }
        }
 
        public string Name
        {
            set
            {
                displayName = value;
                OnPropertyChanged("Name");
            }
            get
            {
                return displayName;
            }
        }
 
        public event PropertyChangedEventHandler 
            PropertyChanged;
 
        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new 
                                PropertyChangedEventArgs(info));
        }
    }
 
    public class SColorList : 
        ObservableCollection<SColor>
    {
 
    }

Under an initialization method, you should then do the following:

        SColorList colorList = 
            (SColorList)FindResource("CheckedSource"); 

After that is done, the binding is complete and you can then add data item into the colorList and it will show up in the list box control.  And the best thing is your designer can do “magic” on the DataTemplate.

To bind or not to bind

In Programming on 24 July, 2008 at 7:00 pm

One feature of WPF is to bind a data providing class to a data receiving control, Microsoft decided to call it data-binding.  Data-binding is a strange topic for developer coming from C++, MFC background.  For them when you create a control, you will then needs to manually write code to add data to the control, there is no magic there.  In WPF, magic is created once you use data-binding, when the data providing class refresh with new data added, the control will automatically get updated to show all the new data items.  To accommodate developer who don’t understand much about data-binding, WPF with C# still provides the traditional way of adding data to a user control.  This is OK until you bring in the designer.

Let’s see an example.  For some reason WPF does not support checked list box, however customizing one from the vanilla ListBox is not that difficult.  I’ve seen developer recommending that since list box can take any control item, thus when you add item, just new a CheckBox item, add that to the ListBox and you have a checked list box.  This is correct as long as no visual changes is going to be done to the CheckBox item.  If you are going to change the square check box into something more fanciful and you want your designer to do that for you, you’re out of luck.  Since these check box items are created in the code and not on the XAML, designer can’t see it in the Microsoft Expression Blend and thus can’t change them.  Global styling you say?  What if in your UI there is another check box item and you don’t want them to look the same?

Data-binding can help you here.  Create a DataTemplate in your XAML file, bind it to the ItemTemplate of the ListBox control and bind the ItemSource to the data class which inherit from the ObservableCollection class.  Now you can customize the DataTemplate to consist of a check box and a text label and since this is created in a XAML file, designer can see it and thus can reshape to whatever she wants.

Wow! Been a while!

In Programming on 8 December, 2007 at 5:46 am

Yeah, how time flies!  Near the end of the year already.  Visual Studio 2008 has finally been released just 2 or 3 weeks ago.  It is definitely a must for any .NET 3.0 developer to check out VS2008.  With its built-in XAML editor whereby you can finally drag and drop UI controls from the toolbox, this is definitely a big step forward compared to VS2005.

I’ve also developed a VisionBoard program using WPF.  It is a software that let you pin any pictures on to its board like surface.  This is so that you can pin pictures of your goals that you want to achieve and thus remind you of what your goals are.  The installation file is small, but I need to find a way to store those installation file.  So stay tuned!

.NET Framework 3.0 (formerly WinFX) has RTM

In Programming on 7 November, 2006 at 6:31 am

This is really going to be a golden week.  After Microsoft’s latest development platform, .NET Framework 3.0 RTM, tools for developing application that make use of this platform will RTM soon.  Windows Vista arrival will also be imminent.

Applications = Code + Markup

In Programming on 9 September, 2006 at 1:30 pm

Charles Petzold is at it again.  I have just finished Chapter 2 and I am hook!  After playing around with Windows Presentation Foundation for nearly a year, he still managed to give me a whole new perspective on WPF programming.  With just 2 chapters under my belt, I have already learn much more about the Window class.  LinearGradientBrush class is also now much more clearer to me.  I believe I have made a good investment on this book and it will definitely help me on my way to WPF nirvana!

Orcas CTP and Expression suite are now available

In Programming on 9 September, 2006 at 1:00 pm

Finally, tools that are compatible with Windows Vista RC1, .NET Framework 3.0 RC1 and Windows SDK RC1 are now available.  Orcas is now a real gem to use, especially the included Cider (the Visual Designer tools for Windows Presentation Foundation) which won’t slow down your Visual Studio anymore.  I am now quite satisfy with Cider performance and it can finally display my application’s GUI in its designer window.

Other tools of interest to any .NET Framework 3.0 programmer are Expression Interactive Designer, Expression Graphic Designer, and Extensions for Windows Workflow Foundation.  Enjoy!

Release Candidate for Windows SDK and .NET Framework 3.0

In Programming on 2 September, 2006 at 6:00 pm

Hot on the heels of Windows Vista RC1 (or is it the other way around?), Microsoft has released the set of tools that developers can use on Windows Vista RC1 (or use to develop applications that can work on Windows Vista RC1).

The Windows SDK has been updated to be compatible with RC1 of Windows Vista.  Hopefully, all the header files that had been breaking since Windows Vista interim build 5483, has been fixed and applications build using Windows SDK July CTP will not crash on Windows Vista RC1 (they have been crashing on interim build 5536).

The .NET Framework 3.0 has been bump up to Release Candidate status.  Thus any developer that feels more comfortable in Windows XP (hmmm….. that’s a thought) can install this build of .NET Framework 3.0 (together with Windows SDK RC1) and still be able to program applications that can work on Windows Vista RC1.

Strangely, the Visual Studio 2005 extension for .NET Framework 3.0 (codename Orcas) has not been updated to support RC of .NET Framework 3.0.  It has been missing ever since Windows Vista July CTP and .NET Framework 3.0 July CTP.  I hope this will be release soon.

Lastly, designers who wish to get onto the RC1 bandwagon, have to wait patiently.  This is because Microsoft Expression suite of applications has not been release for RC1 just yet.  Soon.  I hope.