Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした