投稿時間:2005/02/02(Wed) 01:01 投稿者名:Starfish
Eメール:
URL :
タイトル:Re^4: テキスト起動
> Starfishさんの方法でやった場合,SendKeysで送った > データが文字化けしたりしませんかね? > 例えば,半角カナとか・・あれがいやで僕は使用は控えるべきかと > 対処方法があればSendKeysももっと実用的になるんだけど
半角カナは使わないのでわかりませんが、SendKeysで常にアクティブ になっているかが不安です。自分で使うプログラムならキーボードに 触らないとか気をつけていればいいんですけどね。
APIで、メモ帳を起動してそこに表示するサンプルを作ってみました。 エラー処理(ループってるので、時間がたったら抜けるとか)は考慮してません。
APIの定義>標準モジュールに記述してください
Public Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadId As Long End Type
Public Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _ (ByVal lpApplicationName As String, ByVal lpCommandLine As String, _ lpProcessAttributes As Any, lpThreadAttributes As Any, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ lpEnvironment As Any, ByVal lpCurrentDriectory As String, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ ByVal lpsz2 As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _ (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long Public Const WM_SETTEXT = &HC
処理の部分>ここではコマンドボタンで実行しています Private Sub Command1_Click() Dim udtProcInfo As PROCESS_INFORMATION Dim udtStartInfo As STARTUPINFO Dim lngret As Long Dim lngHwnd As Long Dim lngPrev As Long Dim lngProcessID As Long Dim lngHwndNotepad As Long
udtStartInfo.cb = Len(udtStartInfo) lngret = CreateProcess(vbNullString, "notepad.exe", ByVal 0&, ByVal 0&, _ 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, _ udtStartInfo, udtProcInfo)
Do lngPrev = 0 Do lngHwnd = FindWindowEx(0&, lngPrev, "Notepad", vbNullString) If lngHwnd <> 0 Then lngret = GetWindowThreadProcessId(lngHwnd, lngProcessID) If lngProcessID = udtProcInfo.dwProcessId Then lngHwndNotepad = lngHwnd Exit Do End If lngPrev = lngHwnd End If DoEvents Loop Until lngHwnd = 0 Or lngHwndNotepad <> 0 Loop Until lngHwndNotepad <> 0 Do lngHwnd = FindWindowEx(lngHwndNotepad, 0&, "Edit", vbNullString) DoEvents Loop Until lngHwnd <> 0 lngret = SendMessage(lngHwnd, WM_SETTEXT, 0&, _ ByVal "VBのプログラムからメモ帳に書き込んでいます。" & vbCrLf & "2行目です。") End Sub
|