sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [C\C++] SketchUp window handle

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 841 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T Offline
      tomasz
      last edited by

      What is the most reliable and safe way to get SketchUp main window handle?

      1. Finding a "main" application of my .dll,.dlib?
      2. Guessing a name of SU Window and then figuring out the window handle (I have seen it in WxSU, but it was unreliable)?
      3. Creating a WebDialog with an unique name, finding it and getting its' parent?
        4...

      Will a procedure be much different on MAC?

      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        I'm doing this: http://www.thomthom.net/software/sketchup/tt_lib2/doc/TT/Win32.html#get_sketchup_window-class_method

        <span class="syntaxdefault"><br />def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_sketchup_window<br />  hwnd </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> GetActiveWindow</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">call<br />  return nil if hwnd&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxdefault">0<br />  </span><span class="syntaxcomment"># In case the SketchUp window was not the active one - get the ancestor.<br /></span><span class="syntaxdefault">  GetAncestor</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">call</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">hwnd</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> GA_ROOTOWNER</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
        

        Guessing the name of the Window - what if you have multiple windows, or your browser has an article of SketchUp open?

        I used GetActiveWindow because it returns a window for the current process. Then I get the ancestor because that will be the SketchUp window. However, it only work if SketchUp or any of its child windows has focus. If the function is called when another application has focus you get nil.
        Maybe it can be obtained from the process id?

        @unknownuser said:

        Will a procedure be much different on MAC?

        I'd like to know how to do this. The method above is using the Win32 API - and no Googling has lead me to any solution for OSX. 😞

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          Here's a stack overflow thread I made earlier trying to work out how to get the window handle: http://stackoverflow.com/questions/4548354/win32-can-one-enumerate-the-windows-belonging-to-the-calling-thread

          I've not implemented it yet though.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            a note that on the C side of things, the constant NULL is set to the integer 0, which is what C will return for many Win API functions that are unsuccessful.

            Win32API.so I don't think converts a C NULL into a Ruby nil

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              @dan rathbun said:

              Win32API.so I don't think converts a C NULL into a Ruby nil

              You're right. That was a wee bug in my code there. I compare against 0 elsewhere. Thanks for spotting that.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • T Offline
                tomasz
                last edited by

                I have succeeded to find SU hwnd in a following way (pseudo-Ruby \ C++ code):

                
                		curr_proc_id = GetCurrentProcessId();
                		h = GetTopWindow(0);
                		while ( h )
                		{
                				 pid=GetWindowThreadProcessId( h );
                				 if ( pid == curr_proc_id )
                				 {
                					class_name=GetClassName( h );
                					if (class_name[0..2]=='Afx' && class_name[13]=='b') su_hwnd=h;
                				 }
                				h = GetNextWindow( h );
                		}
                		return su_hwnd;
                
                

                p.s. I am a spoiled child. I want my Ruby back!!! 🎉 I hate defining almost every single byte in memory!

                Mac OSX now...

                Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  The current pid in Ruby is the global $$, or a call to: Process.pid()

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    GetTopWindow(0)

                    What does this actually do?

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      tomasz
                      last edited by

                      @thomthom said:

                      GetTopWindow(0)

                      What does this actually do?

                      It gets "top most" window.

                      @unknownuser said:

                      If this parameter is NULL (0), the function returns a handle to the window at the top of the Z order.

                      The z-order of a window indicates the window's position in a stack of overlapping windows. This window stack is oriented along an imaginary axis, the z-axis, extending outward from the screen. The window at the top of the z-order overlaps all other windows. The window at the bottom of the z-order is overlapped by all other windows.

                      http://msdn.microsoft.com/en-us/library/windows/desktop/ms633514(v=vs.85).aspx

                      GetNextWindow
                      gets a window beneath.. till all windows examined.

                      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        How fast is it to search through all the windows and do string comparison?

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          tomasz
                          last edited by

                          @thomthom said:

                          How fast is it to search through all the windows and do string comparison?

                          I haven't learned yet how to measure time in C (whole function is written in C), but I guess it is very fast. 😄

                          Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            Here's a way that doesn't iterate every windows there is. It enumerates the windows of the calling thread. Cuts down the searching and ensure that you don't get the wrong window.


                            win32_sketchup_window.rb

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement