-
Hello, Win32 GUI(SWT) World!
Posted on 7月 18th, 2012 by cx20
Win32 GUI(Java)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は Java の GUI ツールキット SWT の非公開 API を使用した Win32 GUI アプリケーション の例となっている。ソースコード
import org.eclipse.swt.internal.Callback; import org.eclipse.swt.internal.win32.MSG; import org.eclipse.swt.internal.win32.OS; import org.eclipse.swt.internal.win32.PAINTSTRUCT; import org.eclipse.swt.internal.win32.RECT; import org.eclipse.swt.internal.win32.TCHAR; import org.eclipse.swt.internal.win32.WNDCLASS; public class Hello { public static int WndProc(int hWnd, int uMsg, int wParam, int lParam) { int hdc; PAINTSTRUCT lpPaint = new PAINTSTRUCT(); String strMessage = "Hello, Win32 GUI(SWT) World!"; switch (uMsg) { case OS.WM_PAINT: RECT rect = new RECT(); hdc = OS.BeginPaint(hWnd, lpPaint); OS.GetClientRect(hWnd, rect); OS.DrawTextW(hdc, strMessage.toCharArray(), strMessage.length(), rect, OS.DT_SINGLELINE); OS.EndPaint(hWnd, lpPaint); return 0; case OS.WM_DESTROY: System.exit(wParam); default: return OS.DefWindowProc(hWnd, uMsg, wParam, lParam); } } public static void WinMain(String[] args) { int hInstance = OS.GetModuleHandle(null); TCHAR className = new TCHAR(0, "helloWindow", true); TCHAR windowName = new TCHAR(0, "Hello, World", true); WNDCLASS wc = new WNDCLASS(); wc.style = OS.CS_HREDRAW | OS.CS_VREDRAW; wc.lpfnWndProc = new Callback(Hello.class, "WndProc", 4).getAddress(); wc.hInstance = hInstance; wc.hCursor = OS.LoadCursor(0, OS.IDC_ARROW); wc.hbrBackground = OS.GetStockObject(OS.COLOR_WINDOW + 1 ); wc.lpszClassName = OS.HeapAlloc(OS.GetProcessHeap(), OS.HEAP_ZERO_MEMORY, className.length() * 2); OS.MoveMemory(wc.lpszClassName, className, className.length() * 2); OS.RegisterClass(wc); int hWnd = OS.CreateWindowEx( 0, className, windowName, OS.WS_OVERLAPPEDWINDOW, OS.CW_USEDEFAULT, OS.CW_USEDEFAULT, 640, 480, 0, 0, hInstance, null); OS.ShowWindow(hWnd, OS.SW_SHOW); OS.UpdateWindow(hWnd); MSG lpMsg = new MSG(); while (OS.GetMessage(lpMsg, 0, 0, 0)) { OS.TranslateMessage(lpMsg); OS.DispatchMessage(lpMsg); } } public static void main(String[] args) { WinMain( args ); } }
コンパイル&実行方法
C:¥> javac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.java C:¥> java -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(SWT) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(UWSC) World!
Posted on 7月 17th, 2012 by cx20
Win32 GUI(UWSC)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は UWSC にて PowerShell を呼び出す機能により C# を使用した Win32 GUI アプリケーション の例となっている。ソースコード(UWSC + PowerShell + C# + Win32)
Option Explicit Main() Procedure Main() PowerShell( helloBlock, True, True ) FEnd TextBlock helloBlock $source = @" using System; using System.Runtime.InteropServices; public class Hello { [StructLayout(LayoutKind.Sequential)] struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; } delegate IntPtr WndProcDelgate(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct WNDCLASSEX { public uint cbSize; public uint style; public WndProcDelgate lpfnWndProc; public Int32 cbClsExtra; public Int32 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)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] struct PAINTSTRUCT { public IntPtr hdc; public int fErase; public RECT rcPaint; public int fRestore; public int fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgbReserved; } const uint WS_OVERLAPPED = 0x00000000; const uint WS_POPUP = 0x80000000; const uint WS_CHILD = 0x40000000; const uint WS_MINIMIZE = 0x20000000; const uint WS_VISIBLE = 0x10000000; const uint WS_DISABLED = 0x08000000; const uint WS_CLIPSIBLINGS = 0x04000000; const uint WS_CLIPCHILDREN = 0x02000000; const uint WS_MAXIMIZE = 0x01000000; const uint WS_CAPTION = 0x00C00000; // WS_BORDER | WS_DLGFRAME const uint WS_BORDER = 0x00800000; const uint WS_DLGFRAME = 0x00400000; const uint WS_VSCROLL = 0x00200000; const uint WS_HSCROLL = 0x00100000; const uint WS_SYSMENU = 0x00080000; const uint WS_THICKFRAME = 0x00040000; const uint WS_GROUP = 0x00020000; const uint WS_TABSTOP = 0x00010000; const uint WS_MINIMIZEBOX = 0x00020000; const uint WS_MAXIMIZEBOX = 0x00010000; const uint WS_TILED = WS_OVERLAPPED; const uint WS_ICONIC = WS_MINIMIZE; const uint WS_SIZEBOX = WS_THICKFRAME; const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW; const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; const uint WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU; const uint WS_CHILDWINDOW = WS_CHILD; const uint WM_CREATE = 0x0001; const uint WM_DESTROY = 0x0002; const uint WM_PAINT = 0x000F; const uint WM_CLOSE = 0x0010; const uint WM_COMMAND = 0x0111; const uint COLOR_WINDOW = 5; const uint COLOR_BTNFACE = 15; const uint CS_VREDRAW = 0x0001; const uint CS_HREDRAW = 0x0002; const int CW_USEDEFAULT = -2147483648; // ((uint)0x80000000) const uint SW_SHOWDEFAULT = 10; const int IDI_APPLICATION = 32512; const int IDC_ARROW = 32512; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern short RegisterClassEx(ref WNDCLASSEX pcWndClassEx); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool UpdateWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int TranslateMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DispatchMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern void PostQuitMessage(int nExitCode); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); [DllImport("gdi32.dll", CharSet = CharSet.Auto)] static extern IntPtr TextOut( IntPtr hdc, int x, int y, string lpString, int nCount ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr GetModuleHandle(string lpModuleName); static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) { PAINTSTRUCT ps = new PAINTSTRUCT(); IntPtr hdc; string strMessage = "Hello, Win32 GUI(UWSC) World!"; switch (uMsg) { case WM_PAINT: hdc = BeginPaint( hWnd, out ps ); TextOut( hdc, 0, 0, strMessage, strMessage.Length ); EndPaint( hWnd, ref ps ); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return IntPtr.Zero; } static int WinMain() { IntPtr hInstance = Marshal.GetHINSTANCE(typeof(Hello).Module); const string CLASS_NAME = "helloWindow"; const string WINDOW_NAME = "Hello, World!"; WNDCLASSEX wcex = new WNDCLASSEX(); wcex.cbSize = (uint)Marshal.SizeOf(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = new WndProcDelgate(WndProc); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, new IntPtr(IDI_APPLICATION)); wcex.hCursor = LoadIcon(hInstance, new IntPtr(IDC_ARROW)); wcex.hbrBackground = new IntPtr(COLOR_WINDOW + 1); wcex.lpszMenuName = ""; wcex.lpszClassName = CLASS_NAME; wcex.hIconSm = IntPtr.Zero; RegisterClassEx(ref wcex); IntPtr hWnd = CreateWindowEx( 0, wcex.lpszClassName, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, IntPtr.Zero, IntPtr.Zero, wcex.hInstance, IntPtr.Zero); ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg = new MSG(); while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); } return (int)msg.wParam; } [STAThread] public static int Main() { return WinMain(); } } "@ Add-Type -Language CSharp -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" ) [void][Hello]::Main() EndTextBlock
実行方法
C:¥> uwsc /K hello.uws
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(UWSC) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(Lua) World!
Posted on 7月 16th, 2012 by cx20
Win32 GUI(Lua)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は Lua にて alien ライブラリを使用した Win32 GUI アプリケーション の例となっている。ソースコード
require 'alien' WS_OVERLAPPED = 0x00000000 WS_CAPTION = 0x00C00000 WS_SYSMENU = 0x00080000 WS_THICKFRAME = 0x00040000 WS_MINIMIZEBOX = 0x00020000 WS_MAXIMIZEBOX = 0x00010000 WS_OVERLAPPEDWINDOW = WS_OVERLAPPED + WS_CAPTION + WS_SYSMENU + WS_THICKFRAME + WS_MINIMIZEBOX + WS_MAXIMIZEBOX WM_DESTROY = 0x0002 WM_SIZE = 0x0005 WM_CLOSE = 0x0010 WM_PAINT = 0x000F CW_USEDEFAULT = 0x80000000 SW_SHOWNORMAL = 1 BLACK_BRUSH = 4 COLOR_WINDOW = 5 POINTAPI = alien.defstruct{ {"x", "long"}; {"y", "long"}; } MSG = alien.defstruct{ {"hwnd", "pointer"}; {"message", "uint"}; {"wParam", "pointer"}; {"lParam", "pointer"}; {"time", "ulong"}; {"x", "long"}; {"y", "long"}; } WNDCLASSEX = alien.defstruct{ {"cbSize", "uint"}; {"style", "uint"}; {"lpfnWndProc", "callback"}; {"cbClsExtra", "int"}; {"cbWndExtra", "int"}; {"hInstance", "pointer"}; {"hIcon", "pointer"}; {"hCursor", "pointer"}; {"hbrBackground", "pointer"}; {"lpszMenuName", "string"}; {"lpszClassName", "string"}; {"hIconSm", "pointer"}; } RECT = alien.defstruct{ {"Left", "long"}; {"Top", "long"}; {"Right", "long"}; {"Bottom", "long"}; } PAINTSTRUCT = alien.defstruct{ {"hdc", "uint"}; {"fErase", "int"}; {"rcPaint_left", "long"}; {"rcPaint_top", "long"}; {"rcPaint_right", "long"}; {"rcPaint_bottom", "long"}; {"fRestore", "int"}; {"fIncUpdate", "int"}; --[[ {"rgbReserved", "byte[32]"}; ]] } PAINTSTRUCT.size = PAINTSTRUCT.size + 32 RegisterClassEx = alien.user32.RegisterClassExA; RegisterClassEx:types{ ret = "ushort", abi = "stdcall"; "pointer" } DestroyWindow = alien.user32.DestroyWindow; DestroyWindow:types{ ret = "int", abi = "stdcall"; "pointer" } PostQuitMessage = alien.user32.PostQuitMessage; PostQuitMessage:types{ ret = "void", abi = "stdcall"; "int" } DefWindowProc = alien.user32.DefWindowProcA; DefWindowProc:types{ ret = "long", abi = "stdcall"; "pointer", "uint", "uint", "long" } GetMessage = alien.user32.GetMessageA; GetMessage:types{ ret = "int", abi = "stdcall"; "pointer", "pointer", "uint", "uint" } TranslateMessage = alien.user32.TranslateMessage; TranslateMessage:types{ ret = "int", abi = "stdcall"; "pointer" } DispatchMessage = alien.user32.DispatchMessageA; DispatchMessage:types{ ret = "int", abi = "stdcall"; "pointer" } CreateWindowEx = alien.user32.CreateWindowExA; CreateWindowEx:types{ ret = "pointer", abi = "stdcall"; "ulong", "string", "string", "ulong", "int", "int", "int", "int", "pointer", "pointer", "pointer" } ShowWindow = alien.user32.ShowWindow; ShowWindow:types{ ret = "int", abi = "stdcall"; "pointer", "int" } UpdateWindow = alien.user32.UpdateWindow; UpdateWindow:types{ ret = "int", abi = "stdcall"; "pointer" } BeginPaint = alien.user32.BeginPaint; BeginPaint:types{ ret = "pointer", abi = "stdcall"; "pointer", "pointer" } EndPaint = alien.user32.EndPaint; EndPaint:types{ ret = "int", abi = "stdcall"; "long", "pointer" } TextOut = alien.gdi32.TextOutA; TextOut:types{ ret = "int", abi = "stdcall"; "pointer", "int", "int", "string", "int" } GetStockObject = alien.gdi32.GetStockObject; GetStockObject:types{ ret = "pointer", abi = "stdcall"; "int" } CLASS_NAME = "helloWindow" WINDOW_NAME = "Hello, World" function WndProc(hwnd, uMsg, wParam, lParam) ps = PAINTSTRUCT:new() strMessage = string.format("Hello, Win32 GUI(Lua) World!") if (uMsg == WM_DESTROY) then PostQuitMessage(0) elseif (uMsg == WM_PAINT) then hdc = BeginPaint( hwnd, ps() ) TextOut( hdc, 0, 0, strMessage, string.len(strMessage) ) EndPaint( hwnd, ps() ) else return DefWindowProc(hwnd, uMsg, wParam, lParam) end return nil end function WinMain() wcex = WNDCLASSEX:new() wcex.cbSize = WNDCLASSEX.size wcex.style = 0 wcex.lpfnWndProc = alien.callback(WndProc, { ret = "long", abi = "stdcall"; "pointer", "uint", "uint", "long" }) wcex.cbClsExtra = 0 wcex.cbWndExtra = 0 wcex.hInstance = nil wcex.hIcon = nil wcex.hCursor = nil wcex.hbrBackground = GetStockObject(COLOR_WINDOW+1) wcex.lpszMenuName = nil wcex.lpszClassName = CLASS_NAME wcex.hIconSm = nil RegisterClassEx(wcex()) hwnd = CreateWindowEx( 0, CLASS_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nil, nil, nil) ShowWindow(hwnd, SW_SHOWNORMAL) UpdateWindow(hwnd) msg = MSG:new() msg_ptr = msg() while (GetMessage(msg_ptr, nil, 0, 0) ~= 0) do TranslateMessage(msg_ptr) DispatchMessage(msg_ptr) end end function main() WinMain() end main()
実行方法
C:¥> wlua hello.lua
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(Lua) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(Python) World!
Posted on 7月 15th, 2012 by cx20
Win32 GUI(Python)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は Python にて ctypes ライブラリを使用した Win32 GUI アプリケーション の例となっている。ソースコード
import win32con import sys from ctypes import * WNDPROC = WINFUNCTYPE(c_long, c_int, c_uint, c_int, c_int) NULL = c_int(0) WM_PAINT = win32con.WM_PAINT WM_DESTROY = win32con.WM_DESTROY CS_HREDRAW = win32con.CS_HREDRAW CS_VREDRAW = win32con.CS_VREDRAW IDI_APPLICATION = win32con.IDI_APPLICATION IDC_ARROW = win32con.IDC_ARROW WHITE_BRUSH = win32con.WHITE_BRUSH WS_OVERLAPPEDWINDOW = win32con.WS_OVERLAPPEDWINDOW CW_USEDEFAULT = win32con.CW_USEDEFAULT SW_SHOWNORMAL = win32con.SW_SHOWNORMAL SW_SHOW = win32con.SW_SHOW SW_SHOWDEFAULT = win32con.SW_SHOWDEFAULT GetModuleHandle = windll.kernel32.GetModuleHandleA BeginPaint = windll.user32.BeginPaint EndPaint = windll.user32.EndPaint PostQuitMessage = windll.user32.PostQuitMessage DefWindowProc = windll.user32.DefWindowProcA CreateWindowEx = windll.user32.CreateWindowExA CreateWindowEx.argtypes = [c_int, c_char_p, c_char_p, c_int, c_int, c_int, c_int, c_int, c_int, c_int, c_int, c_int] LoadIcon = windll.user32.LoadIconA LoadCursor = windll.user32.LoadCursorA RegisterClass = windll.user32.RegisterClassA ShowWindow = windll.user32.ShowWindow UpdateWindow = windll.user32.UpdateWindow GetMessage = windll.user32.GetMessageA TranslateMessage = windll.user32.TranslateMessage DispatchMessage = windll.user32.DispatchMessageA TextOut = windll.gdi32.TextOutA GetStockObject = windll.gdi32.GetStockObject class POINT(Structure): _fields_ = [('x', c_long), ('y', c_long)] class MSG(Structure): _fields_ = [('hwnd', c_int), ('message', c_uint), ('wParam', c_int), ('lParam', c_int), ('time', c_int), ('pt', POINT)] class WNDCLASS(Structure): _fields_ = [('style', c_uint), ('lpfnWndProc', WNDPROC), ('cbClsExtra', c_int), ('cbWndExtra', c_int), ('hInstance', c_int), ('hIcon', c_int), ('hCursor', c_int), ('hbrBackground', c_int), ('lpszMenuName', c_char_p), ('lpszClassName', c_char_p)] class RECT(Structure): _fields_ = [('left', c_long), ('top', c_long), ('right', c_long), ('bottom', c_long)] class PAINTSTRUCT(Structure): _fields_ = [('hdc', c_int), ('fErase', c_int), ('rcPaint', RECT), ('fRestore', c_int), ('fIncUpdate', c_int), ('rgbReserved', c_char * 32)] def WndProc(hwnd, message, wParam, lParam): strMessage = "Hello, Win32 GUI(Python) World!" if message == WM_PAINT: ps = PAINTSTRUCT() hdc = BeginPaint( hwnd, byref(ps) ) TextOut( hdc, 0, 0, strMessage, len(strMessage) ) EndPaint( hwnd, byref(ps) ) return 0 elif message == WM_DESTROY: PostQuitMessage(0) return 0 return DefWindowProc(hwnd, message, wParam, lParam) def WinMain(): wndclass = WNDCLASS() wndclass.style = CS_HREDRAW | CS_VREDRAW wndclass.lpfnWndProc = WNDPROC(WndProc) wndclass.cbClsExtra = 0 wndclass.cbWndExtra = 0 wndclass.hInstance = GetModuleHandle(0) wndclass.hIcon = LoadIcon(0, IDI_APPLICATION) wndclass.hCursor = LoadCursor(0, IDC_ARROW) wndclass.hbrBackground = GetStockObject(WHITE_BRUSH) wndclass.lpszMenuName = 0 wndclass.lpszClassName = "helloWindow" RegisterClass(byref(wndclass)) hwnd = CreateWindowEx( 0, wndclass.lpszClassName, "Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, wndclass.hInstance, 0) ShowWindow(hwnd, SW_SHOWDEFAULT) UpdateWindow(hwnd) msg = MSG() pMsg = pointer(msg) while GetMessage( pMsg, NULL, 0, 0 ) != 0: TranslateMessage(pMsg) DispatchMessage(pMsg) return msg.wParam if __name__=='__main__': sys.exit(WinMain())
実行方法
C:¥> python hello.py
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(Python) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(Ruby) World!
Posted on 7月 14th, 2012 by cx20
Win32 GUI(Ruby)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は Ruby による Win32 GUI アプリケーション の例となっている。
なお、Win32 の構造体の定義に CStruct ライブラリを使用している。ソースコード
require 'win32/api' require 'cstruct/win32struct' include Win32 WS_VISIBLE = 0x10000000 WS_CAPTION = 0x00C00000 WS_SYSMENU = 0x00080000 WS_MINIMIZEBOX = 0x00020000 WS_MAXIMIZEBOX = 0x00010000 WS_THICKFRAME = 0x00040000 WS_OVERLAPPED = 0x00000000 WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) WM_CREATE = 0x0001 WM_DESTROY = 0x0002 WM_PAINT = 0x000F WM_CLOSE = 0x0010 WM_COMMAND = 0x0111 CS_VREDRAW = 0x0001 CS_HREDRAW = 0x0002 COLOR_WINDOW = 5 CW_USEDEFAULT = -2147483648 #0x80000000 SW_SHOW = 5 SW_SHOWDEFAULT = 10 Memset = API.new('memset', 'PLL', 'L', 'msvcrt') LocalAlloc = API.new('LocalAlloc', 'LL', 'L', 'kernel32') GetModuleHandle = API.new('GetModuleHandle', 'P', 'L', 'kernel32') RegisterClass = API.new('RegisterClass', 'P', 'L', 'user32') RegisterClassEx = API.new('RegisterClassEx', 'P', 'L', 'user32') CloseWindow = API.new('CloseWindow', 'L', 'B', 'user32') CreateWindowEx = API.new('CreateWindowEx', 'LPPLIIIILLLL', 'L', 'user32') ShowWindow = API.new('ShowWindow', 'LL', 'B', 'user32') UpdateWindow = API.new('UpdateWindow', 'L', 'B', 'user32') BeginPaint = API.new('BeginPaint', 'LP', 'L', 'user32') EndPaint = API.new('EndPaint', 'LP', 'L', 'user32') PostQuitMessage = API.new('PostQuitMessage', 'I', 'V', 'user32') DefWindowProc = API.new('DefWindowProc', 'LLLL', 'L', 'user32') GetMessage = API.new('GetMessage', 'PLII', 'L', 'user32') DispatchMessage = API.new('DispatchMessage', 'P', 'L', 'user32') TranslateMessage = API.new('TranslateMessage', 'P', 'B', 'user32') TextOut = API.new('TextOut', 'LLLPL', 'B', 'gdi32' ); class POINT < Win32Struct LONG :x LONG :y end class MSG < Win32Struct uint32 :hwnd UINT :message WPARAM :wParam LPARAM :lParam DWORD :time POINT :pt end class WNDCLASSA < Win32Struct UINT :style uint32 :lpfnWndProc int32 :cbClsExtra int32 :cbWndExtra HINSTANCE :hInstance HICON :hIcon HCURSOR :hCursor HBRUSH :hbrBackground LPCSTR :lpszMenuName LPCSTR :lpszClassName end class RECT < Win32Struct LONG :left LONG :top LONG :right LONG :bottom end class PAINTSTRUCT < Win32Struct HDC :hdc BOOL :fErase RECT :rcPaint BOOL :fRestore BOOL :fIncUpdate BYTE :rgbReserved,[32] end @window_proc = API::Callback.new('LLLL', 'I') do |hwnd, msg, wparam, lparam| ret = 0 strMessage = "Hello, Win32 GUI(Ruby) World!" case msg when WM_PAINT ps = PAINTSTRUCT.new hdc = BeginPaint.call(hwnd, ps.data) TextOut.call(hdc, 0, 0, strMessage, strMessage.length); EndPaint.call(hwnd, ps.data) when WM_DESTROY PostQuitMessage.call(0) else ret = DefWindowProc.call(hwnd, msg, wparam, lparam) end ret end def alloc_string_buffer init_string, max_length buffer_pointer = LocalAlloc.call(0, max_length) buffer_addr = buffer_pointer Memset.call(buffer_addr, 0, max_length) init_string.each_byte do |byte| Memset.call(buffer_pointer, byte, 1) buffer_pointer += 1 end buffer_addr end def WinMain instance, cmd_show msg = MSG.new wc = WNDCLASSA.new wc.style = 0 wc.lpfnWndProc = @window_proc.address wc.cbClsExtra = 0 wc.cbWndExtra = 0 wc.hInstance = instance wc.hIcon = 0 wc.hCursor = 0 wc.hbrBackground = (COLOR_WINDOW+1) wc.lpszMenuName = 0 wc.lpszClassName = alloc_string_buffer 'helloWindow', 256 RegisterClass.call(wc.data) hwnd = CreateWindowEx.call( 0, "helloWindow", "Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, wc.hInstance, 0 ) ShowWindow.call(hwnd, cmd_show) UpdateWindow.call(hwnd) while(GetMessage.call(msg.data, 0, 0, 0) > 0) TranslateMessage.call(msg.data) DispatchMessage.call(msg.data) end end def main instance = GetModuleHandle.call(0) WinMain instance, SW_SHOW end main
ライブラリ導入方法
C:¥> gem install cstruct
実行方法
C:¥> ruby hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(Ruby) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(Perl) World!
Posted on 7月 13th, 2012 by cx20
Win32 GUI(Perl)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は Perl にて Win32::GUI ライブラリを使用した Win32 GUI アプリケーション の例となっている。ソースコード
use Win32::GUI(); $main = Win32::GUI::Window->new( -text => "Hello, world" ); $label = $main->AddLabel( -text => "Hello, Win32 GUI(Perl) World!" ); $main->Resize(640, 480); $main->Show(); Win32::GUI::Dialog();
実行方法
C:¥> perl hello.pl
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(Perl) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(PowerShell) World!
Posted on 7月 12th, 2012 by cx20
Win32 GUI(PowerShell)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は PowerShell にて Add-Type コマンドレット経由で C# を呼び出すことにより Win32 API を使用した GUI アプリケーション の例となっている。ソースコード
$source = @" using System; using System.Runtime.InteropServices; public class Hello { [StructLayout(LayoutKind.Sequential)] struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; } delegate IntPtr WndProcDelgate(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct WNDCLASSEX { public uint cbSize; public uint style; public WndProcDelgate lpfnWndProc; public Int32 cbClsExtra; public Int32 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)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] struct PAINTSTRUCT { public IntPtr hdc; public int fErase; public RECT rcPaint; public int fRestore; public int fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgbReserved; } const uint WS_OVERLAPPED = 0x00000000; const uint WS_POPUP = 0x80000000; const uint WS_CHILD = 0x40000000; const uint WS_MINIMIZE = 0x20000000; const uint WS_VISIBLE = 0x10000000; const uint WS_DISABLED = 0x08000000; const uint WS_CLIPSIBLINGS = 0x04000000; const uint WS_CLIPCHILDREN = 0x02000000; const uint WS_MAXIMIZE = 0x01000000; const uint WS_CAPTION = 0x00C00000; // WS_BORDER | WS_DLGFRAME const uint WS_BORDER = 0x00800000; const uint WS_DLGFRAME = 0x00400000; const uint WS_VSCROLL = 0x00200000; const uint WS_HSCROLL = 0x00100000; const uint WS_SYSMENU = 0x00080000; const uint WS_THICKFRAME = 0x00040000; const uint WS_GROUP = 0x00020000; const uint WS_TABSTOP = 0x00010000; const uint WS_MINIMIZEBOX = 0x00020000; const uint WS_MAXIMIZEBOX = 0x00010000; const uint WS_TILED = WS_OVERLAPPED; const uint WS_ICONIC = WS_MINIMIZE; const uint WS_SIZEBOX = WS_THICKFRAME; const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW; const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; const uint WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU; const uint WS_CHILDWINDOW = WS_CHILD; const uint WM_CREATE = 0x0001; const uint WM_DESTROY = 0x0002; const uint WM_PAINT = 0x000F; const uint WM_CLOSE = 0x0010; const uint WM_COMMAND = 0x0111; const uint COLOR_WINDOW = 5; const uint COLOR_BTNFACE = 15; const uint CS_VREDRAW = 0x0001; const uint CS_HREDRAW = 0x0002; const int CW_USEDEFAULT = -2147483648; // ((uint)0x80000000) const uint SW_SHOWDEFAULT = 10; const int IDI_APPLICATION = 32512; const int IDC_ARROW = 32512; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern short RegisterClassEx(ref WNDCLASSEX pcWndClassEx); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool UpdateWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int TranslateMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DispatchMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern void PostQuitMessage(int nExitCode); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); [DllImport("gdi32.dll", CharSet = CharSet.Auto)] static extern IntPtr TextOut( IntPtr hdc, int x, int y, string lpString, int nCount ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr GetModuleHandle(string lpModuleName); static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) { PAINTSTRUCT ps = new PAINTSTRUCT(); IntPtr hdc; string strMessage = "Hello, Win32 GUI(PowerShell) World!"; switch (uMsg) { case WM_PAINT: hdc = BeginPaint( hWnd, out ps ); TextOut( hdc, 0, 0, strMessage, strMessage.Length ); EndPaint( hWnd, ref ps ); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return IntPtr.Zero; } static int WinMain() { IntPtr hInstance = Marshal.GetHINSTANCE(typeof(Hello).Module); const string CLASS_NAME = "helloWindow"; const string WINDOW_NAME = "Hello, World!"; WNDCLASSEX wcex = new WNDCLASSEX(); wcex.cbSize = (uint)Marshal.SizeOf(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = new WndProcDelgate(WndProc); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, new IntPtr(IDI_APPLICATION)); wcex.hCursor = LoadIcon(hInstance, new IntPtr(IDC_ARROW)); wcex.hbrBackground = new IntPtr(COLOR_WINDOW + 1); wcex.lpszMenuName = ""; wcex.lpszClassName = CLASS_NAME; wcex.hIconSm = IntPtr.Zero; RegisterClassEx(ref wcex); IntPtr hWnd = CreateWindowEx( 0, wcex.lpszClassName, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, IntPtr.Zero, IntPtr.Zero, wcex.hInstance, IntPtr.Zero); ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg = new MSG(); while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); } return (int)msg.wParam; } [STAThread] public static int Main() { return WinMain(); } } "@ Add-Type -Language CSharp -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" ) [void][Hello]::Main()
実行方法
C:¥> powershell -file Hello.ps1
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(PowerShell) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(MSIL) World!
Posted on 7月 11th, 2012 by cx20
Win32 GUI(MSIL)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は MSIL において Win32 API を使用した GUI アプリケーション の例となっている。ソースコード
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.1 // Copyright (c) Microsoft Corporation. All rights reserved. // Metadata version: v2.0.50727 .module extern user32.dll .module extern gdi32.dll .module extern kernel32.dll .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .zV.4.. .ver 2:0:0:0 } .assembly Hello { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. .hash algorithm 0x00008004 .ver 0:0:0:0 } .module Hello.exe // MVID: {BEAF0F56-F7B6-4F51-B26E-C0D153A452A2} .imagebase 0x00400000 .file alignment 0x00000200 .stackreserve 0x00100000 .subsystem 0x0002 // WINDOWS_GUI .corflags 0x00000001 // ILONLY // Image base: 0x00410000 // =============== CLASS MEMBERS DECLARATION =================== .class private auto ansi beforefieldinit Hello extends [mscorlib]System.Object { .class sequential ansi sealed nested private beforefieldinit POINT extends [mscorlib]System.ValueType { .field public int32 x .field public int32 y } // end of class POINT .class sequential ansi sealed nested private beforefieldinit MSG extends [mscorlib]System.ValueType { .field public native int hwnd .field public uint32 message .field public native int wParam .field public native int lParam .field public uint32 time .field public valuetype Hello/POINT pt } // end of class MSG .class auto ansi sealed nested private WndProcDelgate extends [mscorlib]System.MulticastDelegate { .method public hidebysig specialname rtspecialname instance void .ctor(object 'object', native int 'method') runtime managed { } // end of method WndProcDelgate::.ctor .method public hidebysig newslot virtual instance native int Invoke(native int hWnd, uint32 uMsg, native int wParam, native int lParam) runtime managed { } // end of method WndProcDelgate::Invoke .method public hidebysig newslot virtual instance class [mscorlib]System.IAsyncResult BeginInvoke(native int hWnd, uint32 uMsg, native int wParam, native int lParam, class [mscorlib]System.AsyncCallback callback, object 'object') runtime managed { } // end of method WndProcDelgate::BeginInvoke .method public hidebysig newslot virtual instance native int EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed { } // end of method WndProcDelgate::EndInvoke } // end of class WndProcDelgate .class sequential autochar sealed nested private beforefieldinit WNDCLASSEX extends [mscorlib]System.ValueType { .field public uint32 cbSize .field public uint32 style .field public class Hello/WndProcDelgate lpfnWndProc .field public int32 cbClsExtra .field public int32 cbWndExtra .field public native int hInstance .field public native int hIcon .field public native int hCursor .field public native int hbrBackground .field public string lpszMenuName .field public string lpszClassName .field public native int hIconSm } // end of class WNDCLASSEX .class sequential ansi sealed nested private beforefieldinit RECT extends [mscorlib]System.ValueType { .field public int32 Left .field public int32 Top .field public int32 Right .field public int32 Bottom } // end of class RECT .class sequential ansi sealed nested private beforefieldinit PAINTSTRUCT extends [mscorlib]System.ValueType { .field public native int hdc .field public int32 fErase .field public valuetype Hello/RECT rcPaint .field public int32 fRestore .field public int32 fIncUpdate .field public marshal( fixed array [32]) uint8[] rgbReserved } // end of class PAINTSTRUCT .field private static literal uint32 WS_OVERLAPPED = uint32(0x00000000) .field private static literal uint32 WS_POPUP = uint32(0x80000000) .field private static literal uint32 WS_CHILD = uint32(0x40000000) .field private static literal uint32 WS_MINIMIZE = uint32(0x20000000) .field private static literal uint32 WS_VISIBLE = uint32(0x10000000) .field private static literal uint32 WS_DISABLED = uint32(0x08000000) .field private static literal uint32 WS_CLIPSIBLINGS = uint32(0x04000000) .field private static literal uint32 WS_CLIPCHILDREN = uint32(0x02000000) .field private static literal uint32 WS_MAXIMIZE = uint32(0x01000000) .field private static literal uint32 WS_CAPTION = uint32(0x00C00000) .field private static literal uint32 WS_BORDER = uint32(0x00800000) .field private static literal uint32 WS_DLGFRAME = uint32(0x00400000) .field private static literal uint32 WS_VSCROLL = uint32(0x00200000) .field private static literal uint32 WS_HSCROLL = uint32(0x00100000) .field private static literal uint32 WS_SYSMENU = uint32(0x00080000) .field private static literal uint32 WS_THICKFRAME = uint32(0x00040000) .field private static literal uint32 WS_GROUP = uint32(0x00020000) .field private static literal uint32 WS_TABSTOP = uint32(0x00010000) .field private static literal uint32 WS_MINIMIZEBOX = uint32(0x00020000) .field private static literal uint32 WS_MAXIMIZEBOX = uint32(0x00010000) .field private static literal uint32 WS_TILED = uint32(0x00000000) .field private static literal uint32 WS_ICONIC = uint32(0x20000000) .field private static literal uint32 WS_SIZEBOX = uint32(0x00040000) .field private static literal uint32 WS_TILEDWINDOW = uint32(0x00CF0000) .field private static literal uint32 WS_OVERLAPPEDWINDOW = uint32(0x00CF0000) .field private static literal uint32 WS_POPUPWINDOW = uint32(0x80880000) .field private static literal uint32 WS_CHILDWINDOW = uint32(0x40000000) .field private static literal uint32 WM_CREATE = uint32(0x00000001) .field private static literal uint32 WM_DESTROY = uint32(0x00000002) .field private static literal uint32 WM_PAINT = uint32(0x0000000F) .field private static literal uint32 WM_CLOSE = uint32(0x00000010) .field private static literal uint32 WM_COMMAND = uint32(0x00000111) .field private static literal uint32 COLOR_WINDOW = uint32(0x00000005) .field private static literal uint32 COLOR_BTNFACE = uint32(0x0000000F) .field private static literal uint32 CS_VREDRAW = uint32(0x00000001) .field private static literal uint32 CS_HREDRAW = uint32(0x00000002) .field private static literal int32 CW_USEDEFAULT = int32(0x80000000) .field private static literal uint32 SW_SHOWDEFAULT = uint32(0x0000000A) .field private static literal int32 IDI_APPLICATION = int32(0x00007F00) .field private static literal int32 IDC_ARROW = int32(0x00007F00) .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int LoadCursor(native int hInstance, native int lpCursorName) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int LoadIcon(native int hInstance, native int lpIconName) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) int16 RegisterClassEx(valuetype Hello/WNDCLASSEX& pcWndClassEx) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int CreateWindowEx(uint32 dwExStyle, string lpClassName, string lpWindowName, uint32 dwStyle, int32 x, int32 y, int32 nWidth, int32 nHeight, native int hWndParent, native int hMenu, native int hInstance, native int lpParam) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) bool ShowWindow(native int hWnd, uint32 nCmdShow) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) bool UpdateWindow(native int hWnd) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) int32 GetMessage([out] valuetype Hello/MSG& lpMsg, native int hWnd, uint32 wMsgFilterMin, uint32 wMsgFilterMax) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) int32 TranslateMessage([in] valuetype Hello/MSG& lpMsg) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int DispatchMessage([in] valuetype Hello/MSG& lpMsg) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) void PostQuitMessage(int32 nExitCode) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int DefWindowProc(native int hWnd, uint32 uMsg, native int wParam, native int lParam) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int BeginPaint(native int hWnd, [out] valuetype Hello/PAINTSTRUCT& lpPaint) cil managed preservesig { } .method private hidebysig static pinvokeimpl("user32.dll" autochar winapi) native int EndPaint(native int hWnd, valuetype Hello/PAINTSTRUCT& lpPaint) cil managed preservesig { } .method private hidebysig static pinvokeimpl("gdi32.dll" autochar winapi) native int TextOut(native int hdc, int32 x, int32 y, string lpString, int32 nCount) cil managed preservesig { } .method private hidebysig static pinvokeimpl("kernel32.dll" autochar winapi) native int GetModuleHandle(string lpModuleName) cil managed preservesig { } .method private hidebysig static native int WndProc(native int hWnd, uint32 uMsg, native int wParam, native int lParam) cil managed { // コード サイズ 98 (0x62) .maxstack 5 .locals init (valuetype Hello/PAINTSTRUCT V_0, native int V_1, string V_2, native int V_3, uint32 V_4) IL_0000: nop IL_0001: ldloca.s V_0 IL_0003: initobj Hello/PAINTSTRUCT IL_0009: ldstr "Hello, Win32 GUI(MSIL) World!" IL_000e: stloc.2 IL_000f: ldarg.1 IL_0010: stloc.s V_4 IL_0012: ldloc.s V_4 IL_0014: ldc.i4.2 IL_0015: beq.s IL_0043 IL_0017: ldloc.s V_4 IL_0019: ldc.i4.s 15 IL_001b: beq.s IL_001f IL_001d: br.s IL_004c IL_001f: ldarg.0 IL_0020: ldloca.s V_0 IL_0022: call native int Hello::BeginPaint(native int, valuetype Hello/PAINTSTRUCT&) IL_0027: stloc.1 IL_0028: ldloc.1 IL_0029: ldc.i4.0 IL_002a: ldc.i4.0 IL_002b: ldloc.2 IL_002c: ldloc.2 IL_002d: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0032: call native int Hello::TextOut(native int, int32, int32, string, int32) IL_0037: pop IL_0038: ldarg.0 IL_0039: ldloca.s V_0 IL_003b: call native int Hello::EndPaint(native int, valuetype Hello/PAINTSTRUCT&) IL_0040: pop IL_0041: br.s IL_0058 IL_0043: ldc.i4.0 IL_0044: call void Hello::PostQuitMessage(int32) IL_0049: nop IL_004a: br.s IL_0058 IL_004c: ldarg.0 IL_004d: ldarg.1 IL_004e: ldarg.2 IL_004f: ldarg.3 IL_0050: call native int Hello::DefWindowProc(native int, uint32, native int, native int) IL_0055: stloc.3 IL_0056: br.s IL_0060 IL_0058: ldsfld native int [mscorlib]System.IntPtr::Zero IL_005d: stloc.3 IL_005e: br.s IL_0060 IL_0060: ldloc.3 IL_0061: ret } // end of method Hello::WndProc .method private hidebysig static int32 WinMain(string[] args) cil managed { // コード サイズ 358 (0x166) .maxstack 12 .locals init (native int V_0, valuetype Hello/WNDCLASSEX V_1, native int V_2, valuetype Hello/MSG V_3, int32 V_4, bool V_5) IL_0000: nop IL_0001: ldtoken Hello IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) IL_000b: callvirt instance class [mscorlib]System.Reflection.Module [mscorlib]System.Type::get_Module() IL_0010: call native int [mscorlib]System.Runtime.InteropServices.Marshal::GetHINSTANCE(class [mscorlib]System.Reflection.Module) IL_0015: stloc.0 IL_0016: ldloca.s V_1 IL_0018: initobj Hello/WNDCLASSEX IL_001e: ldloca.s V_1 IL_0020: ldloc.1 IL_0021: box Hello/WNDCLASSEX IL_0026: call int32 [mscorlib]System.Runtime.InteropServices.Marshal::SizeOf(object) IL_002b: stfld uint32 Hello/WNDCLASSEX::cbSize IL_0030: ldloca.s V_1 IL_0032: ldc.i4.3 IL_0033: stfld uint32 Hello/WNDCLASSEX::style IL_0038: ldloca.s V_1 IL_003a: ldnull IL_003b: ldftn native int Hello::WndProc(native int, uint32, native int, native int) IL_0041: newobj instance void Hello/WndProcDelgate::.ctor(object, native int) IL_0046: stfld class Hello/WndProcDelgate Hello/WNDCLASSEX::lpfnWndProc IL_004b: ldloca.s V_1 IL_004d: ldc.i4.0 IL_004e: stfld int32 Hello/WNDCLASSEX::cbClsExtra IL_0053: ldloca.s V_1 IL_0055: ldc.i4.0 IL_0056: stfld int32 Hello/WNDCLASSEX::cbWndExtra IL_005b: ldloca.s V_1 IL_005d: ldloc.0 IL_005e: stfld native int Hello/WNDCLASSEX::hInstance IL_0063: ldloca.s V_1 IL_0065: ldloc.0 IL_0066: ldc.i4 0x7f00 IL_006b: newobj instance void [mscorlib]System.IntPtr::.ctor(int32) IL_0070: call native int Hello::LoadIcon(native int, native int) IL_0075: stfld native int Hello/WNDCLASSEX::hIcon IL_007a: ldloca.s V_1 IL_007c: ldloc.0 IL_007d: ldc.i4 0x7f00 IL_0082: newobj instance void [mscorlib]System.IntPtr::.ctor(int32) IL_0087: call native int Hello::LoadIcon(native int, native int) IL_008c: stfld native int Hello/WNDCLASSEX::hCursor IL_0091: ldloca.s V_1 IL_0093: ldc.i4.6 IL_0094: conv.i8 IL_0095: newobj instance void [mscorlib]System.IntPtr::.ctor(int64) IL_009a: stfld native int Hello/WNDCLASSEX::hbrBackground IL_009f: ldloca.s V_1 IL_00a1: ldstr "" IL_00a6: stfld string Hello/WNDCLASSEX::lpszMenuName IL_00ab: ldloca.s V_1 IL_00ad: ldstr "helloWindow" IL_00b2: stfld string Hello/WNDCLASSEX::lpszClassName IL_00b7: ldloca.s V_1 IL_00b9: ldsfld native int [mscorlib]System.IntPtr::Zero IL_00be: stfld native int Hello/WNDCLASSEX::hIconSm IL_00c3: ldloca.s V_1 IL_00c5: call int16 Hello::RegisterClassEx(valuetype Hello/WNDCLASSEX&) IL_00ca: pop IL_00cb: ldc.i4.0 IL_00cc: ldloca.s V_1 IL_00ce: ldfld string Hello/WNDCLASSEX::lpszClassName IL_00d3: ldstr "Hello, World!" IL_00d8: ldc.i4 0xcf0000 IL_00dd: ldc.i4 0x80000000 IL_00e2: ldc.i4 0x80000000 IL_00e7: ldc.i4 0x280 IL_00ec: ldc.i4 0x1e0 IL_00f1: ldsfld native int [mscorlib]System.IntPtr::Zero IL_00f6: ldsfld native int [mscorlib]System.IntPtr::Zero IL_00fb: ldloca.s V_1 IL_00fd: ldfld native int Hello/WNDCLASSEX::hInstance IL_0102: ldsfld native int [mscorlib]System.IntPtr::Zero IL_0107: call native int Hello::CreateWindowEx(uint32, string, string, uint32, int32, int32, int32, int32, native int, native int, native int, native int) IL_010c: stloc.2 IL_010d: ldloc.2 IL_010e: ldc.i4.s 10 IL_0110: call bool Hello::ShowWindow(native int, uint32) IL_0115: pop IL_0116: ldloc.2 IL_0117: call bool Hello::UpdateWindow(native int) IL_011c: pop IL_011d: ldloca.s V_3 IL_011f: initobj Hello/MSG IL_0125: br.s IL_0139 IL_0127: nop IL_0128: ldloca.s V_3 IL_012a: call int32 Hello::TranslateMessage(valuetype Hello/MSG&) IL_012f: pop IL_0130: ldloca.s V_3 IL_0132: call native int Hello::DispatchMessage(valuetype Hello/MSG&) IL_0137: pop IL_0138: nop IL_0139: ldloca.s V_3 IL_013b: ldsfld native int [mscorlib]System.IntPtr::Zero IL_0140: ldc.i4.0 IL_0141: ldc.i4.0 IL_0142: call int32 Hello::GetMessage(valuetype Hello/MSG&, native int, uint32, uint32) IL_0147: ldc.i4.0 IL_0148: ceq IL_014a: ldc.i4.0 IL_014b: ceq IL_014d: stloc.s V_5 IL_014f: ldloc.s V_5 IL_0151: brtrue.s IL_0127 IL_0153: ldloca.s V_3 IL_0155: ldfld native int Hello/MSG::wParam IL_015a: call int32 [mscorlib]System.IntPtr::op_Explicit(native int) IL_015f: stloc.s V_4 IL_0161: br.s IL_0163 IL_0163: ldloc.s V_4 IL_0165: ret } // end of method Hello::WinMain .method private hidebysig static int32 Main(string[] args) cil managed { .entrypoint .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) // コード サイズ 12 (0xc) .maxstack 1 .locals init (int32 V_0) IL_0000: nop IL_0001: ldarg.0 IL_0002: call int32 Hello::WinMain(string[]) IL_0007: stloc.0 IL_0008: br.s IL_000a IL_000a: ldloc.0 IL_000b: ret } // end of method Hello::Main .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // コード サイズ 7 (0x7) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: ret } // end of method Hello::.ctor } // end of class Hello // ============================================================= // *********** 逆アセンブルが完了しました *********************** // 警告: Win32 リソース ファイル Hello.res を作成しました。
上記コードは、以下の C# の実行ファイルを ildadm.exe で逆アセンブルしたものである。
ソースコード
using System; using System.Runtime.InteropServices; class Hello { [StructLayout(LayoutKind.Sequential)] struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; } delegate IntPtr WndProcDelgate(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct WNDCLASSEX { public uint cbSize; public uint style; public WndProcDelgate lpfnWndProc; public Int32 cbClsExtra; public Int32 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)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] struct PAINTSTRUCT { public IntPtr hdc; public int fErase; public RECT rcPaint; public int fRestore; public int fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgbReserved; } const uint WS_OVERLAPPED = 0x00000000; const uint WS_POPUP = 0x80000000; const uint WS_CHILD = 0x40000000; const uint WS_MINIMIZE = 0x20000000; const uint WS_VISIBLE = 0x10000000; const uint WS_DISABLED = 0x08000000; const uint WS_CLIPSIBLINGS = 0x04000000; const uint WS_CLIPCHILDREN = 0x02000000; const uint WS_MAXIMIZE = 0x01000000; const uint WS_CAPTION = 0x00C00000; // WS_BORDER | WS_DLGFRAME const uint WS_BORDER = 0x00800000; const uint WS_DLGFRAME = 0x00400000; const uint WS_VSCROLL = 0x00200000; const uint WS_HSCROLL = 0x00100000; const uint WS_SYSMENU = 0x00080000; const uint WS_THICKFRAME = 0x00040000; const uint WS_GROUP = 0x00020000; const uint WS_TABSTOP = 0x00010000; const uint WS_MINIMIZEBOX = 0x00020000; const uint WS_MAXIMIZEBOX = 0x00010000; const uint WS_TILED = WS_OVERLAPPED; const uint WS_ICONIC = WS_MINIMIZE; const uint WS_SIZEBOX = WS_THICKFRAME; const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW; const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; const uint WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU; const uint WS_CHILDWINDOW = WS_CHILD; const uint WM_CREATE = 0x0001; const uint WM_DESTROY = 0x0002; const uint WM_PAINT = 0x000F; const uint WM_CLOSE = 0x0010; const uint WM_COMMAND = 0x0111; const uint COLOR_WINDOW = 5; const uint COLOR_BTNFACE = 15; const uint CS_VREDRAW = 0x0001; const uint CS_HREDRAW = 0x0002; const int CW_USEDEFAULT = -2147483648; // ((uint)0x80000000) const uint SW_SHOWDEFAULT = 10; const int IDI_APPLICATION = 32512; const int IDC_ARROW = 32512; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern short RegisterClassEx(ref WNDCLASSEX pcWndClassEx); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool UpdateWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int TranslateMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DispatchMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern void PostQuitMessage(int nExitCode); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); [DllImport("gdi32.dll", CharSet = CharSet.Auto)] static extern IntPtr TextOut( IntPtr hdc, int x, int y, string lpString, int nCount ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr GetModuleHandle(string lpModuleName); static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) { PAINTSTRUCT ps = new PAINTSTRUCT(); IntPtr hdc; string strMessage = "Hello, Win32 GUI(MSIL) World!"; switch (uMsg) { case WM_PAINT: hdc = BeginPaint( hWnd, out ps ); TextOut( hdc, 0, 0, strMessage, strMessage.Length ); EndPaint( hWnd, ref ps ); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return IntPtr.Zero; } static int WinMain(string[] args) { IntPtr hInstance = Marshal.GetHINSTANCE(typeof(Hello).Module); const string CLASS_NAME = "helloWindow"; const string WINDOW_NAME = "Hello, World!"; WNDCLASSEX wcex = new WNDCLASSEX(); wcex.cbSize = (uint)Marshal.SizeOf(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = new WndProcDelgate(WndProc); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, new IntPtr(IDI_APPLICATION)); wcex.hCursor = LoadIcon(hInstance, new IntPtr(IDC_ARROW)); wcex.hbrBackground = new IntPtr(COLOR_WINDOW + 1); wcex.lpszMenuName = ""; wcex.lpszClassName = CLASS_NAME; wcex.hIconSm = IntPtr.Zero; RegisterClassEx(ref wcex); IntPtr hWnd = CreateWindowEx( 0, wcex.lpszClassName, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, IntPtr.Zero, IntPtr.Zero, wcex.hInstance, IntPtr.Zero); ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg = new MSG(); while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); } return (int)msg.wParam; } [STAThread] static int Main(string[] args) { return WinMain( args ); } }
コンパイル方法
C:¥> ilasm Hello.il
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(MSIL) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(VB.NET) World!
Posted on 7月 10th, 2012 by cx20
Win32 GUI(VB.NET)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は VB.NET において Win32 API を使用した GUI アプリケーション の例となっている。ソースコード
Imports System.Runtime.InteropServices Public Class HelloWindow Const WS_CAPTION As Integer = &HC00000 Const WS_MAXIMIZEBOX As Integer = &H10000 Const WS_MINIMIZEBOX As Integer = &H20000 Const WS_OVERLAPPED As Integer = &H0 Const WS_SYSMENU As Integer = &H80000 Const WS_THICKFRAME As Integer = &H40000 Const WS_OVERLAPPEDWINDOW As Integer = ( _ WS_OVERLAPPED Or _ WS_CAPTION Or _ WS_SYSMENU Or _ WS_THICKFRAME Or _ WS_MINIMIZEBOX Or _ WS_MAXIMIZEBOX) Const COLOR_WINDOW As Integer = 5 Const COLOR_BTNFACE As Integer = 15 Const CS_VREDRAW As Integer = &H1 Const CS_HREDRAW As Integer = &H2 Const CW_USEDEFAULT As Integer = &H80000000 Const IDI_APPLICATION As Integer = 32512 Const IDC_ARROW As Integer = 32512 Const LTGRAY_BRUSH As Integer = 1 Const SW_SHOWNORMAL As Integer = 1 Const SW_SHOWDEFAULT As Integer = 10 Const WM_DESTROY As Integer = &H2 Const WM_PAINT As Integer = &HF Delegate Function WndProcDelgate( _ ByVal hWnd As IntPtr, _ ByVal Message As Integer, _ ByVal wParam As IntPtr, _ ByVal lParam As IntPtr _ ) As IntPtr <StructLayout(LayoutKind.Sequential)> _ Structure POINTAPI Public x As Integer Public y As Integer End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ Structure MSG Public hWnd As IntPtr Public Message As Integer Public wParam As IntPtr Public lParam As IntPtr Public time As Integer Public pt As POINTAPI End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ Structure WNDCLASSEX Public cbSize As Integer Public style As Integer Public lpfnWndProc As WndProcDelgate Public cbClsExtra As Integer Public cbWndExtra As Integer Public hInstance As IntPtr Public hIcon As IntPtr Public hCursor As IntPtr Public hbrBackground As IntPtr Public lpszMenuName As String Public lpszClassName As String Public hIconSm As IntPtr End Structure <StructLayout(LayoutKind.Sequential)> _ Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure <StructLayout(LayoutKind.Sequential)> _ Structure PAINTSTRUCT Public hdc As IntPtr Public fErase As Integer Public rcPaint As RECT Public fRestore As Integer Public fIncUpdate As Integer <MarshalAs(UnmanagedType.ByValArray, SizeConst := 32)> _ Public rgbReserved As Byte() End Structure Declare Auto Function LoadCursor Lib "user32" ( _ ByVal hInstance As IntPtr, _ ByVal lpCursorName As IntPtr _ ) As IntPtr Declare Auto Function LoadIcon Lib "user32" ( _ ByVal hInstance As IntPtr, _ ByVal lpIconName As IntPtr _ ) As IntPtr Declare Auto Function RegisterClassEx Lib "user32" ( _ ByRef pcWndClassEx As WNDCLASSEX _ ) As Integer Declare Auto Function CreateWindowEx Lib "user32" ( _ ByVal dwExStyle As Integer, _ ByVal lpClassName As String, _ ByVal lpWindowName As String, _ ByVal dwStyle As Integer, _ ByVal x As Integer, _ ByVal y As Integer, _ ByVal nWidth As Integer, _ ByVal nHeight As Integer, _ ByVal hWndParent As IntPtr, _ ByVal hMenu As IntPtr, _ ByVal hInstance As IntPtr, _ ByVal lpParam As IntPtr _ ) As IntPtr Declare Function ShowWindow Lib "user32" ( _ ByVal hWnd As IntPtr, _ ByVal nCmdShow As Integer _ ) As Boolean Declare Function UpdateWindow Lib "user32" (ByVal hWnd As IntPtr) As Boolean Declare Auto Function GetMessage Lib "user32" ( _ ByRef lpMsg As MSG, _ ByVal hWnd As IntPtr, _ ByVal wMsgFilterMin As Integer, _ ByVal wMsgFilterMax As Integer _ ) As Boolean Declare Function TranslateMessage Lib "user32" ( _ ByRef lpMsg As MSG _ ) As Boolean Declare Auto Function DispatchMessage Lib "user32" ( _ ByRef lpMsg As MSG _ ) As IntPtr Declare Sub PostQuitMessage Lib "user32" ( _ ByVal nExitCode As Integer _ ) Declare Function BeginPaint Lib "user32" ( _ ByVal hwnd As IntPtr, _ ByRef lpPaint As PAINTSTRUCT _ ) As IntPtr Declare Function EndPaint Lib "user32" ( _ ByVal hwnd As IntPtr, _ ByRef lpPaint As PAINTSTRUCT _ ) As IntPtr Declare Auto Function TextOut Lib "gdi32" ( _ ByVal hdc As IntPtr, _ ByVal x As Integer, _ ByVal y As Integer, _ ByVal lpString As String, _ ByVal nCount As Integer _ ) As Integer Declare Function GetStockObject Lib "gdi32" ( _ ByVal nIndex As Integer _ ) As IntPtr Declare Auto Function DefWindowProc Lib "user32" ( _ ByVal hWnd As IntPtr, _ ByVal wMsg As Integer, _ ByVal wParam As IntPtr, _ ByVal lParam As IntPtr _ ) As IntPtr Function WndProc( _ ByVal hWnd As IntPtr, _ ByVal msg As Integer, _ ByVal wParam As IntPtr, _ ByVal lParam As IntPtr _ ) As IntPtr Dim ps As New PAINTSTRUCT Dim hdc As IntPtr Dim strMessage As String strMessage = "Hello, Win32 GUI(VB.NET) World!" Select Case msg Case WM_PAINT hdc = BeginPaint(hwnd, ps) TextOut( hdc, 0, 0, strMessage, Len(strMessage) ) EndPaint( hwnd, ps ) Case WM_DESTROY PostQuitMessage(0) Case Else Return DefWindowProc(hWnd, msg, wParam, lParam) End Select Return IntPtr.Zero End Function Public Function WinMain() As Integer Const CLASS_NAME As String = "helloWindow" Const WINDOW_NAME As String = "Hello, World!" Dim hInstance As IntPtr = Marshal.GetHINSTANCE(GetType(HelloWindow).Module) Dim hWnd As IntPtr Dim msg As MSG Dim wcex As New WNDCLASSEX With wcex .cbSize = Marshal.SizeOf(wcex) .style = CS_HREDRAW Or CS_VREDRAW .lpfnWndProc = New WndProcDelgate(AddressOf WndProc) .cbClsExtra = 0 .cbWndExtra = 0 .hInstance = hInstance .hIcon = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION)) .hCursor = LoadCursor(hInstance, New IntPtr(IDC_ARROW)) .hbrBackground = New IntPtr(COLOR_WINDOW + 1) .lpszMenuName = Nothing .lpszClassName = CLASS_NAME .hIconSm = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION)) End With RegisterClassEx(wcex) hWnd = CreateWindowEx( _ 0, _ CLASS_NAME, _ WINDOW_NAME, _ WS_OVERLAPPEDWINDOW, _ CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, _ IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero) ShowWindow(hWnd, SW_SHOWDEFAULT) UpdateWindow(hWnd) Do While GetMessage(msg, IntPtr.Zero, 0, 0) TranslateMessage(msg) DispatchMessage(msg) Loop Return CType(msg.wParam, Integer) End Function Public Shared Sub Main() Dim hello As New HelloWindow hello.WinMain() End Sub End Class
コンパイル方法
C:¥> vbc /target:winexe Hello.vb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(VB.NET) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Win32 GUI(C#) World!
Posted on 7月 9th, 2012 by cx20
Win32 GUI(C#)
Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
以下は C# において Win32 API を使用した GUI アプリケーション の例となっている。ソースコード
using System; using System.Runtime.InteropServices; class Hello { [StructLayout(LayoutKind.Sequential)] struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; } delegate IntPtr WndProcDelgate(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct WNDCLASSEX { public uint cbSize; public uint style; public WndProcDelgate lpfnWndProc; public Int32 cbClsExtra; public Int32 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)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential)] struct PAINTSTRUCT { public IntPtr hdc; public int fErase; public RECT rcPaint; public int fRestore; public int fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgbReserved; } const uint WS_OVERLAPPED = 0x00000000; const uint WS_POPUP = 0x80000000; const uint WS_CHILD = 0x40000000; const uint WS_MINIMIZE = 0x20000000; const uint WS_VISIBLE = 0x10000000; const uint WS_DISABLED = 0x08000000; const uint WS_CLIPSIBLINGS = 0x04000000; const uint WS_CLIPCHILDREN = 0x02000000; const uint WS_MAXIMIZE = 0x01000000; const uint WS_CAPTION = 0x00C00000; // WS_BORDER | WS_DLGFRAME const uint WS_BORDER = 0x00800000; const uint WS_DLGFRAME = 0x00400000; const uint WS_VSCROLL = 0x00200000; const uint WS_HSCROLL = 0x00100000; const uint WS_SYSMENU = 0x00080000; const uint WS_THICKFRAME = 0x00040000; const uint WS_GROUP = 0x00020000; const uint WS_TABSTOP = 0x00010000; const uint WS_MINIMIZEBOX = 0x00020000; const uint WS_MAXIMIZEBOX = 0x00010000; const uint WS_TILED = WS_OVERLAPPED; const uint WS_ICONIC = WS_MINIMIZE; const uint WS_SIZEBOX = WS_THICKFRAME; const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW; const uint WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; const uint WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU; const uint WS_CHILDWINDOW = WS_CHILD; const uint WM_CREATE = 0x0001; const uint WM_DESTROY = 0x0002; const uint WM_PAINT = 0x000F; const uint WM_CLOSE = 0x0010; const uint WM_COMMAND = 0x0111; const uint COLOR_WINDOW = 5; const uint COLOR_BTNFACE = 15; const uint CS_VREDRAW = 0x0001; const uint CS_HREDRAW = 0x0002; const int CW_USEDEFAULT = -2147483648; // ((uint)0x80000000) const uint SW_SHOWDEFAULT = 10; const int IDI_APPLICATION = 32512; const int IDC_ARROW = 32512; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern short RegisterClassEx(ref WNDCLASSEX pcWndClassEx); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool UpdateWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int TranslateMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DispatchMessage([In] ref MSG lpMsg); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern void PostQuitMessage(int nExitCode); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr BeginPaint(IntPtr hWnd, out PAINTSTRUCT lpPaint); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); [DllImport("gdi32.dll", CharSet = CharSet.Auto)] static extern IntPtr TextOut( IntPtr hdc, int x, int y, string lpString, int nCount ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr GetModuleHandle(string lpModuleName); static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) { PAINTSTRUCT ps = new PAINTSTRUCT(); IntPtr hdc; string strMessage = "Hello, Win32 GUI(C#) World!"; switch (uMsg) { case WM_PAINT: hdc = BeginPaint( hWnd, out ps ); TextOut( hdc, 0, 0, strMessage, strMessage.Length ); EndPaint( hWnd, ref ps ); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return IntPtr.Zero; } static int WinMain(string[] args) { IntPtr hInstance = Marshal.GetHINSTANCE(typeof(Hello).Module); const string CLASS_NAME = "helloWindow"; const string WINDOW_NAME = "Hello, World!"; WNDCLASSEX wcex = new WNDCLASSEX(); wcex.cbSize = (uint)Marshal.SizeOf(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = new WndProcDelgate(WndProc); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, new IntPtr(IDI_APPLICATION)); wcex.hCursor = LoadIcon(hInstance, new IntPtr(IDC_ARROW)); wcex.hbrBackground = new IntPtr(COLOR_WINDOW + 1); wcex.lpszMenuName = ""; wcex.lpszClassName = CLASS_NAME; wcex.hIconSm = IntPtr.Zero; RegisterClassEx(ref wcex); IntPtr hWnd = CreateWindowEx( 0, wcex.lpszClassName, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, IntPtr.Zero, IntPtr.Zero, wcex.hInstance, IntPtr.Zero); ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg = new MSG(); while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); } return (int)msg.wParam; } [STAThread] static int Main(string[] args) { return WinMain( args ); } }
コンパイル方法
C:¥> csc /target:winexe Hello.cs
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Win32 GUI(C#) World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+