All pastes #637568 Raw Edit

C# WinAPI

public text v1 · immutable
#637568 ·published 2007-07-29 00:32 UTC
rendered paste body
using System;
using System.Runtime.InteropServices;
using System.Reflection;

unsafe class Program
{

    #region WinAPI
    [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(
        MSG* 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(
        MSG* 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
    );
    [DllImport("user32.dll")]
    public static extern short RegisterClassEx(
        ref WNDCLASSEX lpwcx	// address of structure with class data
    );
    [DllImport("user32.dll")]
    public static extern IntPtr LoadIcon(
        IntPtr hInstance,	// handle of application instance
        IntPtr lpIconName 	// icon-name string or icon resource identifier
    );
    [DllImport("user32.dll")]
    public static extern IntPtr LoadCursor(
        IntPtr hInstance,	// handle of application instance
        IntPtr lpCursorName 	// name string or cursor resource identifier  
    );
    [DllImport("user32.dll")]
    public static extern bool TranslateMessage(
        MSG* lpMsg 	// address of structure with message
    );
    public static short HIWORD(uint l)
    {
        return ((short)(((uint)(l) >> 16) & 0xFFFF));
    }
    public static IntPtr MAKEINTRESOURCE(uint i){
        return (IntPtr)((uint)((UInt16)(i)));
    }


    [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 POINT pt;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct WNDCLASSEX
    {
        public uint cbSize;
        public uint style;
        [MarshalAs(UnmanagedType.FunctionPtr)]
        public WNDPROC lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        public string lpszMenuName;
        public string lpszClassName;
        public IntPtr hIconSm;
    }

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate uint WNDPROC(IntPtr hwnd, uint uMsg, uint wParam, uint lParam);

    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 const uint BN_CLICKED    = 0;
    public const uint WM_DESTROY    = 2;
    public const uint WM_CREATE     = 1;
    public const uint WM_COMMAND    = 273;
    public const uint COLOR_WINDOW  = 5;
    public static IntPtr IDI_APPLICATION = MAKEINTRESOURCE(32512);
    public static IntPtr IDC_CROSS = MAKEINTRESOURCE(32515);
    #endregion

    public static uint WindowProc(IntPtr hwnd, uint uMsg, uint wParam, uint lParam)
    {
        switch (uMsg)
        {
            case WM_CREATE:
                return 0;

            case WM_DESTROY:
                PostQuitMessage(0);
                break;

            case WM_COMMAND:
                switch (HIWORD(wParam))
                {
                    case (short)BN_CLICKED:
                        MessageBox(hwnd, "Button clicked!", "Awesome", 0);
                        PostQuitMessage(0);
                        break;
                }
                break;

            default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);

        }
        return 0;
    }

    public static int Main()
    {
        WNDCLASSEX wc = new WNDCLASSEX();
        wc.cbSize = (uint)Marshal.SizeOf(wc);
        wc.style = 0;
        wc.cbClsExtra = 0;
        wc.lpfnWndProc = new WNDPROC(WindowProc);
        wc.cbWndExtra = 0;
        wc.hIcon = LoadIcon(IntPtr.Zero, IDI_APPLICATION);
        wc.hCursor = LoadCursor(IntPtr.Zero, IDC_CROSS);
        wc.hbrBackground = (IntPtr)(COLOR_WINDOW + 1);
        wc.lpszMenuName = null;
        wc.lpszClassName = "HAX";
        wc.hIconSm = LoadIcon(IntPtr.Zero, IDI_APPLICATION);

        RegisterClassEx(ref wc);

        IntPtr wind = CreateWindowEx(0, "HAX", "Window! >:D", WS_SYSMENU | WS_CAPTION | WS_BORDER, CW_USEDEFAULT, 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...", WS_CHILD | SS_CENTER, 10, 10, 175, 60, wind, IntPtr.Zero, IntPtr.Zero, null);
        IntPtr button = CreateWindowEx(0, "BUTTON", "Close", WS_CHILD, 55, 80, 80, 30, wind, IntPtr.Zero, IntPtr.Zero, null);

        ShowWindow(wind, SW_SHOW);
        ShowWindow(label, SW_SHOW);
        ShowWindow(button, SW_SHOW);

        MSG msg;
        while (GetMessage(&msg, wind, 0, 0) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        return (int)msg.wParam;
    }
}