WSHでWin32APIを呼び出す-その2
2005年6月27日
←戻る
さて、DynaWrapを用いれば、Win32APIにアクセスできることが分かった。では、これを用いてVBscriptで本格的なWindowsdプログラムを作成できるのであろうか。
いきなりVBscriptでプログラムする前に、Visual Basic(ver5を用いた)でWin32APIによるWindowsプログラムを作成してみた。このプログラムの作成には、このページを参考にさせていただいた。
まず、ソースコードから。
このプログラムの実行結果は、次のようになる。

VBscriptとWin32APIを用いたWindowsプログラムを作成するためのたたき台ができあがった。次は、このプログラムがVBscriptに移植できるかどうかを考えてみる。
(続く)
さて、DynaWrapを用いれば、Win32APIにアクセスできることが分かった。では、これを用いてVBscriptで本格的なWindowsdプログラムを作成できるのであろうか。
いきなりVBscriptでプログラムする前に、Visual Basic(ver5を用いた)でWin32APIによるWindowsプログラムを作成してみた。このプログラムの作成には、このページを参考にさせていただいた。
まず、ソースコードから。
Option Explicit Type POINTAPI x As Long y As Long End Type (略) Const SW_SHOW = 5 (略) Declare Function LoadIcon Lib "user32" Alias "LoadIconA" _ (ByVal hInstance As Long, ByVal lpIconName As String) As Long (略) Private g_chAppName As String Private g_chClassName As String Private p_hInstance As Long, p_hPreInst As Long Private p_pchCmdLine As String, p_iCmdShow As Integer Sub Main() 'WinMainと互換性を持たせるための記述 p_hInstance = App.hInstance p_hPreInst = App.PrevInstance p_pchCmdLine = Command p_iCmdShow = SW_SHOW g_chAppName = "TestApplication" 'アプリケーションの名前 g_chClassName = "TestWndClass" 'ウィンドウクラス名 If InitApplication(p_hInstance, AddressOf WndProc) = True Then If InitInstance(p_hInstance, p_iCmdShow) = True Then Call Run End If Call UnregisterClass(g_chClassName, p_hInstance) End If End Sub 'ウィンドウクラスを登録 Function InitApplication(p_hInstance As Long, AddressOfWndProc As Long) As Boolean Dim stWndClass As WNDCLASSEX 'ウィンドウクラス Call UnregisterClass(g_chClassName, p_hInstance) stWndClass.cbSize = Len(stWndClass) stWndClass.style = CS_BYTEALIGNWINDOW Or CS_HREDRAW Or CS_VREDRAW stWndClass.lpfnWndProc = AddressOfWndProc stWndClass.cbClsExtra = 0 stWndClass.cbWndExtra = 0 stWndClass.hInstance = p_hInstance stWndClass.hIcon = LoadIcon(p_hInstance, IDI_EXCLAMATION) stWndClass.hCursor = LoadCursor(p_hInstance, IDC_ARROW) stWndClass.hbrBackground = COLOR_BACKGROUND stWndClass.lpszMenuName = "" stWndClass.lpszClassName = g_chClassName stWndClass.hIconSm = LoadImage(p_hInstance, g_chAppName, IMAGE_ICON, _ 16, 16, LR_DEFAULTCOLOR) InitApplication = RegisterClassEx(stWndClass) End Function 'ウィンドウの表示 Function InitInstance(p_hInstance As Long, p_nCmdShow As Integer) As Boolean Dim hWnd As Long 'ウィンドウハンドル 'ウィンドウを作製 hWnd = CreateWindowEx(0, g_chClassName, g_chAppName, _ WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 200, 100, 0, 0, _ p_hInstance, 0) 'ウィンドウを表示 Call ShowWindow(hWnd, p_nCmdShow) Call UpdateWindow(hWnd) If hWnd = 0 Then InitInstance = False Else InitInstance = True End If End Function 'メッセージループ Function Run() As Integer Dim stMsg As MSG 'メッセージを送る Do While (GetMessage(stMsg, 0, 0, 0)) Call TranslateMessage(stMsg) Call DispatchMessage(stMsg) Loop Run = stMsg.wParam End Function 'コールバックルーチン Function WndProc(ByVal p_hWnd As Long, ByVal p_uiMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Select Case p_uiMsg 'メッセージの処理 Case WM_PAINT 'Hello World! の書き込み Dim hPaintDC As Long Dim stPaint As PAINTSTRUCT Dim chHello As String chHello = "Hello, world!" hPaintDC = BeginPaint(p_hWnd, stPaint) Call TextOut(hPaintDC, 0, 0, chHello, Len(chHello)) Call EndPaint(p_hWnd, stPaint) WndProc = 0 Case WM_DESTROY '×ボタンが押されたとき。 Call PostQuitMessage(0) 'アプリケーションを終了 WndProc = 0 Case Else WndProc = DefWindowProc(p_hWnd, p_uiMsg, wParam, lParam) End Select End Function
このプログラムの実行結果は、次のようになる。

VBscriptとWin32APIを用いたWindowsプログラムを作成するためのたたき台ができあがった。次は、このプログラムがVBscriptに移植できるかどうかを考えてみる。
(続く)