ixmx

Code snippets

C# Rect.IsEmpty


In C# System.Windows.Rect.IsEmpty doesn’t return what you think it would.
For example:

Rect r = new Rect();
r.IsEmpty

returns false.

Empty has a special meaning, a value that represents a rectangle with no position or area.
From MSDN
The empty rectangle, which has X and Y property values of PositiveInfinity, and has Width and Height property values of NegativeInfinity.

Avoid mathematical computations involving empty rectangles. Because both X and Y are infinite values, processor operations are severely impaired by these operations.

Comparisons involving these values will not affect application performance.

How to re-raise an event in a WPF adorner


protected override void OnMouseWheel(MouseWheelEventArgs e)
{
    this.AdornedElement.RaiseEvent(e);
}

Get a string that has current date time for file name creation


DateTime.Now.ToString("yyyyMMddHHmmss")
returns "20101216110856"

Annoying!

How to use #ifdef to differentiate 64bit and 32bit


Original Article from http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/2b24afa3-62ef-42d4-9aff-ca309588289c

If you want to use the _WIN64, _WIN32 macros, list the _WIN64 one first.

#if defined _WIN64

#elif defined _WIN32

#else

#endif

_WIN32 is defined for both 32 and 64 bit projects so you know that it will always be defined on a MSVC build.

_WIN64 on the other hand is only defined with a 64 bit compiler. So putting 64 first eliminates the need to check for both at once.

The final else is there for if your code is ever built with a different compiler which doesn’t predefine _WIN32 or _WIN64.

This way you can provide an option for that, but if you know it will never be built besides an MSVC compiler then you can just remove it.

crescens2k – http://c2kblog.blogspot.com/

How to reference C++/CLI Input Library correctly so that it builds on TFS build agent


In your C++/CLI Project,

Go to
   Properties/Configuration Properties/Linker/Input/Additional Dependencies/
      and clear the dependencies if you have specified them.
Go to
   Properties/Common Properties/Framework and References/
      click on Add New Reference and select the files or projects or other assemblies
      click on the newly added reference
         On the right hand pane under Project Reference, set Properties/Link Library Dependencies = true

WPF XAML xmlns: assembly not found problem


Solved by giving the custom Assembly a strong name or in other words sign the assembly.

  1. Goto Project/Properties/Signing
  2. Click “Sign the assembly” and add a key file to the assembly
  3. Rebuild the assembly and the designer will accept the namespace declaration without an error.

Reference

Remove Child Elements from WPF Panel


This Extension Method shows how to remove Rectangle elements from Panel container.
Many objects derive from Panel. There are six defined panel classes (Canvas, DockPanel, Grid, StackPanel, VirtualizingStackPanel, WrapPanel)


public static class MyExtentions
{
    public static void RemoveRectangles(this Panel panel)
    {
        var children = from UIElement c in panel.Children where c is Rectangle select c;
        foreach (UIElement u in children)
        {
            panel.Children.Remove(u);
        }
    }
}

Adding a System wide Hot Key in Win32


ATOM hotkeyID = GlobalAddAtom(_T("MyHotKeyCombination"));

if(!UnregisterHotKey(hWnd,hotkeyID))
{
   if (RegisterHotKey( hWnd, hotkeyID, MOD_ALT, 0x42))  //0x42 is 'b'
   {
       _tprintf(_T("Hotkey 'Alt+b' registered \n"));
   }
}
Handle the WM_HOTKEY message in WndProc
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_HOTKEY:
        {
            _tprintf(_T("WM_HOTKEY received\n"));
        }
        break;
    case WM_COMMAND:

.....

Cleaner


This is a small utility I created to clean any file type recursively in a folder on Windows

Download v1.3 Freeware

You will require .Net 3.5 runtime on your system.

How to use:

  • Start the application and browse to the folder or you can type in the path in the top text box and press enter.
  • It will show the file types in that folder and all sub folders on the right hand side.
  • Select the file type by putting a check against it
  • When you navigate the file type list, you will see all the files of that type on the middle pane.
  • When you are happy to clean, press clean and all the selected file types will be deleted to the Recycle Bin