c#.net

c#.net 다른 프로세스의 콤보박스 index 변경, items 가져오기

우유빛 2011. 5. 26. 15:26


        uint CB_GETLBTEXT = 0x0148;
        uint CB_SETCURSEL = 0x014E;

        [DllImport("user32")]
        public static extern uint SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetComboItem(1);
        }

        public string GetComboItem(int index)
        {
            StringBuilder ssb = new StringBuilder(256, 256);
            SendRefMessage(this.comboBox1.Handle, CB_GETLBTEXT, index, ssb);
            return ssb.ToString();
        }

        public void SetComboItem(int index)
        {
            SendMessage(this.Handle, CB_SETCURSEL, index, "0");
        }



Option Explicit
 
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SendMessageLong& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Const CB_GETLBTEXTLEN = &H149
Const CB_GETLBTEXT = &H148
Const CB_GETCOUNT = &H146
Const CB_ERR = (-1)
 
Sub Main()
  Dim sItems() As String
  Dim hwnd As Long
  Dim cbo As Long
  Dim I As Long
  'Get the handle of the Parent window
  hwnd = FindWindow("Window Class name", "Window Caption") 'Specify the windows class name and caption; you can specify both or at least one and use vbNullString
  If hwnd > 0 Then
    'Get the handle of the Combo box
    cbo = FindWindowEx(hwnd, 0&, "Combo box class name", vbNullString) 'Specify the windows class name and caption; you can specify both or at least one and use vbNullString
    If cbo > 0 Then
      'Get the Combo box items
      sItems = GetComboBoxItems(cbo)
      'loop through Combo box items and print them in immediate window
      For I = LBound(sItems) To UBound(sItems)
        Debug.Print sItems(I)
      Next I
    End If
  End If
End Sub
 
Private Function GetComboBoxItems(ByVal hWndComboBox As Long) As String()
  Dim I As Long
  Dim sItems() As String
  Dim iLen As Long
  Dim iCount As Long
  'Get the combo box items count
  iCount = SendMessage(hWndComboBox, CB_GETCOUNT, 0, 0)
  'Exit if the items count is none
  If (iCount = CB_ERR) Or (iCount = 0) Then Exit Function
  ReDim Preserve sItems(iCount - 1)
  For I = 0 To iCount - 1
    'Get the item text length
    iLen = SendMessage(hWndComboBox, CB_GETLBTEXTLEN, I, 0)
    'Exit if item text length is -1
    If iLen = CB_ERR Then Exit Function
    sItems(I) = String(iLen, vbNullChar)
    'Get the item text
    If SendMessage(hWndComboBox, CB_GETLBTEXT, I, ByVal sItems(I)) = CB_ERR Then Exit Function
  Next
  GetComboBoxItems = sItems
End Function