Monday, October 5, 2009

WPF Command line arguments.


App.xaml
<Application ...
Startup="App_Startup">
...


App.xaml.cs;
...
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
// Storing your arguments in your type you wish:
public static string Input = "";

void App_Startup(object sender, StartupEventArgs e)
{
// Store the arguments to static public you declared:
Input = String.Join(" ", e.Args);
}
}
...


Window1.xaml.cs
...
/// <summary>
/// Create window.
/// </summary>
public Window1()
{
Console.WriteLine(App.Input); // Woohoo! Got the input...
...

See, MSDN, you don't have to be so damn verbose always, when little codespeak would do.

Saturday, October 3, 2009

Glass *only* fallback, in Windows.Forms (when aero disabled / in XP)


Usage: Create windows.forms application and take stuff from here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GlassingJustFallbackInForms
{
public partial class FormGlassedFallback : Form
{
public FormGlassedFallback()
{
InitializeComponent();
}

#region WINAPI Crap:
private bool isActivated;

protected override void OnActivated(EventArgs e)
{
isActivated = true;
base.OnActivated(e);
this.Refresh(); // Causes flickering, any ideas?
}

protected override void OnDeactivate(EventArgs e)
{
isActivated = false;
base.OnDeactivate(e);
this.Refresh(); // Causes flickering, any ideas?
}

protected override void OnPaint(PaintEventArgs e)
{
IntPtr thmdata = OpenThemeData(this.Handle, "WINDOW");
IntPtr hdc = e.Graphics.GetHdc();

// Not very efficient, but doesn't flicker:
RECT rect = new RECT(-2, 0, this.Width, this.Height);
// RECT rect = new RECT(e.ClipRectangle.Left-3, e.ClipRectangle.Top, e.ClipRectangle.Right+3, e.ClipRectangle.Bottom);

base.OnPaint(e);
DrawThemeBackground(thmdata, hdc, WP_FRAMELEFT, isActivated ? FS_ACTIVE : FS_INACTIVE, ref rect, 0);
}

private const int FS_ACTIVE = 1;
private const int FS_INACTIVE = 2;
private const int WP_FRAMELEFT = 7;

[DllImport("uxtheme", ExactSpelling = true)]
private extern static Int32 DrawThemeBackground(IntPtr hTheme, IntPtr hdc, int iPartId,
int iStateId, ref RECT pRect, int test);

[DllImport("uxtheme", ExactSpelling = true)]
private extern static Int32 DrawThemeBackground(IntPtr hTheme, IntPtr hdc, int iPartId,
int iStateId, ref RECT pRect, IntPtr pClipRect);

[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
private static extern IntPtr OpenThemeData(IntPtr hWnd, String classList);

private const int WM_PAINT = 15;

[Serializable, StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;

public RECT(int left_, int top_, int right_, int bottom_)
{
Left = left_;
Top = top_;
Right = right_;
Bottom = bottom_;
}
}
#endregion


}


}

Friday, October 2, 2009

WPF Window with aero glass background. (C# .NET)

Usage, in your Window constructor do:

 ...
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.GlassBackground();
}
}
...

Installation:
1.) Add reference "System.Drawing" to your Project (Right click on References folder on your project, and Add reference)

2.) Add to your project a class file (GlassExtensions.cs) replace the "YOURNAMESPACE" in with same namespace as in your Window1.xaml.cs, and paste this:


using System;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace YOURNAMESPACE
{
public static class GlassingExtension
{
/// <summary>
/// Sets glass background to whole window.
/// </summary>
/// <remarks>Remember to set your WPF Window Background to "Transparent"!</remarks>
/// <param name="win"></param>
public static void GlassBackground(this Window win)
{
// Glass extend WINAPI thingie http://msdn.microsoft.com/en-us/library/aa969512%28VS.85%29.aspx form more details
// If any of the margins is "-1" the whole window is glass!
win.GlassBackground(-1, 0, 0, 0);
}
/// <summary>
/// Sets glass background to custom margins in the window.
/// </summary>
/// <param name="win"></param>
public static void GlassBackground(this Window win, int left, int right, int top, int bottom)
{
// Why would you read the inner workings? Why? If you need to know why...
// DwmExtendFrameIntoClientArea http://msdn.microsoft.com/en-us/library/aa969512%28VS.85%29.aspx is the magical WINAPI call
// rest is just crap to get its parameters populated.
win.Loaded += delegate(object sender, RoutedEventArgs e)
{
try
{
// Obtain the window handle for WPF application
IntPtr mainWindowPtr = new WindowInteropHelper(win).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);

// Transparent shall be glassed!
mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Colors.Transparent;

// Margin for the DwmExtendFrameIntoClientArea WINAPI call.
NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();
margins.cxLeftWidth = left;
margins.cxRightWidth = right;
margins.cyBottomHeight = bottom;
margins.cyTopHeight = top;

// Glass extend WINAPI thingie http://msdn.microsoft.com/en-us/library/aa969512%28VS.85%29.aspx form more details
int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
if (hr < 0)
{
//DwmExtendFrameIntoClientArea Failed
} else {
win.Background = System.Windows.Media.Brushes.Transparent;
}
}
// If not glassing capabilities (Windows XP...), paint background white.
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = System.Windows.Media.Brushes.White;
}
};
}

#region WINAPI Crap, none should handle this in 21st century
private class NonClientRegionAPI
{
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth; // width of left border that retains its size
public int cxRightWidth; // width of right border that retains its size
public int cyTopHeight; // height of top border that retains its size
public int cyBottomHeight; // height of bottom border that retains its size
};


[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);

}
#endregion
}
}