5/07/2013

Rule for using c++ 0x auto?

'auto' is one of popular keywords in c++ 0x. Now, one of envy lists, which we feel when we see c# or such as script languages, can be reduced. The most typical example of using is when declare 'iterator' in STL.
//when declared typical iter
for (map&ltstring, int>::const_iterator iter = MapScores.begin(); iter != MapScores.end(); ++iter)
{
    cout << "Name = " << iter->first << ", Score = " << iter->second << endl;
}
 
//when used auto
for (auto iter = MapScores.begin(); iter != MapScores.end(); ++iter)
{
    cout << "Name = " << iter->first << ", Score = " << iter->second << endl;
}
(In first one, instead of 'const_iterator', using of cbegin(), cend() is also fine for const appliance. cbegin(), cend() are also added containers of c++ 0x.)

The problem is that lots of c++ developers are already used for one rule, which type of variables always have to be declared. Therefore, inordinate using of 'auto' might bring about confusing and annoying. Furthermore, because of operation through misunderstanding, proportion of debugging time may be longer.

So, I make a rule for me.

  1. when immediately know 'return type' like above code and such as 'for', 'while' loop, when use local variable which available in short scope only.
  2. As much as possible, use 'const auto'. So that, if possible, it uses for 'read only'. I think it can aid to protect wrong operation by means of misunderstanding.

Below link is a stackoverflow thread which discussed for 'guideline using auto'.

http://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c0x-auto-keyword
Personally, I believe that the use of at least is better way.

5/06/2013

The best way to appear dialog invisibly when dialog is created

For this, 'WM_WINDOWPOSCHANGING' is used.

If we use WM_CREATE or WM_INITDIALOG and then, call ShowWindow(SW_HIDE),
we will firstly see a dialog, after that, it will be disappear.

Below code shows how to use WM_WINDOWPOSCHANGING in WTL.

LRESULT CHideDialog::OnWindowPosChanging(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
 LPWINDOWPOS lpwndpos = (LPWINDOWPOS)lParam;
 if (FALSE == m_isVisible)
 {
  lpwndpos->flags &= ~SWP_SHOWWINDOW;
 }
 else
 {
  lpwndpos->flags |= SWP_SHOWWINDOW;
 }
 return 0;
}

As we see, the value of  m_isVisible decides on whether dialog is visible.
So, if we call below function, after setting the value of m_isVisible to TRUE,
the dialog will be appear again.

::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

5/02/2013

How can I make a transparent window in WPF?

We will make a transparent window, which can be adjusted the opicity by slider control, llike below picture.



To begin with, it is required to adjust 3 of properties. They are 'AllowsTransparency', 'Background' and 'WindowStyle'.
<Window x:class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" 
        AllowsTransparency="True" Background="Transparent"
        WindowStyle="None" >


And then, the opacity of window bind to slider control, like this,
<Window x:class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" 
        AllowsTransparency="True" Background="Transparent"
        WindowStyle="None"
        Height="550" Width="400" Opacity="{Binding Value, ElementName=slider}" >

Here is all xaml code.
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" 
        AllowsTransparency="True" Background="Transparent"
        WindowStyle="None"
        Height="550" Width="400" Opacity="{Binding Value, ElementName=slider}" >
    <Border BorderBrush="Gray" BorderThickness="1" CornerRadius="6" Background="Gray">
        <Border BorderBrush="White" BorderThickness="1" CornerRadius="6">
            <DockPanel Background="#303030" Margin="2,2,2,2" >
                <Border Height="100" MouseMove="Header_MouseMove" DockPanel.Dock="Top" VerticalAlignment="Top">
                    <Button  Click="Button_Close" HorizontalAlignment="Right" Margin="0,0,0,80" Width="15" Height="20" VerticalAlignment="Bottom" BorderThickness="1">X</Button>
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,2">
                            <GradientStop Color="#C0C0C0" Offset="0." />
                            <GradientStop Color="#303030" Offset="0.1" />
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
                <Slider x:Name="slider" Height="20" DockPanel.Dock="Bottom" Maximum="1" Minimum="0.5" TickFrequency="0.2" Value="0.9"/>
                <Border Height="100" MouseMove="Header_MouseMove" DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,18">
                            <GradientStop Color="#303030" Offset="0." />
                            <GradientStop Color="#C0C0C0" Offset="0.1" />
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
            </DockPanel>
        </Border>
    </Border>
</Window>