關於我自己

2013年11月20日 星期三

視窗操作

主旨:

1.發這一篇,我是覺得蠻有趣的說
開發是用VB6.0 ( 跟C++幾乎是大同小異,所以可以參考看看原理)
用到的涵式會用到
GetWindow,GetWindowStructure,FindWindow ,SendMessage

利用 SendMessage 涵式對視窗做操作

SendMessage 部分參數:
Private Const WM_CLOSE = &H10   '關閉視窗
Private Const WM_SETFOCUS = &H7 '設定焦點
Private Const WM_KEYDOWN = &H100 '鍵盤按下
Private Const WM_KEYUP = &H101 '鍵盤放開
Private Const WM_GETTEXT = &HD  '得到文字
Private Const WM_SETTEXT = &HC  '設定文字
Private Const WM_GETTEXTLENGTH = &HE  '得到文字長度
Private Const BM_CLICK = &HF5 '點擊按鈕

要比較完整的可以到GOOGLE打WM_SYSCOMMAND message搜尋就可以搜尋
到微軟的官方API說明

2.要如何節取到想要的視窗操作權呢?

先解說一下,每一個視窗都會有一個編碼代號,視窗裡面會有許多不同元件
也都會有編碼代號,只要取得編碼代號,就可以利用SendMessage向物件作操作

3.要取得編碼代號將會GetWindow,FindWindow兩個涵式
 GetWindowStructure將是得到元件的型態名稱

4.

5.


程式碼:
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private 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
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
 
Private Const WM_CLOSE = &H10  '關閉指令
Private Const WM_SETFOCUS = &H7
Private Const WM_GETDLGCODE = &H87
Private Const WM_IME_KEYDOWN = &H2
Private Const WM_GETTEXT = &HD
Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Command1_Click()

Dim hwnd As Long, hwndex As Long
Dim lpbuffer As String
Dim a As String
Dim slen As Integer
lpbuffer = Space(255)

hwnd = FindWindow(vbNullString, "小算盤")
If hwnd <> 0 Then
    hwndex = FindWindowEx(hwnd, 0, "Edit", vbNullString)
    SendMessage hwndex, WM_SETTEXT, 0, ByVal Text1.Text
End If


6.
    '  搜尋子元件
    '  擷取子元件編號代碼

    num_children = 0
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    Do While child_hwnd <> 0
        num_children = num_children + 1
        ReDim Preserve children(1 To num_children)
        children(num_children) = child_hwnd
        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)

    Loop

7.


  

    For i = 1 To 2
   
    If i = 1 Then
    slen = SendMessage(A22(i), WM_GETTEXTLENGTH, 0, 0) '
    SendMessage A22(i), WM_GETTEXT, slen + 1, ByVal lpbuffer
    frmWindowList.Text1.Text = lpbuffer
    Else
    slen = SendMessage(A22(i), WM_GETTEXTLENGTH, 0, 0) '
    SendMessage A22(i), WM_GETTEXT, slen + 1, ByVal lpbuffer
     frmWindowList.Text2.Text = lpbuffer
    End If



8.
   





2013年11月3日 星期日

Lego 兩個馬達車理念

1.

2.

3.

4.

5.

6.

想想看如何我要在兩個馬達在同時運轉的時候,

如果我要左右彎的時候我要如何去控制兩個馬達呢?