The original DRAWITEMSTRUCT looks like this:
typedef struct tagDRAWITEMSTRUCT { UINT CtlType; UINT CtlID; UINT itemID; UINT itemAction; UINT itemState; HWND hwndItem; HDC hDC; RECT rcItem; ULONG_PTR itemData; } DRAWITEMSTRUCT;
To convert it for use with managed code and marshalling we need to create a structure using .NET types.
UINTS are the same size as integers and the .NET uint type is a pain to use because compilers refuse to allow easy conversion from int types and even bitwise logical operations on two uints. This is nonsensical pedantic frippery on the part of the compiler designer but we can cheat the system and get all our unsigned-ints as plain old integers and just remember that we may need to compensate for signed operations. Generally, all we're looking for in these structures is a simple number or bit combination so this cheat is fine in 99% of cases.
The adjusted structure looks like this.
[StructLayout(LayoutKind.Sequential)]
public struct DRAWITEMSTRUCT
{
public int CtlType;
public int CtlID;
public int itemID;
public int itemAction;
public int itemState;
public IntPtr hwndItem;
public IntPtr hDC;
public RECT rcItem;
public IntPtr itemData;
}
Note that there is still a RECT structure in there unaccounted for. This is managed using an equivalent managed structure of the same style as the RECT.
public struct RECT
public int left;
public int top;
public int right;
public int bottom;
public int Width
get{return right-left;}
public int Height
get{return bottom-top;}
This structure has Height and Width properties that enable its conversion to a Rectangle more easily.
Return to the article.
Copyright © Bob Powell 2003-2009. All rights reserved