COM组件如果不是拖拽方式创建,写代码如何实现??

ponydph 2014-12-25 03:13:15
通过拖拽控件 生成的实例 创建代码如下

this.axvg1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axvg1.Enabled = true;
this.axvg1.Location = new System.Drawing.Point(0, 0);
this.axvg1.Name = "axvg1";
this.axvg1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axvg1.OcxState")));
this.axvg1.Size = new System.Drawing.Size(484, 369);
this.axvg1.TabIndex = 0;
//

如果是不用拖拽方式,手动创建的代码的话, this.axvg1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axvg1.OcxState"))); 的含义如何替换 才能实现??
...全文
152 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ponydph 2014-12-26
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
[quote=引用 4 楼 ponydph 的回复:] 查看Designer.cs代码,其他的都容易懂, axvg1是控件的名字,下面设置ocxState是指什么??
人家知道 axvg 是个控件,人家问你 axvg1 是个什么类型的玩意。 如果你说不出来,把 axvg 这个变量的定义语句贴出来。 [/quote] public class Form1 : System.Windows.Forms.Form { private Axgraphctrl.Axgraphctrl axvgctrl; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.axvgctrl= new Axvgctrl40.Axgraphctrl(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.axvgctrl1)).BeginInit(); this.SuspendLayout(); // // axvgctrl1 // this.axvgctrl1.Enabled = true; this.axvgctrl1.Location = new System.Drawing.Point(16, 24); this.axvgctrl1.Name = "axvgctrl1"; this.axvgctrl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axvgctrl1.OcxState"))); this.axvgctrl1.Size = new System.Drawing.Size(192, 192); this.axvgctrl1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(224, 24); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(464, 334); this.Controls.Add(this.button3); this.Controls.Add(this.axvgctrl2); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.axvgctrl1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.axvgctrl1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { axvgctrl1.Run( Application.StartupPath + "\\电力设备.tbl" ); } } //此问题是 在【选择项】中选择COM 添加组件到工具箱后,直接拖动 会将编译器死住, 如果调试运行 发现出问题的代码为: ((System.ComponentModel.ISupportInitialize)(this.axvgctrl1)).EndInit(); 不采用拖动的方式,采用手动创建的代码 能否避免上述问题?
devmiao 2014-12-25
  • 打赏
  • 举报
回复
var type = Type.GetTypeFromProgID(ProgId); // ProgId 就是axvg1.OcxState var comObject = Activator.CreateInstance(type);
  • 打赏
  • 举报
回复
引用 4 楼 ponydph 的回复:
查看Designer.cs代码,其他的都容易懂, axvg1是控件的名字,下面设置ocxState是指什么??
人家知道 axvg 是个控件,人家问你 axvg1 是个什么类型的玩意。 如果你说不出来,把 axvg 这个变量的定义语句贴出来。
ponydph 2014-12-25
  • 打赏
  • 举报
回复
没有说明白,再解释一下。 这个控件最早是在VS2005的时候是可以拖到界面上,直接可以使用的。 这个控件是个画图的应用,创建后生成一个窗口 但是在VS2008 中,只要一拖到界面上,整个VS就死住了。 如果手动创建的的时候, 查看Designer.cs代码,其他的都容易懂, axvg1是控件的名字,下面设置ocxState是指什么?? axvg1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("xxx1.OcxState")));
江南小鱼 2014-12-25
  • 打赏
  • 举报
回复
你先拖拽到窗体,然后去designer.cs找到生成的代码,把COM相关的代码在cs里面使用。
於黾 2014-12-25
  • 打赏
  • 举报
回复
axvg1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axvg1.OcxState"))); 这里面"axvg1.OcxState",axvg1不就是它的name吗 如果你代码创建的代码如下: ThisCom axvg1=new ThisCom(); axvg1.Name = "xxx1"; axvg1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("xxx1.OcxState")));
於黾 2014-12-25
  • 打赏
  • 举报
回复
axvg1是个什么玩意 首先你就必须初始化它,因为我不知道你到底用的什么com组件,所以先用ThisCom代替它的类名 ThisCom axvg1=new ThisCom(); 其实你拖拽完,看看设计器代码里多出了什么东西,复制出来用就好了

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧