0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Revit】Revitのメインウィンドウハンドルの取得方法

Last updated at Posted at 2024-08-20

概要

RevitAPIを使用した、Revitのウィンドウハンドルの取得方法をまとめました。

方法

Revit起動時のOnStartup()の引数であるUIControlledApplicationから取得可能です。

public Result OnStartup(UIControlledApplication uicapp)
{
    var revitHandle = uicapp.MainWindowHandle;
}

ハンドルの取得を失敗した例

Revitアドインの開発中に、Revit起動後のアドイン実行中に下記のようなコードで、必要になったタイミングでウィンドウハンドルを取得しようとしました。ただ、原因がわからないですが数回に一度GetCurrentProcess()でRevitではないプロセスが返却される場合がありました。
この現象は記事に記載した方法で取得し、いつでも使用できるようフィールドで保持しておく形にすると、解消しました。

var process = Process.GetCurrentProcess();
var revitHandle = process.MainWindowHandle;

Revitのウィンドウハンドルの使用例

一例ですが、開発中に使用する場面としては、アドインから新しい画面を起動する際に、Revitのウィンドウと親子関係を設定するときによく使用していました。

var view = new SampleView();
view.DataContext = new SampleViewModel();

if (revitHandle != IntPtr.Zero)
{
    // Revitのメインウィンドウを親にする
    var wnd = new WindowInteropHelper(view);
    wnd.Owner = revitHandle;
}

view.ShowDialog();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?