Thursday, August 28, 2008

Getting a Device ID using the Compact Framework

Here are a couple of approaches I found for getting a Device ID using the compact framework. Both are lengthy solutions to something that should probably be built into platform.

http://msdn.microsoft.com/en-us/library/ms172516.aspx


http://msdn.microsoft.com/en-us/library/aa446562.aspx


http://blogs.msdn.com/jehance/archive/2004/07/12/181067.aspx

How to do a CDC.Escape() in .NET

There may be ways around using the old CDC Escape function from MFC, but when you are porting C++ to C#, it is nice to just call the same Win32api functions that you were using before. For printing, I used the Escape function to get information about the printer, but since this method can return a Point structure the PInvoke was more difficult. Here is the code necessary to call the Escape method:

First you need the POINT structure defined in .NET:

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}



Then you need the Signature for the Escape method ):

[DllImport("gdi32.dll")]
private static extern int Escape(IntPtr hdc, int nEscape, int cbInput, string lpvInData, IntPtr lpvOutData);



There are also the constants that are sent as the Escape type (I just provide a couple of example that return a Point structure):

private const int GETPRINTINGOFFSET = 13;
private const int GETPHYSPAGESIZE = 12;



Then you can call the method. You can get the Graphics object from various places, but you need the HDc from it regardless:


IntPtr hDC = e.Graphics.GetHdc();
IntPtr pnt;
long stat;
POINT printingOffsetPoint = new POINT(0,0);
pnt = Marshal.AllocHGlobal(Marshal.SizeOf(printingOffsetPoint));
Marshal.StructureToPtr(printingOffsetPoint, pnt, false);
stat = Escape(hDC,GETPRINTINGOFFSET, 0, null, pnt);
printingOffsetPoint = (POINT)Marshal.PtrToStructure(pnt, typeof(POINT));
Marshal.FreeHGlobal(pnt);



POINT physPageSizePoint = new POINT(0, 0);
pnt = Marshal.AllocHGlobal(Marshal.SizeOf(physPageSizePoint));
Marshal.StructureToPtr(physPageSizePoint, pnt, false);
stat = Escape(hDC, GETPHYSPAGESIZE, 0, null, pnt);
physPageSizePoint = (POINT)Marshal.PtrToStructure(pnt, typeof(POINT));
Marshal.FreeHGlobal(pnt);

Thursday, August 7, 2008

Using Dock Fill on a Control when you have a Menu and/or Status Bar

I have long been frustrated in Visual Studio .NET when I try to use a control's Dock property set to Fill and also have a Menu or Status bar on the same form. The Docked control typically fills to the form and is not the right size (not accounting for the size of the Menu or Status bar).

It turns out that there is a very simple (but not very obvious) solution to this problem. Taken from Alex Fr's response to a comment on a similar problem:

"Actually, all you need to do is rightclick the panel and choose "bring to front", the dock layout logic handles each component in order so when it reaches your panel first it doesnt know it has other components that fill up other parts of the container with their docking settings. "

Just make sure the Docked control is in front of the menu and status bar and it works as you would expect.

Sunday, August 3, 2008

Seeing what color the .NET Color names actually are

I found a very useful .NET Color reference at the following web site. If you want to compare AliceBlue, DodgerBlue, and SteelBlue, here is an easy way to do it:


http://www.opinionatedgeek.com/DotNet/Tools/Colors/default.aspx