情報アイランド

「情報を制する者は世界を制す」をモットーに様々な情報を提供することを目指すブログです。現在はプログラミング関連情報が多めですが、投資関連情報も取り扱っていきたいです。

C#でWIN32API step1 ウィンドウの表示

2017/07/18

最近P/Iに嵌まっています。

まず、真っ白のウィンドウだけが表示される所謂スケルトンプログラムを作ってみました。

private static IntPtr hinstance = Process.GetCurrentProcess().Handle;
private static IntPtr hicon = WIN32.Window.ExtractIcon(hinstance, "up0669.ico", 0);

[STAThread]
public static void Main(string[] args)
{
    WIN32.Window.WNDCLASSEX wndclassex = new WIN32.Window.WNDCLASSEX();
    wndclassex.cbSize = (uint)Marshal.SizeOf(wndclassex);
    wndclassex.style = WIN32.Window.CS.CS_HREDRAW | WIN32.Window.CS.CS_VREDRAW;
    wndclassex.lpfnWndProc = WndProc;
    wndclassex.cbClsExtra = 0;
    wndclassex.cbWndExtra = 0;
    wndclassex.hInstance = hinstance;
    wndclassex.hIcon = hicon;
    wndclassex.hCursor = WIN32.Window.LoadCursor(IntPtr.Zero, WIN32.Window.IDC.IDC_ARROW);
    wndclassex.hbrBackground = IntPtr.Zero;
    wndclassex.lpszMenuName = null;
    wndclassex.lpszClassName = "anttn";
    wndclassex.hIconSm = IntPtr.Zero;

    WIN32.Window.RegisterClassEx(ref wndclassex);

    IntPtr hWnd = WIN32.Window.CreateWindowEx(0, wndclassex.lpszClassName, "∀n++n", WIN32.Window.WS.WS_OVERLAPPEDWINDOW, WIN32.Window.CW_USEDEFAULT, WIN32.Window.CW_USEDEFAULT, 800, 600, IntPtr.Zero, IntPtr.Zero, wndclassex.hInstance, IntPtr.Zero);

    WIN32.Window.ShowWindow(hWnd, WIN32.Window.SW.SW_SHOW);
    WIN32.Window.UpdateWindow(hWnd);

    WIN32.Window.MSG msg = new WIN32.Window.MSG();
    while (WIN32.Window.GetMessage(out msg, IntPtr.Zero, 0, 0) != 0)
    {
        WIN32.Window.TranslateMessage(ref msg);
        WIN32.Window.DispatchMessage(ref msg);
    }
}

private static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
{
    switch (uMsg)
    {
        case WIN32.Window.WM_DESTROY:
            WIN32.Window.PostQuitMessage(0);
            break;
        default:
            return WIN32.Window.DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    return IntPtr.Zero;
}

こちらがP/I用のコードです。

using System;
using System.Runtime.InteropServices;

namespace Anttn.WIN32
{
    public static partial class Window
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr LoadCursor(IntPtr hInstance, IDC cursor);

        [DllImport("shell32.dll")]
        public static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, uint nIconIndex);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.U2)]
        public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, SW nCmdShow);

        [DllImport("user32.dll")]
        public static extern bool UpdateWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

        [DllImport("user32.dll")]
        public static extern int TranslateMessage([In] ref MSG lpMsg);

        [DllImport("user32.dll")]
        public static extern IntPtr DispatchMessage([In] ref MSG lpMsg);

        [DllImport("user32.dll")]
        public static extern void PostQuitMessage(int nExitCode);

        [DllImport("user32.dll")]
        public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        public delegate IntPtr WNDPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

        [StructLayout(LayoutKind.Sequential)]
        public struct WNDCLASSEX
        {
            public uint cbSize;
            public CS style;
            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;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MSG
        {
            public IntPtr hwnd;
            public uint message;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public POINT pt;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            int x;
            int y;
        }

        public enum IDC : uint
        {
            IDC_APPSTARTING = 0x7f8a,
            IDC_ARROW = 0x7f00,
            IDC_CROSS = 0x7f03,
            IDC_HAND = 0x7f89,
            IDC_HELP = 0x7f8b,
            IDC_IBEAM = 0x7f01,
            IDC_ICON = 0x7f81,
            IDC_NO = 0x7f88,
            IDC_SIZE = 0x7f80,
            IDC_SIZEALL = 0x7f86,
            IDC_SIZENESW = 0x7f83,
            IDC_SIZENS = 0x7f85,
            IDC_SIZENWSE = 0x7f82,
            IDC_SIZEWE = 0x7f84,
            IDC_UPARROW = 0x7f04,
            IDC_WAIT = 0x7f02
        }

        [Flags]
        public enum CS : uint
        {
            CS_VREDRAW = 0x0001,
            CS_HREDRAW = 0x0002,
            CS_DBLCLKS = 0x0008,
            CS_OWNDC = 0x0020,
            CS_CLASSDC = 0x0040,
            CS_PARENTDC = 0x0080,
            CS_NOCLOSE = 0x0200,
            CS_SAVEBITS = 0x0800,
            CS_BYTEALIGNCLIENT = 0x1000,
            CS_BYTEALIGNWINDOW = 0x2000,
            CS_GLOBALCLASS = 0x4000,
            CS_IME = 0x00010000,
            CS_DROPSHADOW = 0x00020000
        }

        [Flags]
        public enum WS : uint
        {
            WS_OVERLAPPED = 0x00000000,
            WS_POPUP = 0x80000000,
            WS_CHILD = 0x40000000,
            WS_MINIMIZE = 0x20000000,
            WS_VISIBLE = 0x10000000,
            WS_DISABLED = 0x08000000,
            WS_CLIPSIBLINGS = 0x04000000,
            WS_CLIPCHILDREN = 0x02000000,
            WS_MAXIMIZE = 0x01000000,
            WS_CAPTION = 0x00C00000,
            WS_BORDER = 0x00800000,
            WS_DLGFRAME = 0x00400000,
            WS_VSCROLL = 0x00200000,
            WS_HSCROLL = 0x00100000,
            WS_SYSMENU = 0x00080000,
            WS_THICKFRAME = 0x00040000,
            WS_GROUP = 0x00020000,
            WS_TABSTOP = 0x00010000,
            WS_MINIMIZEBOX = 0x00020000,
            WS_MAXIMIZEBOX = 0x00010000,
            WS_TILED = WS_OVERLAPPED,
            WS_ICONIC = WS_MINIMIZE,
            WS_SIZEBOX = WS_THICKFRAME,
            WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW,
            WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
            WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
            WS_CHILDWINDOW = WS_CHILD
        }

        public enum SW : uint
        {
            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_FORCEMINIMIZE = 11,
            SW_MAX = 11,
        }

        public const int CW_USEDEFAULT = unchecked((int)0x80000000);

        public const uint WM_DESTROY = 0x0002;
    }
}
pizyumi
プログラミング歴19年のベテランプログラマー。業務システム全般何でも作れます。現在はWeb系の技術を勉強中。
スポンサーリンク

-C#, WIN32API