winform非模态窗口测试。
MainClass.csusing Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
namespace ModelessDialogTest
{
public class MainClass
{
//注册命令
public void UiStart()
{
var myfrom = new testFrom();
//Application.ShowModalDialog(myfrom); //模态显示
Application.ShowModelessDialog(myfrom); //非模态显示
}
}
}
testFrom.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using TransactionManager = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
namespace ModelessDialogTest
{
public partial class testFrom : Form
{
public testFrom()
{
//界面的初始化
InitializeComponent();
}
//为了解决窗口切换问题,需要利用Windows API函数SetFocus实现
//初始化窗口焦点切换功能
private static extern IntPtr SetFocus(IntPtr hwnd);
/// <summary>
/// 测试测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_get_Click(object sender, EventArgs e)
{
var entopts = new PromptEntityOptions("\n在图中选择一个图元对象");
entopts.Message = "\n在图中选择一个图元对象:";
PromptEntityResult ent = null;
try
{
SetFocus(Application.MainWindow.Handle);//焦点切换到CAD
ent = Application.DocumentManager.MdiActiveDocument.Editor.GetEntity(entopts);
if (ent.Status != PromptStatus.Error)
{
ObjectId entid = ent.ObjectId;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
TransactionManager tm = db.TransactionManager;
using (Transaction myT = tm.StartTransaction())
{
var entity = (Entity) tm.GetObject(entid, OpenMode.ForRead, true);
textBox1.Text = "所选对象Name为:" + entity.GetType().Name;
myT.Commit();
}
}
Focus();
}
catch
{
}
}
private void button_ok_Click(object sender, EventArgs e)
{
Close();
}
}
}
遗留问题:鼠标移出移进时,如何自动切换焦点?private void testFrom_MouseLeave(object sender, EventArgs e)
{
textBox1.Text="";
}
private void testFrom_MouseEnter(object sender, EventArgs e)
{
textBox1.SelectAll();
}
testFrom.Designer.cs
namespace ModelessDialogTest
{
partial class testFrom
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button_get = new System.Windows.Forms.Button();
this.button_ok = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(13, 89);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(244, 28);
this.textBox1.TabIndex = 0;
//
// button_get
//
this.button_get.Location = new System.Drawing.Point(11, 22);
this.button_get.Name = "button_get";
this.button_get.Size = new System.Drawing.Size(102, 49);
this.button_get.TabIndex = 1;
this.button_get.Text = "Get";
this.button_get.UseVisualStyleBackColor = true;
this.button_get.Click += new System.EventHandler(this.button_get_Click);
//
// button_ok
//
this.button_ok.Location = new System.Drawing.Point(155, 22);
this.button_ok.Name = "button_ok";
this.button_ok.Size = new System.Drawing.Size(102, 49);
this.button_ok.TabIndex = 2;
this.button_ok.Text = "OK";
this.button_ok.UseVisualStyleBackColor = true;
this.button_ok.Click += new System.EventHandler(this.button_ok_Click);
//
// testFrom
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 151);
this.Controls.Add(this.button_ok);
this.Controls.Add(this.button_get);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "testFrom";
this.ShowInTaskbar = false;
this.Text = "CAD交互";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button_get;
private System.Windows.Forms.Button button_ok;
}
} 简单方法可以hook鼠标通过全局位置判断把焦点给谁 万能的MJ,找到答案了:lol。
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=169490&extra=page%3D2%26filter%3Dtypeid%26typeid%3D85 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?mod=viewthread&tid=113873&highlight=%B6%CF%C3%E6 怎么总是错误,一打开非模态窗口,就提示错误,cad关闭 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAApCAYAAACm7CmwAAALhUlEQVR4Ae1dMW7cOhD1fXwW3yLYU2wRwKkDBKp+4cZItc03IKRxkbgzNlWAILUXCHIGAcFP+DEkHzkcUfJuVutY1isWlDjkcDR8w8eh1t6zruvcST671q1XK9dsT6T/VHZT72nwQL/Sr8QAMfBMMXB2EhJUD7ttSIan9jH1c7NFDBADxMCfY+DkRLhr18wK1caAYP1zsNJ39B0xQAycAgMkQpIUj2uIAWKAGFg0Bp6ECNftbtFOPsUOhjq5MyYGiAFiYBoMnJwIu27rmtXKrVZr1+6mMZqTTz8SA8QAMUAMTIWBJyBCTtZUk0U9xBIxQAwQA9NjgETIdwM8tiYGiAFiYNEYIBEyABYdANxdT7+7pk/p07lhgERIIiQREgPEADGwaAyQCBkAiw6Aue1caS+zLWJgegyQCEmEJEJigBggBhaNARIhA2DRAcDd9fS7a/qUPp0bBkiEJEISITFADBADi8YAiZABsOgAmNvOlfYy2yIGpscAiZBESCIkBogBYmDRGCARMgAWHQDcXU+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Dx2OAREgiJBESA8QAMbBoDJAIGQCLDgDupo/fTdOH9OHcMUAiJBGSCIkBYoAYWDQGSIQMgEUHwNx3srSf2RgxcDwGSIQkQhIhMUAMEAOLxgCJkAGw6ADgbvr43TR9SB/OHQP/A+DK4KerkVbtAAAAAElFTkSuQmCC
你这里面明明有个setfocus,放鼠标事件里不就好了
页:
[1]