乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 178|回复: 6

winform非模态窗口测试。

[复制链接]

96

主题

351

帖子

62

银币

中流砥柱

Rank: 25

铜币
692
发表于 2021-12-25 23:02:00 | 显示全部楼层 |阅读模式
MainClass.cs
  1. using Autodesk..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.         ///
  24.         /// 测试测试
  25.         ///
  26.         ///
  27.         ///
  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.         }

回复

使用道具 举报

96

主题

351

帖子

62

银币

中流砥柱

Rank: 25

铜币
692
发表于 2021-12-25 23:11:00 | 显示全部楼层
testFrom.Designer.cs
  1. namespace ModelessDialogTest
  2. {
  3.      partial class testFrom
  4.      {
  5.          ///
  6.          /// Required designer variable.
  7.          ///
  8.          private System.ComponentModel.IContainer components = null;
  9.          ///
  10.          /// Clean up any resources being used.
  11.          ///
  12.          /// true if managed resources should be disposed; otherwise, false.
  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.          ///
  23.          /// Required method for Designer support - do not modify
  24.          /// the contents of this method with the code editor.
  25.          ///
  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. }
回复

使用道具 举报

2

主题

36

帖子

7

银币

初来乍到

Rank: 1

铜币
44
发表于 2021-12-27 08:14:00 | 显示全部楼层
简单方法可以hook鼠标通过全局位置判断把焦点给谁
回复

使用道具 举报

96

主题

351

帖子

62

银币

中流砥柱

Rank: 25

铜币
692
发表于 2021-12-28 23:05:00 | 显示全部楼层
万能的MJ,找到答案了。
回复

使用道具 举报

13

主题

106

帖子

11

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
156
发表于 2022-3-17 13:01:00 | 显示全部楼层

楼主在么 看到您在2015年发的一个读取文件数据绘断面帖子 有个程序 请问能否分享下代码呢谢谢您
回复

使用道具 举报

2

主题

16

帖子

6

银币

初来乍到

Rank: 1

铜币
24
发表于 2022-6-4 18:34:00 | 显示全部楼层
怎么总是错误,一打开非模态窗口,就提示错误,cad关闭
回复

使用道具 举报

1

主题

21

帖子

6

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-21 12:32:00 | 显示全部楼层


                               
登录/注册后可看大图


你这里面明明有个setfocus,放鼠标事件里不就好了
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 01:44 , Processed in 0.319158 second(s), 66 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表