Results 1 to 7 of 7

Thread: problem with api-call

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52

    problem with api-call

    hi... i've a problem with an api-call

    the class should prevent the ExtCombobox from opening the dropdown-box.

    Code:
    Public Class ExtComboBox
        Inherits ComboBox
    
        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 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
        Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, ByVal lpRect As RECT) As Long
        Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
        Private Structure rect
            Dim Left As Long
            Dim Top As Long
            Dim Right As Long
            Dim Botton As Long
        End Structure
    
        Private Const GWL_WNDPROC = (-4)
        Private Const WM_CTLCOLORLISTBOX = &H134
    
        Private lbRect As rect
        Private hWndLB As Long
    
        Private OldWndPrg As Long
    
        ' Subclassing einschalten
        Public Sub ChangeWndPrg(ByVal hwnd As Long)
            OldWndPrg = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndPrg)
        End Sub
    
        ' Subclassing ausschalten
        Public Sub ResetWndPrg(ByVal hwnd As Long)
            SetWindowLong(hwnd, GWL_WNDPROC, OldWndPrg)
        End Sub
    
        Public Function WndPrg(ByVal hwnd As Long, _
                               ByVal uMsg As Long, _
                               ByVal wParam As Long, _
                               ByVal lParam As Long) As Long
    
            If uMsg = WM_CTLCOLORLISTBOX Then
    
                ' lParam = Handler für Listbox-Window
                hWndLB = lParam
    
                ' Ausmasse des Windows ermitteln, das erzeugt wird
                GetWindowRect(hWndLB, lbRect)
    
                ' Wir verhindern damit, dass die Listbox aufgeklappt wird
                ' Parameter: SW_Hidde = 0&
                ShowWindow(hWndLB, 0&)
            End If
    
            ' Alle Nachrichten an die Fensterprozedur weiterleiten
            Return CallWindowProc(OldWndPrg, hwnd, uMsg, wParam, lParam)
        End Function
    
    End Class
    The compiler doesn't accept this code because in ChangeWndPrg the AdressOf-instruction doesn't return a long value.

    has andybody an idea how to solve this problem?

    thanks
    robert

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Change the long to interger and see if that's the problem !

  3. #3
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    You must add a delegate for the AddressOf operator, and change the parameter of the function declaration to the Delegate type.

    See the following topic in the online MSDN Library:

    http://msdn.microsoft.com/library/en...l/vbup1048.asp
    jovton

  4. #4
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    I have a problem with the API call GetWindowLong in regard to the AddressOf operator though.

    Does anyone know how to use the GetWindowLong API call in VB.NET using the GWL_WNDPROC constant as the last parameter, and then use the return value to point to a delegate using the AddressOf operator? Yes it's probably easier said than done. But take a look at this scenario:

    I have a class named CFoo, okay. It has a Public variable named lpPrevWndFunc, another called frm of type Windows.Forms.Form, and another variable called hwnd of type Long.

    I have an instance of the CFoo class called Foo. Okay, now... in a public module SubClass.vb:
    VB Code:
    1. Module modSubclass
    2.  
    3.    Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, _
    4.                                           ByVal msg As Integer, _
    5.                                           ByVal wParam As Integer, _
    6.                                           ByVal lParam As Integer) As Integer
    7.  
    8.    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    9.                             (ByVal lpPrevWndFunc As Integer, _
    10.                              ByVal hwnd As Integer, _
    11.                              ByVal msgWinMessage As Integer, _
    12.                              ByVal wParam As Integer, _
    13.                              ByRef lParam As Integer) As Integer
    14.  
    15.    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
    16.                             (ByVal hwnd As Integer, _
    17.                              ByVal nIndex As Integer, _
    18.                              ByVal dwNewLong As SubClassProcDelegate) As Integer
    19.  
    20.    Private Function SubclassWndProc(ByVal hwnd As Integer, _
    21.                                       ByVal msgWinMessage As Integer, _
    22.                                       ByVal wParam As Integer, _
    23.                                       ByVal lParam As Integer) As Integer
    24.  
    25.    
    26.        ' code hidden ;oP
    27.      
    28.    End Function
    29.  
    30.  
    31.    Public Sub DoSubclass(ByRef frm As Windows.Forms.Form)
    32.  
    33.         Dim Foo As New CFoo
    34.  
    35.         ' Create the object as a form
    36.         Foo.frm = frm
    37.         Foo.hwnd = frm.Handle.ToInt32
    38.         Foo.lpPrevWndFunc = GetWindowLong(frm.Handle.ToInt32, GWL_WNDPROC)
    39.  
    40.         ' Replace the basic window procedure of the
    41.         ' form calling this procedure
    42.  
    43.         Call SetWindowLong(frm.Handle.ToInt32, _
    44.                            GWL_WNDPROC, _
    45.                            AddressOf SubclassWndProc)
    46.  
    47.    End Sub
    48.  
    49.    Public Sub UnSubClass(ByRef frm As Windows.Forms.Form)
    50.  
    51.         Dim hHelp As New HTMLHelp
    52.  
    53.         ' Release the subclassed form
    54.  
    55.         Call SetWindowLong(frm.Handle.ToInt32, _
    56.                            GWL_WNDPROC, _
    57.                            Foo.lpPrevWndFunc)
    58.  
    59.    End Sub
    60.  
    61. End Module


    My real question is... of what type should the class member lpPrevWndFunc be in the class CFoo? Can it be of Type AppName.modSubClass.SubClassProcDelegate? Because if it can then the GetWindowLong returns a value of the wrong type. I would get the error:
    Value of type 'Integer' cannot be converted to 'AppName.modSubclass.SubClassProcDelegate'.
    Thanks to anyone who makes effort in helping me out.
    Last edited by jovton; Oct 24th, 2003 at 03:19 AM.
    jovton

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    try this link for an article on codeproject...
    Subclassing in VB.Net
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    a simple example of subclassing controls , based on the above is this ....
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Const[/COLOR] WM_CTLCOLORLISTBOX [Color=Blue]As[/COLOR] [Color=Blue]Integer[/COLOR] = [Color=Blue]CInt[/COLOR](&H134)
    2.  
    3.     [Color=Blue]Private[/COLOR] [Color=Blue]Declare[/COLOR] [Color=Blue]Function[/COLOR] ShowWindow [Color=Blue]Lib[/COLOR] "user32.dll" ([Color=Blue]ByVal[/COLOR] hwnd [Color=Blue]As[/COLOR] [Color=Blue]Integer[/COLOR], [Color=Blue]ByVal[/COLOR] nCmdShow [Color=Blue]As[/COLOR] [Color=Blue]Integer[/COLOR]) [Color=Blue]As[/COLOR] [Color=Blue]Integer
    4.  
    5. [/COLOR]    [Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] sbclss [Color=Blue]As[/COLOR] SubClass
    6.  
    7.     [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
    8.         sbclss = [Color=Blue]New[/COLOR] SubClass(ComboBox1.Handle)
    9.         sbclss.IsSubClassed = [Color=Blue]True
    10. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub
    11.  
    12. [/COLOR]    [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] sbclss_Proc([Color=Blue]ByVal[/COLOR] m [Color=Blue]As[/COLOR] Message) [Color=Blue]Handles[/COLOR] sbclss.MyBaseProc
    13.         [Color=Blue]If[/COLOR] m.Msg = WM_CTLCOLORLISTBOX [Color=Blue]Then
    14. [/COLOR]            ShowWindow(m.HWnd.ToInt32, 0)
    15.         [Color=Blue]End[/COLOR] [Color=Blue]If
    16. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub
    17. End[/COLOR] [Color=Blue]Class
    18.  
    19. Public[/COLOR] [Color=Blue]Class[/COLOR] SubClass
    20.     [Color=Blue]Inherits[/COLOR] System.Windows.Forms.NativeWindow
    21.     [Color=Blue]Public[/COLOR] [Color=Blue]Event[/COLOR] MyBaseProc([Color=Blue]ByVal[/COLOR] m [Color=Blue]As[/COLOR] Message)
    22.  
    23.     [Color=Blue]Public[/COLOR] IsSubClassed [Color=Blue]As[/COLOR] [Color=Blue]Boolean[/COLOR] = [Color=Blue]False
    24. [/COLOR]    [Color=Blue]Sub[/COLOR] [Color=Blue]New[/COLOR]([Color=Blue]ByVal[/COLOR] Handle [Color=Blue]As[/COLOR] IntPtr)
    25.         [Color=blue]MyBase[/color].AssignHandle(Handle)
    26.     [Color=Blue]End[/COLOR] [Color=Blue]Sub
    27.  
    28. [/COLOR]    [Color=Blue]Protected[/COLOR] [Color=Blue]Overrides[/COLOR] [Color=Blue]Sub[/COLOR] WndProc([Color=Blue]ByRef[/COLOR] m [Color=Blue]As[/COLOR] System.Windows.Forms.Message)
    29.         [Color=Blue]If[/COLOR] IsSubClassed [Color=Blue]Then
    30. [/COLOR]            [Color=Blue]RaiseEvent[/COLOR] MyBaseProc(m)
    31.         [Color=Blue]End[/COLOR] [Color=Blue]If
    32. [/COLOR]        [Color=blue]MyBase[/color].WndProc(m)
    33.     [Color=Blue]End[/COLOR] [Color=Blue]Sub
    34. End[/COLOR] [Color=Blue]Class[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    Seeing that I'm in the early stages of the migration phase I would state that you my friend, are a lifesaver! Thanks.
    jovton

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