Results 1 to 15 of 15

Thread: [RESOLVED] Get UserControl handle?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Resolved [RESOLVED] Get UserControl handle?

    Is there a way to recover the handle to a UserControl on a running program? FindWindowEx seems to only return standard VB Class handles.

    J.A. Coutts

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,009

    Re: Get UserControl handle?

    Quote Originally Posted by couttsj View Post
    Is there a way to recover the handle to a UserControl on a running program? FindWindowEx seems to only return standard VB Class handles.

    J.A. Coutts
    From another program?

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Get UserControl handle?

    Quote Originally Posted by couttsj View Post
    FindWindowEx seems to only return standard VB Class handles.
    No, FindWindowEx returns a window handle which is how the OS identifies a window. It has nothing to to with VB at all. All windows have a handle regardless of whether it was created by a VB app.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,009

    Resolved Re: Get UserControl handle?

    ....
    Last edited by Eduardo-; Nov 21st, 2017 at 08:01 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Get UserControl handle?

    For the time being I have used the registry as a way to transfer the handle, but that seems a bit cludgy. The routines that receive messages are contained in a UserControl, and the other program has to find the Window handle to send messages to.

    J.A. Coutts

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get UserControl handle?

    Do you control the code for both the senders and receivers? If so, there are a few ways to have them identify/find each other than hunting down child windows of some top level window
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Get UserControl handle?

    Quote Originally Posted by LaVolpe View Post
    Do you control the code for both the senders and receivers? If so, there are a few ways to have them identify/find each other than hunting down child windows of some top level window
    Yes, I control both programs. To give you a better idea of what I am up to, I have posted a sample program in the Codebank.
    http://www.vbforums.com/showthread.p...41#post5235741
    In that program, dilettante has used "comctl32" to provide the subclassing by proxying to the User Control. The program I am working on uses "user32" to provide the messaging functions, and it didn't make much sense to use both. Dilettante's subclassing is quite functional, but in order to use it for messaging, I need the handle of the User Control.

    J.A. Coutts
    Last edited by couttsj; Nov 22nd, 2017 at 01:29 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,009

    Re: Get UserControl handle?

    There are probably a lot of ways of doing this, but if you want to go to the subclassing route, and idea would be:

    To subclass the parent form where the usercontrol is.
    Then from the other App, to broadcast a message to all windows using: SendMessage HWND_BROADCAST, WM_FINDUSERCONTROL, Me.hWnd, 0&

    First generate in both Apps WM_FINDUSERCONTROL using RegisterWindowMessage

    Once the subclassing of the UserControl receives the message, it must respond with another message: SendMessage hWndCalling, MW_USERCONTROLANSWER, UserControlhWnd, 0&

    Use also RegisterWindowMessage to generate MW_USERCONTROLANSWER.

    Just an idea.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get UserControl handle?

    ...Dilettante's subclassing is quite functional, but in order to use it for messaging, I need the handle of the User Control.
    Another option... So, if you are using FindWindow to locate your target window, you could use SetProp to both identify the UC hWnd and help positively identify the target window. The logic is kinda like this:

    1. During UC InitProperties/ReadProperties. Call SetProp, using a unique "key" and your UC's hWnd
    Code:
    SetProp UserControl.Parent.hWnd, "yourUniqueKey", UserControl.hWnd
    2. Now when the sender is trying to locate your window (tgtHWND) via FindWindow, it should also look for the custom property you just applied to the main level window
    Code:
    tgtUChWND = GetProp(tgtHWND, "yourUniqueKey")
    If tgUChWnd = 0 Then 
         ' failure to find your key, not the right window, keep searching
    Else
         ' you now have the hWnd you're looking for
    End If
    Edited: Assumption is that you are not using multiple UCs on the same form; else last to initialize will be the one that posted its hWnd into that window property.
    Last edited by LaVolpe; Nov 22nd, 2017 at 01:12 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Get UserControl handle?

    Quote Originally Posted by LaVolpe View Post
    Another option... So, if you are using FindWindow to locate your target window, you could use SetProp to both identify the UC hWnd and help positively identify the target window.
    Thanks, I will give it a try!

    J.A. Coutts

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Get UserControl handle?

    LaVolpe;

    That works, but I ran into a problem trying to view the form, save the project, etc. I kept getting
    Run-time error '50304' (An instance of 'frmTray' cannot be created because its designer window is open.) I had to declare a variable as Public in a .bas module and assign the parent handle value to the variable.
    Code:
        
        SetProp frmTray.hWnd, "12345", UserControl.hWnd
    changed to:
    Code:
        SetProp FrmHandle, "12345", UserControl.hWnd
    Also does the property need to be removed using "RemoveProp"?

    J.A. Coutts

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get UserControl handle?

    Ensure you only set the property when your parent form is in run-time, not design-time. If your UC is designed specifically for your current project and not intended to be a general-use UC that can be added to any project, instead of setting the prop from the UC, set it from the form during Form_Load.

    Regarding removing the prop? Recommended by Microsoft, but the data it uses is released when the form is destroyed.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,009

    Re: Get UserControl handle?

    Quote Originally Posted by LaVolpe View Post
    Regarding removing the prop? Recommended by Microsoft, but the data it uses is released when the form is destroyed.
    Hello Lavolpe. Where did you get that information?
    I think it's not released.
    And according to MSDN the application must remove it before closing:

    Before a window is destroyed (that is, before it returns from processing the WM_NCDESTROY message), an application must remove all entries it has added to the property list. The application must use the RemoveProp function to remove the entries.
    Also in my experience, it is not removed automatically (at least on Windows XP).

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get UserControl handle?

    I'd find it hard to believe that Windows would keep that information in its association tables (if that's what is used) when a window is destroyed. Why? Because another window could be later created and be given the same handle. Would that window then have orphaned properties assigned to it? Rhetorical question really. Would be interesting to try to verify, by enumerating that association table (if possible). Hey, if Microsoft says to remove it, then remove it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Get UserControl handle?

    I ran into a different problem using the UserControl handle. I needed to be able to close the socket connection when the computer went to sleep. To do this, I needed to respond to the system message "WM_POWERBROADCAST". Unfortunately, I discovered that the system only broadcasts those messages to top level windows. So I changed:
    Code:
    Public Sub Show()
        SubclassMe UserControl.hWnd, Me
        With nid
            .hWnd = UserControl.hWnd
            .uId = UserControl.hWnd
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE Or NIF_INFO
            .hIcon = mpicIcon.Handle
            .uCallBackMessage = WM_APP_NIF
            .szTip = mstrToolTip & vbNullChar
        End With
        Shell_NotifyIcon NIM_ADD, VarPtr(nid)
        mblnShown = True
    End Sub
    to:
    Code:
    Public Sub Show()
        SubclassMe FrmHandle, Me
        With nid
            .hWnd = FrmHandle
            .uId = FrmHandle
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE Or NIF_INFO
            .hIcon = mpicIcon.Handle
            .uCallBackMessage = WM_APP_NIF
            .szTip = mstrToolTip & vbNullChar
        End With
        Shell_NotifyIcon NIM_ADD, VarPtr(nid)
        mblnShown = True
    End Sub
    FrmHandle is a public long declared in a module and set when the main form is loaded.

    J.A. Coutts

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