明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1534|回复: 6

winform非模态窗口测试。

[复制链接]
发表于 2021-12-25 23:02 | 显示全部楼层 |阅读模式
MainClass.cs
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;

  3. namespace ModelessDialogTest
  4. {
  5.     public class MainClass
  6.     {
  7.         [CommandMethod("mdt")] //注册命令
  8.         public void UiStart()
  9.         {
  10.             var myfrom = new testFrom();
  11.             //Application.ShowModalDialog(myfrom); //  模态显示
  12.             Application.ShowModelessDialog(myfrom); //  非模态显示
  13.         }
  14.     }
  15. }

testFrom.cs
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  8. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  9. using TransactionManager = Autodesk.AutoCAD.DatabaseServices.TransactionManager;

  10. namespace ModelessDialogTest
  11. {
  12.     public partial class testFrom : Form
  13.     {
  14.         public testFrom()
  15.         {
  16.             //界面的初始化
  17.             InitializeComponent();
  18.         }

  19.         //为了解决窗口切换问题,需要利用Windows API函数SetFocus实现
  20.         //初始化  窗口焦点切换功能
  21.         [DllImport("user32.dll")]
  22.         private static extern IntPtr SetFocus(IntPtr hwnd);
  23.         /// <summary>
  24.         /// 测试测试
  25.         /// </summary>
  26.         /// <param name="sender"></param>
  27.         /// <param name="e"></param>
  28.         private void button_get_Click(object sender, EventArgs e)
  29.         {
  30.             var entopts = new PromptEntityOptions("\n在图中选择一个图元对象");
  31.             entopts.Message = "\n在图中选择一个图元对象:";
  32.             PromptEntityResult ent = null;
  33.             try
  34.             {
  35.                 SetFocus(Application.MainWindow.Handle);//焦点切换到CAD
  36.                 ent = Application.DocumentManager.MdiActiveDocument.Editor.GetEntity(entopts);
  37.                 if (ent.Status != PromptStatus.Error)
  38.                 {
  39.                     ObjectId entid = ent.ObjectId;
  40.                     Database db = Application.DocumentManager.MdiActiveDocument.Database;
  41.                     TransactionManager tm = db.TransactionManager;
  42.                     using (Transaction myT = tm.StartTransaction())
  43.                     {
  44.                         var entity = (Entity) tm.GetObject(entid, OpenMode.ForRead, true);
  45.                         textBox1.Text = "所选对象Name为:" + entity.GetType().Name;
  46.                         myT.Commit();
  47.                     }
  48.                 }
  49.                 Focus();
  50.             }
  51.             catch
  52.             {
  53.             }
  54.         }

  55.         private void button_ok_Click(object sender, EventArgs e)
  56.         {
  57.             Close();
  58.         }
  59.     }
  60. }

遗留问题:鼠标移出移进时,如何自动切换焦点?
  1. private void testFrom_MouseLeave(object sender, EventArgs e)
  2.         {
  3.    textBox1.Text="";
  4.         }

  5.         private void testFrom_MouseEnter(object sender, EventArgs e)
  6.         {
  7.    textBox1.SelectAll();
  8.         }



 楼主| 发表于 2021-12-25 23:11 | 显示全部楼层
testFrom.Designer.cs
  1. &#65279;namespace ModelessDialogTest
  2. {
  3.      partial class testFrom
  4.      {
  5.          /// <summary>
  6.          /// Required designer variable.
  7.          /// </summary>
  8.          private System.ComponentModel.IContainer components = null;

  9.          /// <summary>
  10.          /// Clean up any resources being used.
  11.          /// </summary>
  12.          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13.          protected override void Dispose(bool disposing)
  14.          {
  15.              if (disposing && (components != null))
  16.              {
  17.                  components.Dispose();
  18.              }
  19.              base.Dispose(disposing);
  20.          }

  21.          #region Windows Form Designer generated code

  22.          /// <summary>
  23.          /// Required method for Designer support - do not modify
  24.          /// the contents of this method with the code editor.
  25.          /// </summary>
  26.          private void InitializeComponent()
  27.          {
  28.              this.textBox1 = new System.Windows.Forms.TextBox();
  29.              this.button_get = new System.Windows.Forms.Button();
  30.              this.button_ok = new System.Windows.Forms.Button();
  31.              this.SuspendLayout();
  32.              //
  33.              // textBox1
  34.              //
  35.              this.textBox1.Location = new System.Drawing.Point(13, 89);
  36.              this.textBox1.Name = "textBox1";
  37.              this.textBox1.Size = new System.Drawing.Size(244, 28);
  38.              this.textBox1.TabIndex = 0;
  39.              //
  40.              // button_get
  41.              //
  42.              this.button_get.Location = new System.Drawing.Point(11, 22);
  43.              this.button_get.Name = "button_get";
  44.              this.button_get.Size = new System.Drawing.Size(102, 49);
  45.              this.button_get.TabIndex = 1;
  46.              this.button_get.Text = "Get";
  47.              this.button_get.UseVisualStyleBackColor = true;
  48.              this.button_get.Click += new System.EventHandler(this.button_get_Click);
  49.              //
  50.              // button_ok
  51.              //
  52.              this.button_ok.Location = new System.Drawing.Point(155, 22);
  53.              this.button_ok.Name = "button_ok";
  54.              this.button_ok.Size = new System.Drawing.Size(102, 49);
  55.              this.button_ok.TabIndex = 2;
  56.              this.button_ok.Text = "OK";
  57.              this.button_ok.UseVisualStyleBackColor = true;
  58.              this.button_ok.Click += new System.EventHandler(this.button_ok_Click);
  59.              //
  60.              // testFrom
  61.              //
  62.              this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
  63.              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  64.              this.ClientSize = new System.Drawing.Size(282, 151);
  65.              this.Controls.Add(this.button_ok);
  66.              this.Controls.Add(this.button_get);
  67.              this.Controls.Add(this.textBox1);
  68.              this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  69.              this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
  70.              this.MaximizeBox = false;
  71.              this.MinimizeBox = false;
  72.              this.Name = "testFrom";
  73.              this.ShowInTaskbar = false;
  74.              this.Text = "CAD交互";
  75.              this.ResumeLayout(false);
  76.              this.PerformLayout();

  77.          }

  78.          #endregion

  79.          private System.Windows.Forms.TextBox textBox1;
  80.          private System.Windows.Forms.Button button_get;
  81.          private System.Windows.Forms.Button button_ok;

  82.      }
  83. }
发表于 2021-12-27 08:14 | 显示全部楼层
简单方法可以hook鼠标通过全局位置判断把焦点给谁
 楼主| 发表于 2021-12-28 23:05 | 显示全部楼层
发表于 2022-3-17 13:01 | 显示全部楼层
77077 发表于 2021-12-28 23:05
万能的MJ,找到答案了。
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=169490&extra=page%3D2%2 ...

楼主在么 看到您在2015年发的一个读取文件数据绘断面帖子 有个程序 请问能否分享下代码呢谢谢您http://bbs.mjtd.com/forum.php?mo ... hlight=%B6%CF%C3%E6
发表于 2022-6-4 18:34 | 显示全部楼层
怎么总是错误,一打开非模态窗口,就提示错误,cad关闭
发表于 2022-7-21 12:32 | 显示全部楼层


你这里面明明有个setfocus,放鼠标事件里不就好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 00:55 , Processed in 0.337938 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表