• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

[C\C++] SketchUp window handle

Scheduled Pinned Locked Moved Developers' Forum
12 Posts 3 Posters 823 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 3 Nov 2011, 17:15

    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
    • T Offline
      thomthom
      last edited by 4 Nov 2011, 08:38

      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
      • T Offline
        thomthom
        last edited by 4 Nov 2011, 08:53

        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
        • D Offline
          Dan Rathbun
          last edited by 4 Nov 2011, 09:49

          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
          • T Offline
            thomthom
            last edited by 4 Nov 2011, 09:54

            @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 4 Nov 2011, 19:13

              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
              • D Offline
                Dan Rathbun
                last edited by 5 Nov 2011, 00:08

                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
                • T Offline
                  thomthom
                  last edited by 5 Nov 2011, 09:08

                  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 5 Nov 2011, 09:31

                    @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
                    • T Offline
                      thomthom
                      last edited by 5 Nov 2011, 09:42

                      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 7 Nov 2011, 13:36

                        @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
                        • T Offline
                          thomthom
                          last edited by 13 Nov 2011, 17:01

                          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
                          1 / 1
                          • First post
                            1/12
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement