Getting size of Window with Sketchup


#1

I followed this code taken from Eneroth Viewport Resizer and modified.

if RUBY_VERSION < "2.0.0"
  require File.join(PLUGIN_ROOT, "ene_viewport_resizer", "Win32API.so")
else
  require "Win32API"
end;

GetActiveWindow = Win32API.new("user32.dll", "GetActiveWindow", "", "L") unless defined?GetActiveWindow
GetWindowRect = Win32API.new("user32.dll", "GetWindowRect", "LP", "I") unless defined?GetWindowRect
ShowWindow = Win32API.new("user32.dll", "ShowWindow", "LI", "I") unless defined?ShowWindow
MoveWindow = Win32API.new("user32.dll", "MoveWindow", "LIIIII", "I") unless defined?MoveWindow
GetAncestor = Win32API.new("user32.dll", "GetAncestor", "LI", "I") unless defined?GetAncestor

GA_ROOTOWNER = 3 unless defined?GA_ROOTOWNER 
SW_RESTORE = 9 unless defined?SW_RESTORE
@hwnd = GetActiveWindow.call; # remember Scatchup root window

def self.get_hwnd
  return @hwnd
end;

puts "ERROR hwnd UNDEFINED " if self.get_hwnd == 0

#Returns array of left, top, right and bottom
def self.get_rect(hwnd)
  rect = [0, 0, 0, 0].pack("L*")
  GetWindowRect.call hwnd, rect
  rect.unpack("L*").map { |e| [e].pack("L").unpack("l").first }
end;

#Get window size as array of width and height.
def self.get_size(hwnd)  
  left, top, right, bottom = self.get_rect hwnd
  puts "left: #{left}, top: #{top}, right: #{right}, bottom: #{bottom}"
  w = right - left
  h = bottom - top
  return [w, h]
end;:

I have problems with the self.get_rect(hwnd) method. It returns odd numbers.

I show you what I have on screen.

Can you help to correct it?

Height of the window is around 600 or higher. Width is cca 542.

The call window_size = self.get_size @hwnd should return correct size.


#2

Are you sure this is not the Ruby Console size? Have you thought about querying the window name?


#3

This is copied from the pluging:
self.get_size hwnd

BTW: The problem of the plugin is that it resized console, which is not what I want. Maybe the function GetWindowRect is incorrectly used or converted (binary to rect).


#4

So you need to get somehow the correct window handle of the main SketchUp window…

(That’s why I consider using Win32API a hack, it’s not part of the official SketchUp API, but external. Everything external is based on assumptions about a user’s system, the desktop and application windows, active windows, window stacking order. It is not cross-platform, and not even guaranteed to work on every Windows users’s system.)


#5

Ah, so it is incorrect window…

I changed
@hwnd = GetActiveWindow.call;
to
@hwnd = GetActiveWindow.call unless defined?@hwnd
The problem was that the handler was redefined when I reloaded the module in console.


#6

Good you found it!

On a side note, I often start SketchUp and switch to the web browser while SketchUp is (slooowly) starting in the background. In programming it is sometimes very important to pay attention to details like that the meaning of the task “get active window” is not identical to “get main SketchUp window”. Let’s hope this edge case never occurs!


#7

My plugin requires the SketchUp window to be the active window to work. Since it’s supposed to be called from a menu or shortcut this usually isn’t a problem. This is one of my first plugin ever and really not very well written. There are more proper ways to get a reference to the main SketchUp window. As Andreas says you can check the names which I think might be language dependent (or at least model filename dependent). I also think there is a way to find the “parent” window the ruby console belongs to. If you simply assume the main SketchUp window is the active window you should (unlike what I did) make a comment about this assumption in your code to easier understand it in the future.


#8

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.