ChiaFong

Posts Tagged ‘Programming’

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;
    }
Advertisements

Problem when you blog about source code

In Personal on 29 July, 2008 at 7:00 pm

The 2 main issues that will bog you down is that:

  1. how do you get the source code to indent properly
  2. and how do you get the style to show up properly, specifically the line spacing, the color for keyword and so on

I know that it can be solved by defining some CSS code but do you have to do that for every site that you change to?

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!

Transparency is finally easily enabled in XAML

In Programming on 14 July, 2006 at 3:00 pm

With the release of the June CTP .NET Framework 3.0, transparent background and window with rounded corners can finally be easily tweaked in XAML.  Lauren Lavoie, from Microsoft, blog about how you can go about doing it.  The trick is in Window.AllowsTransparency, just set this property to “True” and then setting the Window.Background to “Transparent” will get you a transparent window.  With the whole background being transparent you can then achieve rounded-corner window by drawing a Border with a CornerRadius defined.  Below is a snippet of the XAML code that Lauren did:

and the result:
Don't forget to check out her blog.

More OO?

In Programming on 12 July, 2006 at 3:48 pm

Can a language be more object-oriented (OO) than another language?  OO is a programming paradigm, a style of programming.  This style of programming emphasizes the following concepts:

  • Class – a unit definition of something that belong in a set, Dogs can be considered as a class.
  • Object – an instance of a class, a runtime manifestation of a class.  My wife’s favourite dog, Zombie, would be an instance for the class Dogs.
  • Method – way to interact with an object of some class.  Dogs can contain a method call ActCute, so to ask Zombie to act cute, you can use this method.
  • Inheritance – way to define a class as having all attributes of another class, this kind of class thus defined would normally be call a subclass.  Pomeranian can be a subclass of Dogs.  Zombie will then be more correctly instantiated as Pomeranian, as it is of that kind of breed.
  • Encapsulation – way of hiding the implementation detail of an object.  You can ask Zombie to ActCute but you won’t know its do that.
  • Abstraction – way to ignore details of an object’s subclass and work at a more generic level.  Zombie can be considered as a Pomeranian most of the time, but when needed it can be abstracted as a Dog.
  • Polymorphism – a way whereby an object can dynamically change its behaviour during runtime.  If you think of Zombie as a Dog then it should be able to guard the house, but if you then know that Zombie is actually a Pomeranian, you would know that Zombie is more for play.

A language can be consider OO, as long as it provide mechanisms to support most of the above concepts, so that OO developers can program at ease.  However, certain language might choose not to support certain concept and thus can be consider “less OO”.  For example, Visual Basic does not support inheritance and Java does not support multiple-inheritance which is a more complex mechanism of inheritance.

Advantages and Disadvantages of C# as compared to C++

In Programming on 11 July, 2006 at 3:00 pm

Advantages:

  1. While no programming language lets programmers write entirely bug-free programs, C# goes a long way in comparison with C and C++.
  2. With Windows Form and later on WPF (especially with the aid of Expression Interactive Designer), C# is great for Rapid Application Development (RAD).
  3. C# is safer to run.  Since C# program is compiled into an intermediate language, the OS can always check it to see that no malicious code is about.
  4. C# combines the old and the new in an almost perfect balance.  C# duplicates much of the concise syntax of C and also adds modern, object-oriented features while retaining very little unnecessary baggage from C.
  5. Cost of maintenance for C# is definitely much lower than that of C++.  This is a positive side effect of C# helping programmers to write program that is as bug free as possible.
  6. C# can make use of every feature available in WPF, making it one of the languages that can work perfectly with WPF.

Disadvantages:

  1. C# is slower to run.  This is somewhat taken care of when using WPF, although currently the launching of WPF application is still a bit slow.  However, after the program is launched, the animation effects are all very smooth.
  2. C# is less flexible than C++.  C# depends greatly on .NET framework, anything that is not found in the .NET framework will be difficult to implement.

The 4 Concepts

In Programming on 28 June, 2006 at 3:00 pm

In trying to design a great software with great User Experience (UX) most developers and designers will normally follow 4 great concepts.  They are:

  1. Functional
  2. Aesthetic
  3. Interaction
  4. Process

By using these 4 concepts, you will be able to generate great memories of using the software and thus create an emotional connection.

The software has to be functional, it must achieve all the objectives of the user and in a way that is elegant and advantageous to the user. A knife can open a can which meet the objective of the user but it is better to use a can opener.  Most importantly the user must feel that the software works great!

The software should be beautiful.  Human will naturally be attracted to things that are beautiful.  A set of black and dull speakers can serve it purpose of giving great sound effect but a set of great looking speakers will add to the warmth of the room and at the same time give you great sound effect.  Most importantly the user must feel that the software look great!

The software should create interaction.  A coffee machine that make 2 cups of coffee at one go helps to create togetherness.  You and your friend can enjoy coffee together without the need to wait.  Your coffee will not get cold while waiting for your friend’s coffee to be made.  Most importantly the user must be able to relate to your software!

The software should take care of the process of getting the user to her objectives.  Imagine that you are going for a vacation in a special hotel.  You interact with the hotel’s online reservation system, it is a joy to use.  You then took a cruise to the island where the hotel is situated.  When you arrive at the island you find the scenery is so beautiful.  And when you finally arrive at the hotel you are floored by the service that you received.  This create a whole experience of the process in getting to the hotel.

How to use that Aero Glass API?

In Programming on 19 May, 2006 at 3:00 pm

I have a comment asking me “how do i add instal the shell api on my xp machine”.  The answer to this question is actually quite simple; Just install the latest Windows SDK onto your XP machine and you will get the header file to those Aero API.

However, you can’t test your program under Windows XP; Those API will simply fail.  Desktop Window Manager’s API will only function under Windows Vista.  So you will need the latest compatible Windows Vista CTP build to verify your program.  Have fun!