Skip to content

Instantly share code, notes, and snippets.

@sbaer
Created November 9, 2012 19:41
Show Gist options
  • Save sbaer/4047746 to your computer and use it in GitHub Desktop.
Save sbaer/4047746 to your computer and use it in GitHub Desktop.
Move View in RhinoCommon
using System.Runtime.InteropServices;
public class MyCommand : Rhino.Commands.Command
{
//...other command stuff...
[DllImport("user32.dll")]
static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left, top, right, bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode)
{
var view = doc.Views.Find("Top", false);
if (view != null)
{
IntPtr hWnd = view.Handle;
// need to get parent handle since all Rhino View windows a embedded in a parent frame window
IntPtr hParent = GetParent(hWnd);
RECT winRect;
GetWindowRect(hParent, out winRect); //winRect is in screen coordinates
IntPtr hGrandParent = GetParent(hParent);
POINT pt;
pt.X = winRect.left;
pt.Y = winRect.top;
ScreenToClient(hGrandParent, ref pt);
MoveWindow(hParent, pt.X + 50, pt.Y + 50, winRect.right - winRect.left, winRect.bottom - winRect.top, true);
}
return Rhino.Commands.Result.Success;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment