Technical Journal

Programming, software technologies, operating systems, IT

Enum to List

I’ve found this method useful lately so I thought I should add it here where everyone can see it.
It is meant to be used with the .NET Compact Framework. In the full framework this would have been much easier -
without reflection.
What the method does is that it adds the enum’s fields to a generic list of the same type T as the enum.

public static List EnumToList( )
{
Type enumType = typeof( T );

if( enumType.BaseType != typeof( Enum ) )
{
throw new ArgumentException( “Not an enum type!” );
}

List enumFieldList = new List( );

FieldInfo[ ] enumFields = typeof( T ).GetFields( );

foreach( FieldInfo fieldInfo in enumFields )
{
if( !fieldInfo.IsSpecialName )
{
enumFieldList.Add( (T) Enum.Parse( enumType, fieldInfo.Name, false ) );
}
}

return enumFieldList;
}

April 24, 2009 Posted by andocs | Windows CE | , , , , | No Comments Yet

Zeitgeist

This movie contains some thinghs that I’ve already aknowledged during time and others I just found out. I think we should see all sides of our social existence and not just the one we are being served. I am not saying that everything here is the truth, but it certainly looks like the truth. The zeitgeist does not look like a heaven for the ordinary people.

http://video.google.com/videoplay?docid=-594683847743189197

April 12, 2009 Posted by andocs | Uncategorized | | No Comments Yet

Windows CE Full Screen applications

The first task I had to do when I started developing my first Windows CE application was to make my main program window appear in full screen mode. After some research I came across a small article posted on Code Project. It describes a method of showing a window in full screen mode using standard window management calls which is what I was looking for. I completed the source with proper definitions of system calls and structures. I provide this source code along with a basic test application.

            The usual method of doing this would have been to use SHFullScreen, but I was not able to use this function as my OS Design was not to contain aygshell.dll, the library from which this function was part of.

            I am developing under .NET Compact Framework 2.0.

            First, I had to define some useful enums containing constants of the Windows API. Here are some of them, for the rest please see the attached project as this post would grow unreasonably if I pasted them here.

      [Flags( )]

      internal enum FullScreenFlags : int

      {

            SwHide = 0,

            ShowTaskbar = 0×1,

            HideTaskbar = 0×2,

            ShowSipButton = 0×4,

            HideSipButton = 0×8,

            SwRestore = 9,

            ShowStartIcon = 0×10,

            HideStartIcon = 0×20

 

      }

 

      internal enum Hwnd : int

      {

            HWND_TOP = 0,

            HWND_BOTTOM = 1,

            HWND_TOPMOST = -1,

            HWND_NOTOPMOST = -2

      }

 

            A very useful thing to do is to check www.pinvoke.net and retrieve the values of certain constants that you would make use of in various situations.

            Next, I had to implement a static class that imports my needed functions from coredll.dll. This dll is very useful to get acustomed with because in it you will find system calls that you are very likely to need in your development process under .NET CF. So, here is what you need:

 

      public static class Coredll

      {

            /// <summary>

            /// The function retrieves the handle to the top-level

            /// window whose class name and window name match

            /// the specified strings. This function does not search child windows.

            /// </summary>

            /// <param name=”lpClass”></param>

            /// <param name=”lpWindow”></param>

            /// <returns></returns>

            [DllImport( "coredll.dll", SetLastError = true )]

            public static extern IntPtr FindWindowW( string lpClass, string lpWindow );

 

            [DllImport( "coredll.dll", SetLastError = true )]

            public static extern bool ShowWindow( IntPtr hwnd, int state );

 

            [DllImport( "coredll.dll", SetLastError = true )]

            public static extern bool SetWindowPos( IntPtr hwnd, IntPtr hwndAfter,

                                                                              int xPos, int yPos, int cX, int cY, int wFlage );

 

            [DllImport( "coredll.dll" )]

            public static extern int GetSystemMetrics( int smIndex );

 

            [DllImport( "coredll.dll", SetLastError = true )]

            [return: MarshalAs( UnmanagedType.Bool )]

            public static extern bool SystemParametersInfo( Spi uiAction, uint uiParam, IntPtr pvParam, Spif fWinIni );

 

            [DllImport( "coredll.dll", SetLastError = true )]

            [return: MarshalAs( UnmanagedType.Bool )]

            public static extern bool SystemParametersInfo( Spi uiAction, uint uiParam, String pvParam, Spif fWinIni );

 

            [DllImport( "coredll.dll", SetLastError = true )]

            [return: MarshalAs( UnmanagedType.Bool )]

            public static extern bool SystemParametersInfo( Spi uiAction, uint uiParam, ref AnimationInfo pvParam, Spif fWinIni );

 

            [DllImport( "coredll.dll", EntryPoint = "SystemParametersInfo", SetLastError = true )]

            public static extern bool SystemParametersInfoGet( uint action, uint param, ref uint vparam, uint init );

 

            [DllImport( "coredll.dll", EntryPoint = "SystemParametersInfo", SetLastError = true )]

            public static extern bool SystemParametersInfoSet( uint action, uint param, uint vparam, uint init );

 

            /// <summary>

            /// to find whether you are running on a Smartphone or a Pocket PC

            /// </summary>

            /// <param name=”uiAction”></param>

            /// <param name=”uiParam”></param>

            /// <param name=”pvParam”></param>

            /// <param name=”fWinIni”></param>

            /// <returns></returns>

            [DllImport( "Coredll.dll", EntryPoint = "SystemParametersInfoW", CharSet = CharSet.Unicode )]

            public static extern int SystemParametersInfo4Strings( uint uiAction, uint uiParam, System.Text.StringBuilder pvParam, uint fWinIni );

      }

           

            Now, to put things toghether here are the two methods needed to enter full screen mode and end it:

 

            public static void StartFullScreen( Control control )

            {

                  IntPtr hWndInputPanel = Coredll.FindWindowW( “SipWndClass” , null );

                  IntPtr hWndSipButton = Coredll.FindWindowW( “MS_SIPBUTTON”, null );

                  if( hWndInputPanel != null ) Coredll.ShowWindow( hWndInputPanel, SW_HIDE );

                  IntPtr hWndTaskBar = Coredll.FindWindowW( “HHTaskBar”, null );

 

                  IntPtr hWnd = control.Handle;

 

                  if( hWndTaskBar != null ) Coredll.ShowWindow( hWndTaskBar, (int) Sw.SW_HIDE );

                  if( hWndInputPanel != null ) Coredll.ShowWindow( hWndInputPanel, (int) Sw.SW_HIDE );

                  if( hWndSipButton != null ) Coredll.ShowWindow( hWndSipButton, (int) Sw.SW_HIDE );

 

                  Coredll.SetWindowPos(

                        hWnd, (IntPtr) ( (int)Hwnd.HWND_TOPMOST ), 0, 0,

                        Coredll.GetSystemMetrics( (int) SystemMetric.SM_CXSCREEN ),

                        Coredll.GetSystemMetrics( (int) SystemMetric.SM_CYSCREEN ),

                        (int) Swp.SWP_SHOWWINDOW );

            }

 

            public static void StopFullScreen( Control control )

            {

                  //IntPtr hWndInputPanel = Coredll.FindWindowW( “SipWndClass” , null );

                  IntPtr hWndSipButton = Coredll.FindWindowW( “MS_SIPBUTTON”, null );

                  IntPtr hWndTaskBar = Coredll.FindWindowW( “HHTaskBar”, null );

 

                  IntPtr hWnd = control.Handle;

 

                  Rect rtDesktop = new Rect( );

 

                  if( hWndTaskBar != null )

                  {

                        Coredll.ShowWindow( hWndTaskBar, (int) Sw.SW_SHOW );

                  }

                  //Never forcibly show the input panel

                  //if( hWndSipButton != null )

                  //{

                  //    Coredll.ShowWindow( hWndSipButton, Sw.SW_SHOW );

                  //}

 

                  IntPtr rectPtr = Marshal.AllocHGlobal( Marshal.SizeOf( rtDesktop ) );

 

                  if( Coredll.SystemParametersInfo( Spi.SPI_GETWORKAREA, 0, rectPtr, Spif.None ) ) )

                  {

                        Rect rect = (Rect) Marshal.PtrToStructure( rectPtr, typeof( Rect ) );

 

                        Coredll.SetWindowPos( hWnd, (IntPtr) ( (int) Hwnd.HWND_TOPMOST ), 0, 0, rect.right – rect.left, rect.bottom – rect.top, (int) Swp.SWP_SHOWWINDOW );

                  }

                  Marshal.FreeHGlobal( rectPtr );

            }

 

            The first method, StartFullScreen, hides the SIP( Standard Input Panel ), the SIP button bar and the task bar leaving the entire screen to be filled by the application window. The application window’s size is set to the size of the entire screen in SetWindowPos. It’s that simple!

            When you want to exit full screen mode, you need to show the task bar, get the size of the client work area, specified with Spi.SPI_GETWORKAREA and then calculated the new size of the window( it would appear as a maximized window on your screen ).

You can download a sample project form 4shared: Source code here.

April 12, 2009 Posted by andocs | Windows CE | , , , , , , | 4 Comments

C++ UNREFERENCED_PARAMETER

I am currently developing a driver for one of our custom made devices under Windows CE and I came across this macro -

UNREFERENCED_PARAMETER in one of the driver examples I took as reference for developing my own. The definition itself does not say much:

#define  UNREFERENCED_PARAMETER(P) (P)

All it does is to layout the parameter or expression it has been passed. After some browsing on the internet I’ve found this post by Paul DiLascia:

http://msdn.microsoft.com/en-us/magazine/cc163805.aspx which I think greatly explains what is meant by compiling with Warning Level 4 – it made me a believer anyway.

Indeed, an unreferenced parameter would cause an error when compiling using level 4 and this is where UNREFERENCED_PARAMETER comes in handy.

For instance you have a function like this:

void foo( int arg1, float arg2 )

{

     UNREFERENCED_PARAMETER( arg1 );

}

Since you won’t be using arg1 and also you will compile with level 4, you just fool the compiler by passing arg1 as parameter to a macro that just adds the line:

arg1;

to your code. And since it is not used the compiler does not generate code for it and no efficiency or space is lost.

I will not repeat here stuff written in that post, just thought I’d mention it as a great post on C++ I’ve found.

April 7, 2009 Posted by andocs | Programming | , | No Comments Yet