using System;
using System.Runtime.InteropServices;
unsafe class Program {
[DllImport("Win.dll")]
public static extern void CreateWndClass(
string name // wndclass name
);
[DllImport("user32.dll")]
public static extern IntPtr CreateWindowEx(
uint dwExStyle, // extended window style
string lpClassName, // pointer to registered class name
string lpWindowName, // pointer to window name
uint dwStyle, // window style
uint x, // horizontal position of window
uint y, // vertical position of window
uint nWidth, // window width
uint nHeight, // window height
IntPtr hWndParent, // handle to parent or owner window
IntPtr hMenu, // handle to menu, or child-window identifier
IntPtr hInstance, // handle to application instance
void* lpParam // pointer to window-creation data
);
[DllImport("user32.dll")]
public static extern uint DefWindowProc(
IntPtr hWnd, // handle to window
uint Msg, // message identifier
uint wParam, // first message parameter
uint lParam // second message parameter
);
[DllImport("user32.dll")]
public static extern bool ShowWindow(
IntPtr hWnd, // handle of window
uint nCmdShow // show state of window
);
[DllImport("user32.dll")]
public static extern int GetMessage(
IntPtr lpMsg, // address of structure with message
IntPtr hWnd, // handle of window
uint wMsgFilterMin, // first message
uint wMsgFilterMax // last message
);
[DllImport("user32.dll")]
public static extern long DispatchMessage(
IntPtr lpmsg // pointer to structure with message
);
[DllImport("user32.dll")]
public static extern void MessageBox(
IntPtr hWnd, // handle of owner window
string lpText, // address of text in message box
string lpCaption, // address of title of message box
uint uType // style of message box
);
[DllImport("user32.dll")]
public static extern bool SetWindowText(
IntPtr hWnd, // handle of window or control
string lpString // address of string
);
[DllImport("user32.dll")]
public static extern void PostQuitMessage(
int nExitCode // exit code
);
[StructLayout(LayoutKind.Sequential)]
public struct MSG { //WinAPI MSG struct
public IntPtr hwnd;
public uint message;
public uint wParam;
public uint lParam;
public uint time;
public long x;
public long y;
}
public class WinAPI { //WinAPI defines
public const uint WS_SYSMENU = 0x80000;
public const uint WS_BORDER = 0x800000;
public const uint WS_CAPTION = 0xc00000;
public const uint WS_CHILD = 0x40000000;
public const uint SW_SHOW = 5;
public const uint CW_USEDEFAULT = 0x80000000;
public const uint WM_LBUTTONUP = 514;
public const uint SS_CENTER = 1;
}
public static void Main() {
//the only non WinAPI call
//this function calls a C library function
//which creates and registers a WNDCLASSEX
//it also contains a WndProc function which only calls DefWindowProc
CreateWndClass("wndclass");
//create a window, a label, and a button
IntPtr wind = CreateWindowEx(0, "wndclass", "Window! >:D", WinAPI.WS_SYSMENU | WinAPI.WS_CAPTION | WinAPI.WS_BORDER, WinAPI.CW_USEDEFAULT, WinAPI.CW_USEDEFAULT, 200, 200, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, null);
IntPtr label = CreateWindowEx(0, "STATIC", "This window was created using the WinAPI inside a C# application...", WinAPI.WS_CHILD | WinAPI.SS_CENTER, 10, 10, 175, 60, wind, IntPtr.Zero, IntPtr.Zero, null);
IntPtr button = CreateWindowEx(0, "BUTTON", "Close", WinAPI.WS_CHILD, 55, 80, 80, 30, wind, IntPtr.Zero, IntPtr.Zero, null);
//show all 3 windows
ShowWindow(wind, WinAPI.SW_SHOW);
ShowWindow(label, WinAPI.SW_SHOW);
ShowWindow(button, WinAPI.SW_SHOW);
//create a new MSG struct
MSG msg = new MSG();
//allocate memory for the MSG struct
IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(msg));
//marshals the MSG struct into unmanaged memory
Marshal.StructureToPtr(msg, mem, false);
//run message loop
while (GetMessage(mem, wind, 0, 0) > 0) { //GetMessage fills the unmanaged memory with a message
//marshals the memory into a MSG struct
MSG t_msg = (MSG)Marshal.PtrToStructure(mem, typeof(MSG));
if (t_msg.message == WinAPI.WM_LBUTTONUP) { //check for a mouse button up message
if (t_msg.hwnd == button) { //check if the message window is the button
MessageBox(wind, "Processing close message.", "C# WinAPI", 0);
//exit message loop
break;
}
}
//dispatches message to a C library WndProc which calls DefWindowProc
DispatchMessage(mem);
}
//sends a WM_QUIT message
//this is handled by the C library WndProc
PostQuitMessage(0);
}
}