wpf窗口置顶的使用方法

public class ZWinUtil
{
  #region ShowWindow 方法窗体状态的参数枚举

    /// <summary>
    /// 隐藏窗口并激活其他窗口
    /// </summary>
    private const int SW_HIDE = 0;

  /// <summary>
  /// 激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志
  /// </summary>
  private const int SW_SHOWNORMAL = 1;

  /// <summary>
  /// 激活窗口并将其最小化
  /// </summary>
  private const int SW_SHOWMINIMIZED = 2;

  /// <summary>
  /// 激活窗口并将其最大化
  /// </summary>
  private const int SW_SHOWMAXIMIZED = 3;

  /// <summary>
  /// 以窗口最近一次的大小和状态显示窗口。此值与SW_SHOWNORMAL相似,只是窗口没有被激活
  /// </summary>
  private const int SW_SHOWNOACTIVATE = 4;

  /// <summary>
  /// 在窗口原来的位置以原来的尺寸激活和显示窗口
  /// </summary>
  private const int SW_SHOW = 5;

  /// <summary>
  /// 最小化指定的窗口并且激活在Z序中的下一个顶层窗口
  /// </summary>
  private const int SW_MINIMIZE = 6;

  /// <summary>
  /// 最小化的方式显示窗口,此值与SW_SHOWMINIMIZED相似,只是窗口没有被激活
  /// </summary>
  private const int SW_SHOWMINNOACTIVE = 7;

  /// <summary>
  /// 以窗口原来的状态显示窗口。此值与SW_SHOW相似,只是窗口没有被激活
  /// </summary>
  private const int SW_SHOWNA = 8;

  /// <summary>
  /// 激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志
  /// </summary>
  private const int SW_RESTORE = 9;

  /// <summary>
  /// 依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的
  /// </summary>
  private const int SW_SHOWDEFAULT = 10;

  /// <summary>
  /// 最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数
  /// </summary>
  private const int SW_FORCEMINIMIZE = 11;

  #endregion ShowWindow 方法窗体状态的参数枚举

    //窗体置顶
    private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

  //取消窗体置顶
  private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);

  //不调整窗体位置
  private const uint SWP_NOMOVE = 0x0002;

  //不调整窗体大小
  private const uint SWP_NOSIZE = 0x0001;

  [DllImport("User32.dll", EntryPoint = "FindWindow")]
  private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

  [DllImport("user32.dll")]
  private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

  [DllImport("user32.dll", EntryPoint = "ShowWindow")]
  private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

  /// <summary>
  /// 使窗体置顶
  /// </summary>
  /// <param name="Name">需要置顶的窗体的名字</param>
  public static void SetTop(string Name)
  {
    IntPtr CustomBar = FindWindow(null, Name);
    if (CustomBar != null)
    {
      SetWindowPos(CustomBar, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    }
  }

  /// <summary>
  /// 取消窗体置顶
  /// </summary>
  /// <param name="Name">需要置顶的窗体的名字</param>
  public static void SetTopNO(string Name)
  {
    IntPtr CustomBar = FindWindow(null, Name);
    if (CustomBar != null)
    {
      SetWindowPos(CustomBar, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    }
  }

  /// <summary>
  /// 显示窗口
  /// </summary>
  /// <param name="Name"></param>
  public static void SetWinShow(string Name)
  {
    IntPtr CustomBar = FindWindow(null, Name);
    if (CustomBar != null)
    {
      ShowWindow(CustomBar, SW_SHOW);
    }
  }

  /// <summary>
  /// 隐藏窗口
  /// </summary>
  /// <param name="Name"></param>
  public static void SetWinHide(string Name)
  {
    IntPtr CustomBar = FindWindow(null, Name);
    if (CustomBar != null)
    {
      ShowWindow(CustomBar, SW_HIDE);
    }
  }
}

使用方法:传参中的参数可以是window title

ZWinUtil.SetWinShow("文件资源管理器");
ZWinUtil.SetTop("文件资源管理器");
ZWinUtil.SetWinHide("文件资源管理器");

当前句柄拾取方式

IntPtr handle;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
  handle = new WindowInteropHelper(this).Handle;
}

参考
https://cloud.tencent.com/developer/article/1984247

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中使用WPF窗口来开发AutoCAD插件,你需要使用AutoCAD .NET API和WPF技术。以下是基本步骤: 1. 在Visual Studio中创建一个新的Class Library项目。 2. 添加对AutoCAD .NET API的引用。这可以通过添加对acdbmgd.dll和acmgd.dll的引用来完成。 3. 在项目中添加一个新的WPF窗口或用户控件。 4. 在WPF窗口中添加必要的控件和事件处理程序。 5. 在AutoCAD中加载插件并在需要时显示WPF窗口。 以下是一个简单的示例,演示如何在AutoCAD中使用WPF窗口: ```csharp using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using System.Windows.Controls; using System.Windows.Forms.Integration; namespace MyPlugin { public class MyCommands { [CommandMethod("MyCommand")] public void MyCommand() { // 获取当前文档和编辑器 Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; // 创建并显示WPF窗口 MyWpfWindow wpfWindow = new MyWpfWindow(); ElementHost.EnableModelessKeyboardInterop(wpfWindow); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalWindow(wpfWindow); // 在控制台中显示选定的文本 PromptSelectionResult selRes = ed.GetSelection(); if (selRes.Status == PromptStatus.OK) { SelectionSet selSet = selRes.Value; foreach (SelectedObject selObj in selSet) { if (selObj.ObjectId.ObjectClass == RXClass.GetClass(typeof(DBText))) { DBText text = (DBText)selObj.ObjectId.GetObject(OpenMode.ForRead); ed.WriteMessage("Selected Text: " + text.TextString); } } } } } public class MyWpfWindow : UserControl { public MyWpfWindow() { // 添加WPF控件 TextBox textBox = new TextBox(); textBox.Text = "Hello, world!"; this.Content = textBox; } } } ``` 在这个示例中,我们在AutoCAD中创建了一个名为"MyCommand"的命令,当用户输入该命令时,会打开一个WPF窗口,并在控制台中显示选定的文本。WPF窗口中只包含一个文本框控件,其中包含"Hello, world!"文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值