0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その233

Last updated at Posted at 2022-07-09

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

win32apiを叩いて、windowを表示せよ。

方針

user32.dllを使う。

写真

image.png

サンプルコード



using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;

public static class Native {
	[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
	public static extern IntPtr DeleteDC(IntPtr hDc);
	[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
	public static extern IntPtr DeleteObject(IntPtr hDc);
	[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
	public static extern bool BitBlt(IntPtr hsrcDest, int xDest, int yDest, int wDest, int hsrcSrc, IntPtr hdcSource, int xSrc, int ySrc, int ROp);
	[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
	public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
	[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
	public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
	[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
	public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
	[DllImport("gdi32.dll", EntryPoint = "TextOut")]
	public static extern bool TextOut(IntPtr hdcDest, int x, int y, string moji, int nagasa);
	[DllImport("gdi32.dll", EntryPoint = "LineTo")]
	public static extern bool LineTo(IntPtr hDC, int x, int y);
	[DllImport("gdi32.dll", EntryPoint = "MoveToEx")]
	public static extern bool MoveToEx(IntPtr hDC, int x, int y, IntPtr OldPoint);
	[DllImport("gdi32.dll")]
	public static extern IntPtr CreatePen(PenStyle fnPenStyle, int nWidth, uint crColor);
	[DllImportAttribute("gdi32.dll", EntryPoint = "CreateSolidBrush")]
	public static extern IntPtr CreateSolidBrush(int crColor);
	[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true, EntryPoint = "PatBlt")]
	public static extern bool PatBlt(IntPtr hdc, int left, int top, int width, int height, int rop);
	[DllImport("gdi32.dll")]
	public static extern int SetBkMode(IntPtr hdc, int iBkMode);
	[DllImport("gdi32.dll")]
	public static extern uint SetTextColor(IntPtr hdc, int crColor);
	[DllImport("gdi32")]
	public static extern bool Ellipse(IntPtr hdc, int nLeft, int nTop, int nRight, int nBottom);
	[DllImport("user32.dll")]
	public static extern IntPtr DispatchMessage([In] ref MSG lpmsg);
	[DllImport("user32.dll")]
	public static extern bool TranslateMessage([In] ref MSG lpMsg);
	[DllImport("user32.dll")]
	public static extern sbyte GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
	[DllImport("user32.dll", SetLastError = true, EntryPoint="CreateWindowEx")]
	public static extern IntPtr CreateWindowEx(WindowStylesEx dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
	[DllImport("user32.dll", SetLastError = true, EntryPoint="CreateWindowEx")]
	public static extern IntPtr CreateWindowEx2(WindowStylesEx dwExStyle, UInt16 lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
	[DllImport("user32.dll")]
	public static extern ushort RegisterClass([In] ref WNDCLASS lpWndClass);
	[DllImport("user32.dll")]
	[return: MarshalAs(UnmanagedType.Bool)]
	public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
	[DllImport("user32.dll")]
	public static extern IntPtr BeginPaint(IntPtr hwnd, out PAINTSTRUCT lpPaint);
	[DllImport("user32.dll")]
	public static extern IntPtr DefWindowProc(IntPtr hWnd, WM uMsg, IntPtr wParam, IntPtr lParam);
	[DllImport("user32.dll")]
	public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
	[DllImport("user32.dll")]
	public static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);
	[DllImport("user32.dll")]
	public static extern bool EndPaint(IntPtr hWnd, [In] ref PAINTSTRUCT lpPaint);
	[DllImport("user32.dll")]
	public static extern void PostQuitMessage(int nExitCode);
	[DllImport("user32.dll")]
	public static extern IntPtr LoadIcon(IntPtr hInstance, string lpIconName);
	[DllImport("user32.dll")]
	public static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIConName);
	[DllImport("gdi32.dll")]
	public static extern IntPtr GetStockObject(StockObjects fnObject);
	[DllImport("user32.dll")]
	public static extern MessageBoxResult MessageBox(IntPtr hWnd, string text, string caption, int options);
	[DllImport("user32.dll")]
	public static extern bool UpdateWindow(IntPtr hWnd);
	[DllImport("user32.dll")]
	public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
	[DllImport("user32.dll")]
	public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);
	[DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClassEx")]
	public static extern UInt16 RegisterClassEx2([In] ref WNDCLASSEX lpwcx);
}

public static class Win32_DT_Constant {
	public const int DT_TOP = 0x00000000;
	public const int DT_LEFT = 0x00000000;
	public const int DT_CENTER = 0x00000001;
	public const int DT_RIGHT = 0x00000002;
	public const int DT_VCENTER = 0x00000004;
	public const int DT_BOTTOM = 0x00000008;
	public const int DT_WORDBREAK = 0x00000010;
	public const int DT_SINGLELINE = 0x00000020;
	public const int DT_EXPANDTABS = 0x00000040;
	public const int DT_TABSTOP = 0x00000080;
	public const int DT_NOCLIP = 0x00000100;
	public const int DT_EXTERNALLEADING = 0x00000200;
	public const int DT_CALCRECT = 0x00000400;
	public const int DT_NOPREFIX = 0x00000800;
	public const int DT_INTERNAL = 0x00001000;
}

public static class Win32_CW_Constant {
	public const int CW_USEDEFAULT = -1;
}
public enum PenStyle {
	PS_SOLID = 0,
	PS_DASH = 1,
	PS_DOT = 2,
	PS_DASHDOT = 3,
	PS_DASHDOTDOT = 4,
	PS_NULL = 5,
	PS_INSIDEFRAME = 6,
	PS_USERSTYLE = 7,
	PS_ALTERNATE = 8,
	PS_STYLE_MASK = 0x0000000F,
	PS_ENDCAP_ROUND = 0x00000000,
	PS_ENDCAP_SQUARE = 0x00000100,
	PS_ENDCAP_FLAT = 0x00000200,
	PS_ENDCAP_MASK = 0x00000F00,
	PS_JOIN_ROUND = 0x00000000,
	PS_JOIN_BEVEL = 0x00001000,
	PS_JOIN_MITER = 0x00002000,
	PS_JOIN_MASK = 0x0000F000,
	PS_COSMETIC = 0x00000000,
	PS_GEOMETRIC = 0x00010000,
	PS_TYPE_MASK = 0x000F0000
};
public enum StockObjects {
	WHITE_BRUSH = 0,
	LTGRAY_BRUSH = 1,
	GRAY_BRUSH = 2,
	DKGRAY_BRUSH = 3,
	BLACK_BRUSH = 4,
	NULL_BRUSH = 5,
	HOLLOW_BRUSH = NULL_BRUSH,
	WHITE_PEN = 6,
	BLACK_PEN = 7,
	NULL_PEN = 8,
	OEM_FIXED_FONT = 10,
	ANSI_FIXED_FONT = 11,
	ANSI_VAR_FONT = 12,
	SYSTEM_FONT = 13,
	DEVICE_DEFAULT_FONT = 14,
	DEFAULT_PALETTE = 15,
	SYSTEM_FIXED_FONT = 16,
	DEFAULT_GUI_FONT = 17,
	DC_BRUSH = 18,
	DC_PEN = 19,
}

public static class Win32_IDC_Constants {
	public const int
	IDC_ARROW = 32512,
	IDC_IBEAM = 32513,
	IDC_WAIT = 32514,
	IDC_CROSS = 32515,
	IDC_UPARROW = 32516,
	IDC_SIZE = 32640,
	IDC_ICON = 32641,
	IDC_SIZENWSE = 32642,
	IDC_SIZENESW = 32643,
	IDC_SIZEWE = 32644,
	IDC_SIZENS = 32645,
	IDC_SIZEALL = 32646,
	IDC_NO = 32648,
	IDC_HAND = 32649,
	IDC_APPSTARTING = 32650,
	IDC_HELP = 32651;
}

[Flags]
public enum MessageBoxOptions : uint {
	OkOnly = 0x000000,
	OkCancel = 0x000001,
	AbortRetryIgnore = 0x000002,
	YesNoCancel = 0x000003,
	YesNo = 0x000004,
	RetryCancel = 0x000005,
	CancelTryContinue = 0x000006,
	IconHand = 0x000010,
	IconQuestion = 0x000020,
	IconExclamation = 0x000030,
	IconAsterisk = 0x000040,
	UserIcon = 0x000080,
	IconWarning = IconExclamation,
	IconError = IconHand,
	IconInformation = IconAsterisk,
	IconStop = IconHand,
	DefButton1 = 0x000000,
	DefButton2 = 0x000100,
	DefButton3 = 0x000200,
	DefButton4 = 0x000300,
	ApplicationModal = 0x000000,
	SystemModal = 0x001000,
	TaskModal = 0x002000,
	Help = 0x004000,
	NoFocus = 0x008000,
	SetForeground = 0x010000,
	DefaultDesktopOnly = 0x020000,
	Topmost = 0x040000,
	Right = 0x080000,
	RTLReading = 0x100000
}

public enum MessageBoxResult : uint {
	Ok = 1,
	Cancel,
	Abort,
	Retry,
	Ignore,
	Yes,
	No,
	Close,
	Help,
	TryAgain,
	Continue,
	Timeout = 32000
}

[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct MSG {
	public IntPtr hwnd;
	public UInt32 message;
	public UIntPtr wParam;
	public UIntPtr lParam;
	public UInt32 time;
	public POINT pt;
}

public struct POINT {
	public Int32 x;
	public Int32 Y;
}

[StructLayout(LayoutKind.Sequential)]
public struct WNDCLASS {
	public ClassStyles 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;
	[MarshalAs(UnmanagedType.LPTStr)]
	public string lpszMenuName;
	[MarshalAs(UnmanagedType.LPTStr)]
	public string lpszClassName;
}

public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WNDCLASSEX {
	[MarshalAs(UnmanagedType.U4)]
	public int cbSize;
	[MarshalAs(UnmanagedType.U4)]
	public int style;
	public IntPtr 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;
}

[Flags()]
public enum WindowStyles : uint {
	WS_BORDER = 0x800000,
	WS_CAPTION = 0xc00000,
	WS_CHILD = 0x40000000,
	WS_CLIPCHILDREN = 0x2000000,
	WS_CLIPSIBLINGS = 0x4000000,
	WS_DISABLED = 0x8000000,
	WS_DLGFRAME = 0x400000,
	WS_GROUP = 0x20000,
	WS_HSCROLL = 0x100000,
	WS_MAXIMIZE = 0x1000000,
	WS_MAXIMIZEBOX = 0x10000,
	WS_MINIMIZE = 0x20000000,
	WS_MINIMIZEBOX = 0x20000,
	WS_OVERLAPPED = 0x0,
	WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_SIZEFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
	WS_POPUP = 0x80000000u,
	WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU,
	WS_SIZEFRAME = 0x40000,
	WS_SYSMENU = 0x80000,
	WS_TABSTOP = 0x10000,
	WS_VISIBLE = 0x10000000,
	WS_VSCROLL = 0x200000
}

[Flags]
public enum WindowStylesEx : uint {
	WS_EX_ACCEPTFILES = 0x00000010,
	WS_EX_APPWINDOW = 0x00040000,
	WS_EX_CLIENTEDGE = 0x00000200,
	WS_EX_COMPOSITED = 0x02000000,
	WS_EX_CONTEXTHELP = 0x00000400,
	WS_EX_CONTROLPARENT = 0x00010000,
	WS_EX_DLGMODALFRAME = 0x00000001,
	WS_EX_LAYERED = 0x00080000,
	WS_EX_LAYOUTRTL = 0x00400000,
	WS_EX_LEFT = 0x00000000,
	WS_EX_LEFTSCROLLBAR = 0x00004000,
	WS_EX_LTRREADING = 0x00000000,
	WS_EX_MDICHILD = 0x00000040,
	WS_EX_NOACTIVATE = 0x08000000,
	WS_EX_NOINHERITLAYOUT = 0x00100000,
	WS_EX_NOPARENTNOTIFY = 0x00000004,
	WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
	WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
	WS_EX_RIGHT = 0x00001000,
	WS_EX_RIGHTSCROLLBAR = 0x00000000,
	WS_EX_RTLREADING = 0x00002000,
	WS_EX_STATICEDGE = 0x00020000,
	WS_EX_TOOLWINDOW = 0x00000080,
	WS_EX_TOPMOST = 0x00000008,
	WS_EX_TRANSPARENT = 0x00000020,
	WS_EX_WINDOWEDGE = 0x00000100
}

public enum ShowWindowCommands : int {
	Hide = 0,
	Normal = 1,
	ShowMinimized = 2,
	Maximize = 3,
	ShowMaximized = 3,
	ShowNoActivate = 4,
	Show = 5,
	Minimize = 6,
	ShowMinNoActive = 7,
	ShowNA = 8,
	Restore = 9,
	ShowDefault = 10,
	ForceMinimize = 11
}


[Flags]
public enum ClassStyles : uint {
	ByteAlignClient = 0x1000,
	ByteAlignWindow = 0x2000,
	ClassDC = 0x40,
	DoubleClicks = 0x8,
	DropShadow = 0x20000,
	GlobalClass = 0x4000,
	HorizontalRedraw = 0x2,
	NoClose = 0x200,
	OwnDC = 0x20,
	ParentDC = 0x80,
	SaveBits = 0x800,
	VerticalRedraw = 0x1
}

[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT {
	public IntPtr hdc;
	public bool fErase;
	public RECT rcPaint;
	public bool fRestore;
	public bool fIncUpdate;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
	public byte[] rgbReserved;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT {
	public int Left,
		Top,
		Right,
		Bottom;
	public RECT(int left, int top, int right, int bottom)
	{
		Left = left;
		Top = top;
		Right = right;
		Bottom = bottom;
	}
	public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) {
	}
	public int X {
		get { 
			return Left; 
		}
		set { 
			Right -= (Left - value); 
			Left = value; 
		}
	}
	public int Y {
		get { 
			return Top; 
		}
		set { 
			Bottom -= (Top - value); 
			Top = value; 
		}
	}
	public int Height {
		get { 
			return Bottom - Top; 
		}
		set { 
			Bottom = value + Top; 
		}
	}
	public int Width {
		get { 
			return Right - Left; 
		}
		set { 
			Right = value + Left; 
		}
	}
	public System.Drawing.Point Location {
		get { 
			return new System.Drawing.Point(Left, Top); 
		}
		set { 
			X = value.X; 
			Y = value.Y; 
		}
	}
	public System.Drawing.Size Size {
		get { 
			return new System.Drawing.Size(Width, Height); 
		}
		set { 
			Width = value.Width; 
			Height = value.Height; 
		}
	}
	public static implicit operator System.Drawing.Rectangle(RECT r) {
		return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
	}
	public static implicit operator RECT(System.Drawing.Rectangle r) {
		return new RECT(r);
	}
	public static bool operator == (RECT r1, RECT r2) {
		return r1.Equals(r2);
	}
	public static bool operator != (RECT r1, RECT r2) {
		return !r1.Equals(r2);
	}
	public bool Equals(RECT r) {
		return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
	}
	public override bool Equals(object obj) {
		if (obj is RECT)
			return Equals((RECT) obj);
		else if (obj is System.Drawing.Rectangle)
			return Equals(new RECT((System.Drawing.Rectangle) obj));
		return false;
	}
	public override int GetHashCode() {
		return ((System.Drawing.Rectangle) this).GetHashCode();
	}
	public override string ToString() {
		return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
	}
}

public enum WM : uint {
	NULL = 0x0000,
	CREATE = 0x0001,
	DESTROY = 0x0002,
	MOVE = 0x0003,
	SIZE = 0x0005,
	ACTIVATE = 0x0006,
	SETFOCUS = 0x0007,
	KILLFOCUS = 0x0008,
	ENABLE = 0x000A,
	SETREDRAW = 0x000B,
	SETTEXT = 0x000C,
	GETTEXT = 0x000D,
	GETTEXTLENGTH = 0x000E,
	PAINT = 0x000F,
	CLOSE = 0x0010,
	QUERYENDSESSION = 0x0011,
	QUERYOPEN = 0x0013,
	ENDSESSION = 0x0016,
	QUIT = 0x0012,
	ERASEBKGND = 0x0014,
	SYSCOLORCHANGE = 0x0015,
	SHOWWINDOW = 0x0018,
	WININICHANGE = 0x001A,
	SETTINGCHANGE = WININICHANGE,
	DEVMODECHANGE = 0x001B,
	ACTIVATEAPP = 0x001C,
	FONTCHANGE = 0x001D,
	TIMECHANGE = 0x001E,
	CANCELMODE = 0x001F,
	SETCURSOR = 0x0020,
	MOUSEACTIVATE = 0x0021,
	CHILDACTIVATE = 0x0022,
	QUEUESYNC = 0x0023,
	GETMINMAXINFO = 0x0024,
	PAINTICON = 0x0026,
	ICONERASEBKGND = 0x0027,
	NEXTDLGCTL = 0x0028,
	SPOOLERSTATUS = 0x002A,
	DRAWITEM = 0x002B,
	MEASUREITEM = 0x002C,
	DELETEITEM = 0x002D,
	VKEYTOITEM = 0x002E,
	CHARTOITEM = 0x002F,
	SETFONT = 0x0030,
	GETFONT = 0x0031,
	SETHOTKEY = 0x0032,
	GETHOTKEY = 0x0033,
	QUERYDRAGICON = 0x0037,
	COMPAREITEM = 0x0039,
	GETOBJECT = 0x003D,
	COMPACTING = 0x0041,
	[Obsolete]
	COMMNOTIFY = 0x0044,
	WINDOWPOSCHANGING = 0x0046,
	WINDOWPOSCHANGED = 0x0047,
	[Obsolete]
	POWER = 0x0048,
	COPYDATA = 0x004A,
	CANCELJOURNAL = 0x004B,
	NOTIFY = 0x004E,
	INPUTLANGCHANGEREQUEST = 0x0050,
	INPUTLANGCHANGE = 0x0051,
	TCARD = 0x0052,
	HELP = 0x0053,
	USERCHANGED = 0x0054,
	NOTIFYFORMAT = 0x0055,
	CONTEXTMENU = 0x007B,
	STYLECHANGING = 0x007C,
	STYLECHANGED = 0x007D,
	DISPLAYCHANGE = 0x007E,
	GETICON = 0x007F,
	SETICON = 0x0080,
	NCCREATE = 0x0081,
	NCDESTROY = 0x0082,
	NCCALCSIZE = 0x0083,
	NCHITTEST = 0x0084,
	NCPAINT = 0x0085,
	NCACTIVATE = 0x0086,
	GETDLGCODE = 0x0087,
	SYNCPAINT = 0x0088,
	NCMOUSEMOVE = 0x00A0,
	NCLBUTTONDOWN = 0x00A1,
	NCLBUTTONUP = 0x00A2,
	NCLBUTTONDBLCLK = 0x00A3,
	NCRBUTTONDOWN = 0x00A4,
	NCRBUTTONUP = 0x00A5,
	NCRBUTTONDBLCLK = 0x00A6,
	NCMBUTTONDOWN = 0x00A7,
	NCMBUTTONUP = 0x00A8,
	NCMBUTTONDBLCLK = 0x00A9,
	NCXBUTTONDOWN = 0x00AB,
	NCXBUTTONUP = 0x00AC,
	NCXBUTTONDBLCLK = 0x00AD,
	INPUT_DEVICE_CHANGE = 0x00FE,
	INPUT = 0x00FF,
	KEYFIRST = 0x0100,
	KEYDOWN = 0x0100,
	KEYUP = 0x0101,
	CHAR = 0x0102,
	DEADCHAR = 0x0103,
	SYSKEYDOWN = 0x0104,
	SYSKEYUP = 0x0105,
	SYSCHAR = 0x0106,
	SYSDEADCHAR = 0x0107,
	UNICHAR = 0x0109,
	KEYLAST = 0x0109,
	IME_STARTCOMPOSITION = 0x010D,
	IME_ENDCOMPOSITION = 0x010E,
	IME_COMPOSITION = 0x010F,
	IME_KEYLAST = 0x010F,
	INITDIALOG = 0x0110,
	COMMAND = 0x0111,
	SYSCOMMAND = 0x0112,
	TIMER = 0x0113,
	HSCROLL = 0x0114,
	VSCROLL = 0x0115,
	INITMENU = 0x0116,
	INITMENUPOPUP = 0x0117,
	MENUSELECT = 0x011F,
	MENUCHAR = 0x0120,
	ENTERIDLE = 0x0121,
	MENURBUTTONUP = 0x0122,
	MENUDRAG = 0x0123,
	MENUGETOBJECT = 0x0124,
	UNINITMENUPOPUP = 0x0125,
	MENUCOMMAND = 0x0126,
	CHANGEUISTATE = 0x0127,
	UPDATEUISTATE = 0x0128,
	QUERYUISTATE = 0x0129,
	CTLCOLORMSGBOX = 0x0132,
	CTLCOLOREDIT = 0x0133,
	CTLCOLORLISTBOX = 0x0134,
	CTLCOLORBTN = 0x0135,
	CTLCOLORDLG = 0x0136,
	CTLCOLORSCROLLBAR = 0x0137,
	CTLCOLORSTATIC = 0x0138,
	MOUSEFIRST = 0x0200,
	MOUSEMOVE = 0x0200,
	LBUTTONDOWN = 0x0201,
	LBUTTONUP = 0x0202,
	LBUTTONDBLCLK = 0x0203,
	RBUTTONDOWN = 0x0204,
	RBUTTONUP = 0x0205,
	RBUTTONDBLCLK = 0x0206,
	MBUTTONDOWN = 0x0207,
	MBUTTONUP = 0x0208,
	MBUTTONDBLCLK = 0x0209,
	MOUSEWHEEL = 0x020A,
	XBUTTONDOWN = 0x020B,
	XBUTTONUP = 0x020C,
	XBUTTONDBLCLK = 0x020D,
	MOUSEHWHEEL = 0x020E,
	MOUSELAST = 0x020E,
	PARENTNOTIFY = 0x0210,
	ENTERMENULOOP = 0x0211,
	EXITMENULOOP = 0x0212,
	NEXTMENU = 0x0213,
	SIZING = 0x0214,
	CAPTURECHANGED = 0x0215,
	MOVING = 0x0216,
	POWERBROADCAST = 0x0218,
	DEVICECHANGE = 0x0219,
	MDICREATE = 0x0220,
	MDIDESTROY = 0x0221,
	MDIACTIVATE = 0x0222,
	MDIRESTORE = 0x0223,
	MDINEXT = 0x0224,
	MDIMAXIMIZE = 0x0225,
	MDITILE = 0x0226,
	MDICASCADE = 0x0227,
	MDIICONARRANGE = 0x0228,
	MDIGETACTIVE = 0x0229,
	MDISETMENU = 0x0230,
	ENTERSIZEMOVE = 0x0231,
	EXITSIZEMOVE = 0x0232,
	DROPFILES = 0x0233,
	MDIREFRESHMENU = 0x0234,
	IME_SETCONTEXT = 0x0281,
	IME_NOTIFY = 0x0282,
	IME_CONTROL = 0x0283,
	IME_COMPOSITIONFULL = 0x0284,
	IME_SELECT = 0x0285,
	IME_CHAR = 0x0286,
	IME_REQUEST = 0x0288,
	IME_KEYDOWN = 0x0290,
	IME_KEYUP = 0x0291,
	MOUSEHOVER = 0x02A1,
	MOUSELEAVE = 0x02A3,
	NCMOUSEHOVER = 0x02A0,
	NCMOUSELEAVE = 0x02A2,
	WTSSESSION_CHANGE = 0x02B1,
	TABLET_FIRST = 0x02c0,
	TABLET_LAST = 0x02df,
	CUT = 0x0300,
	COPY = 0x0301,
	PASTE = 0x0302,
	CLEAR = 0x0303,
	UNDO = 0x0304,
	RENDERFORMAT = 0x0305,
	RENDERALLFORMATS = 0x0306,
	DESTROYCLIPBOARD = 0x0307,
	DRAWCLIPBOARD = 0x0308,
	PAINTCLIPBOARD = 0x0309,
	VSCROLLCLIPBOARD = 0x030A,
	SIZECLIPBOARD = 0x030B,
	ASKCBFORMATNAME = 0x030C,
	CHANGECBCHAIN = 0x030D,
	HSCROLLCLIPBOARD = 0x030E,
	QUERYNEWPALETTE = 0x030F,
	PALETTEISCHANGING = 0x0310,
	PALETTECHANGED = 0x0311,
	HOTKEY = 0x0312,
	PRINT = 0x0317,
	PRINTCLIENT = 0x0318,
	APPCOMMAND = 0x0319,
	THEMECHANGED = 0x031A,
	CLIPBOARDUPDATE = 0x031D,
	DWMCOMPOSITIONCHANGED = 0x031E,
	DWMNCRENDERINGCHANGED = 0x031F,
	DWMCOLORIZATIONCOLORCHANGED = 0x0320,
	DWMWINDOWMAXIMIZEDCHANGE = 0x0321,
	GETTITLEBARINFOEX = 0x033F,
	HANDHELDFIRST = 0x0358,
	HANDHELDLAST = 0x035F,
	AFXFIRST = 0x0360,
	AFXLAST = 0x037F,
	PENWINFIRST = 0x0380,
	PENWINLAST = 0x038F,
	APP = 0x8000,
	USER = 0x0400,
	CPL_LAUNCH = USER + 0x1000,
	CPL_LAUNCHED = USER + 0x1001,
	SYSTIMER = 0x118
}

class Program {
	private static IntPtr hinst;
	private static UInt16 atom;
	static IntPtr MainWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) {
		IntPtr hdc;
		PAINTSTRUCT ps;
		RECT rect;
		IntPtr pen;
		switch ((WM) msg)
		{
		case WM.PAINT:
			pen = Native.CreatePen(PenStyle.PS_SOLID, 1, (uint) ColorTranslator.ToWin32(Color.Red));
			hdc = Native.BeginPaint(hWnd, out ps);
			Native.SelectObject(hdc, pen);
			Native.MoveToEx(hdc, 100, 100, IntPtr.Zero);
			Native.LineTo(hdc, 200, 200);
			Native.GetClientRect(hWnd, out rect);
			Native.DrawText(hdc, "Hello, World!!", -1, ref rect, Win32_DT_Constant.DT_SINGLELINE | Win32_DT_Constant.DT_CENTER | Win32_DT_Constant.DT_VCENTER);
			Native.EndPaint(hWnd, ref ps);
			return IntPtr.Zero;
		case WM.DESTROY:
			Native.PostQuitMessage(0);
			return IntPtr.Zero;
		}
		return Native.DefWindowProc(hWnd, (WM) msg, wParam, lParam);
	}
	private static bool InitInstance(IntPtr hInstance, int nCmdShow) {
		IntPtr hwnd;
		hinst = hInstance;
		hwnd = Native.CreateWindowEx2(0, atom, "test", WindowStyles.WS_OVERLAPPEDWINDOW, 100, 100, 400, 300, IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero);
		if (hwnd == IntPtr.Zero)
		{
			string error = new Win32Exception(Marshal.GetLastWin32Error()).Message;
			Console.WriteLine("Failed to InitInstance , error = {0}", error);
			return false;
		}
		Native.ShowWindow(hwnd, (ShowWindowCommands) nCmdShow);
		Native.UpdateWindow(hwnd);
		return true;
	}
	private static bool InitApplication(IntPtr hinstance) {
		WNDCLASSEX wcx = new WNDCLASSEX();
		wcx.cbSize = Marshal.SizeOf(wcx);
		wcx.style = (int) (ClassStyles.VerticalRedraw | ClassStyles.HorizontalRedraw);
		unsafe
		{
			IntPtr address2 = Marshal.GetFunctionPointerForDelegate((Delegate) (WndProc) MainWndProc);
			wcx.lpfnWndProc = address2;
		}
		wcx.cbClsExtra = 0;
		wcx.cbWndExtra = 0;
		wcx.hInstance = hinstance;
		wcx.hbrBackground = Native.GetStockObject(StockObjects.DC_BRUSH);
		wcx.lpszClassName = "Main";
		UInt16 ret = Native.RegisterClassEx2(ref wcx);
		if (ret != 0)
		{
			string message = new Win32Exception(Marshal.GetLastWin32Error()).Message;
			Console.WriteLine("Failed to call RegisterClasEx, error = {0}", message);
		}
		atom = ret;
		return ret != 0;
	}
	static bool Main_(IntPtr hinstance, IntPtr hPrevInstance, string lpCmdLine, int nCmdShow) {
		MSG msg;
		if (!InitApplication(hinstance))
			return false;
		if (!InitInstance(hinstance, nCmdShow))
			return false;
		sbyte hasMessage;
		while ((hasMessage = Native.GetMessage(out msg, IntPtr.Zero, 0, 0)) != 0 && hasMessage != -1)
		{
			Native.TranslateMessage(ref msg);
			Native.DispatchMessage(ref msg);
		}
		return msg.wParam == UIntPtr.Zero;
	}
	static void Main(string[] args) {
		Main_(System.Diagnostics.Process.GetCurrentProcess().Handle, IntPtr.Zero, string.Empty, (int) ShowWindowCommands.Normal);
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?