0
2

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 5 years have passed since last update.

VBAで計算機を回す(物理)

Last updated at Posted at 2018-06-14

VBAで自動的に計算機を回しまくる、というコードです。


'おまじない=======================================
'Win32apiというwindows操作する関数を呼び出してます。
'関数ごとに呼び出すので、行が多いです。やりたいことを「●● vba win32api」でぐぐると出てくるのでコピペです。
Public Declare Function Findwindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpwindowname As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, ByVal hWndInsetAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal uFlags As Long) As Long
Public Const WM_SETTEXT = &HC
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
'おまじない終わり=======================================
 
Sub VBAでパソコン操作のサンプル電卓回し()
 
    '電卓を回す回数をインプット
    回す回数 = InputBox("何回まわしますか?(最大20くらいにして下さい)", , 5)
    
    '電卓を起動する
    Shell "C:\Windows\system32\calc.exe", vbMinimizedFocus
    Sleep 1000
    
    '電卓のwindowハンドルを取得
    hWnd = Findwindow(vbNullString, "電卓")
    
    '電卓の計算欄に回す回数を記述=======================================
    hWnd2 = FindWindowEx(hWnd, 0, "CalcFrame", vbNullString)
    hwnd3 = FindWindowEx(hWnd2, 0, "Static", vbNullString)
    hWnd4 = FindWindowEx(hWnd2, hwnd3, vbNullString, vbNullString)
    hwnd5 = FindWindowEx(hWnd4, 0, vbNullString, vbNullString)
    hwnd5 = FindWindowEx(hWnd4, hwnd5, vbNullString, vbNullString)
    hwnd5 = FindWindowEx(hWnd4, hwnd5, vbNullString, vbNullString)
 
    Call SendMessage(hwnd5, WM_SETTEXT, 0, 回す回数 & "回まわします ")
    Sleep 2000
    
    '電卓を回す=======================================
    Dim myRect As RECT
    Const SWP_NOSIZE As Integer = &H1 '現在のサイズを維持(cx、cyは無視)
    Pi = WorksheetFunction.Pi() '回す用の円周率
    
    '中心を取得
    Call GetWindowRect(hWnd, myRect)
    'サインとコサイン使って回す。setwindowposでwindowの位置を指定できます。
    For i = 1 To 回す回数
        半径 = 300 * i / 回す回数
        For t = 1 To 360 Step 1
            Call SetWindowPos(hWnd, 0, myRect.Left + 半径 * Cos(t * Pi / 180), myRect.Top + 半径 * Sin(t * Pi / 180), -1, -1, SWP_NOSIZE)
        Next
        DoEvents
    Next
End Sub
 
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?