Results 1 to 2 of 2

Thread: ComboBox API

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316

    Question

    I tried posting this on the API list, and I haven't gotten an answer, so I thought I would give this group a shout.

    Anyway, more for just knowing, I would like to know how to populate a Combobox that has been created through the API. Below is the code that I would use to Create a Form via the API and ComboBox, but how do I add items.

    Code:
    Option Explicit
    
    Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
    Public Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long
    Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    Public Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
    Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
    Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Public Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long
    Public Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As String) As Long
    Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
    Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
    
    
    Public Type WNDCLASS
        style As Long
        lpfnwndproc As Long
        cbClsextra As Long
        cbWndExtra2 As Long
        hInstance As Long
        hIcon As Long
        hCursor As Long
        hbrBackground As Long
        lpszMenuName As String
        lpszClassName As String
    End Type
    
    
    Public Type POINTAPI
        x As Long
        y As Long
    End Type
    
    
    Public Type Msg
        hwnd As Long
        message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    
    Public Const CS_VREDRAW = &H1
    Public Const CS_HREDRAW = &H2
    
    Public Const CW_USEDEFAULT = &H80000000
    
    Public Const ES_MULTILINE = &H4&
    
    Public Const WS_BORDER = &H800000
    Public Const WS_CHILD = &H40000000
    Public Const WS_OVERLAPPED = &H0&
    Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
    Public Const WS_SYSMENU = &H80000
    Public Const WS_THICKFRAME = &H40000
    Public Const WS_MINIMIZEBOX = &H20000
    Public Const WS_MAXIMIZEBOX = & H100 00
    Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
    
    Public Const WS_EX_CLIENTEDGE = &H200&
    
    Public Const COLOR_WINDOW = 5
    
    Public Const WM_DESTROY = &H2
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    
    Public Const IDC_ARROW = 32512&
    
    Public Const IDI_APPLICATION = 32512&
    
    Public Const GWL_WNDPROC = (-4)
    
    Public Const SW_SHOWNORMAL = 1
    
    Public Const MB_OK = &H0&
    Public Const MB_ICONEXCLAMATION = &H30&
    
    Public Const CBS_DROPDOWN = 3
    Public Const gClassName = "MyClassName"
    Public Const gAppName = "My Window Caption"
    
    Public gButtonOldProc As Long
    Public gHwnd As Long, gButtonHwnd As Long, gEditHwnd As Long 
    Public gComboHwnd As Long
    
    
    Public Sub Main()
    
       Dim wMsg As Msg
        
       ''Call procedure to register window classname. If false, then exit.
       If RegisterWindowClass = False Then Exit Sub
        
          ''Create window
          If CreateWindows Then
             ''Loop will exit when WM_QUIT is sent to the window.
             Do While GetMessage(wMsg, 0&, 0&, 0&
                ''TranslateMessage takes keyboard messages and converts
                ''them to WM_CHAR for easier processing.
                Call TranslateMessage(wMsg)
                ''Dispatchmessage calls the default window procedure
                ''to process the window message. (WndProc)
                Call DispatchMessage(wMsg)
             Loop
          End If
    
        Call UnregisterClass(gClassName$, App.hInstance)
    
    End Sub
    
    Public Function RegisterWindowClass() As Boolean
    
        Dim wc As WNDCLASS
        
        ''Registers our new window with windows so we
        ''can use our classname.
        
        wc.style = CS_HREDRAW Or CS_VREDRAW
        wc.lpfnwndproc = GetAddress(AddressOf WndProc) ''Address in memory of default window procedure.
        wc.hInstance = App.hInstance
        wc.hIcon = LoadIcon(0&, IDI_APPLICATION) ''Default application icon
        wc.hCursor = LoadCursor(0&, IDC_ARROW) ''Default arrow
        wc.hbrBackground = COLOR_WINDOW ''Default a color for window.
        wc.lpszClassName = gClassName$
    
        RegisterWindowClass = RegisterClass(wc) <> 0
        
    End Function
    Public Function CreateWindows() As Boolean
      On Error GoTo ErrorHandler
        ''Create actual window.
        gHwnd& = CreateWindowEx(0&, gClassName$, gAppName$, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 321, 0&, 0&, App.hInstance, ByVal 0&
        ''Create button
        gOKHwnd& = CreateWindowEx(0&, "Button", "OK", WS_CHILD, 446, 35, 121, 42, gHwnd&, 0&, App.hInstance, 0&
        gButtonHwnd& = CreateWindowEx(0&, "Button", "Button", WS_CHILD, 446, 98, 121, 42, gHwnd&, 0&, App.hInstance, 0&
        gComboHwnd& = CreateWindowEx(0&, "Combobox", "Test", CBS_DROPDOWN Or WS_CHILD, 45, 245, 170, 400, gHwnd&, 0&, App.hInstance, 0&
    
        
        ''Since windows are hidden, show them. You can use UpdateWindow to
        ''redraw the client area.
        Call ShowWindow(gHwnd&, SW_SHOWNORMAL)
        Call ShowWindow(gButtonHwnd&, SW_SHOWNORMAL)
        Call ShowWindow(gComboHwnd&, SW_SHOWNORMAL)
        
        ''Get the memory address of the default window
        ''procedure for the button and store it in gButOldProc
        ''This will be used in ButtonWndProc to call the original
        ''window procedure for processing.
        gButtonOldProc& = GetWindowLong(gButtonHwnd&, GWL_WNDPROC)
       
        ''to set the address of the window procedure.
        Call SetWindowLong(gButtonHwnd&, GWL_WNDPROC, GetAddress(AddressOf ButtonWndProc))
        
        CreateWindows = (gHwnd& <> 0)
        Exit Function
    ErrorHandler:
    MsgBox "error"
    End Function
    Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
      ''This our default window procedure for the window. It will handle all
      ''of our incoming window messages and we will write code based on the
      ''window message what the program should do.
    
      Dim strTemp As String
    
        Select Case uMsg&
           Case WM_DESTROY:
              ''Since DefWindowProc doesn't automatically call
              ''PostQuitMessage (WM_QUIT). We need to do it ourselves.
              ''You can use DestroyWindow to get rid of the window manually.
              Call PostQuitMessage(0&
        End Select
        
    
      ''Let windows call the default window procedure since we're done.
      WndProc = DefWindowProc(hwnd&, uMsg&, wParam&, lParam&
    
    End Function
    I think that this could be useful when writing apps for friends with very sad computers. (less overhead). Thanks for you help.


  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Errr..less overhead?

    I think your answer is in the MFC reference section on CComboBox. I have copied and pasted a section below for you.

    I don't think it is less overhead at all but I have never tested. I mean, given an app with 1000 combo boxes, you may be right, but I would suggest that such an app would be a little unlikely...

    If there is no special functionality you are trying to make the combobox perform, then it's much easier (and I believe better) to create the control dynamically using good 'ol VB and use the .AddItem method...

    Here's the InsertItem definition for you though...

    Regards
    Paul Lewis



    CComboBoxEx::InsertItem
    int InsertItem( const COMBOBOXEXITEM* pCBItem );

    Return Value

    The index at which the new item was inserted if successful; otherwise -1.

    Parameters

    pCBItem

    A pointer to aCOMBOBOXEXITEM structure that will receive the item information. This structure contains callback flag values for the item.

    Remarks

    When you call InsertItem, aWM_NOTIFY message withCBEN_INSERTITEM notification will be sent to the parent window.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width