博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中获取控件窗体句柄,获取窗体等的一些操作
阅读量:4302 次
发布时间:2019-05-27

本文共 1903 字,大约阅读时间需要 6 分钟。

1.Control.Handle 就是获取控件绑定到的窗口句柄。

2.control.IsHandleCreated  控件是否有与其关联的句柄

3.WinForm自定义函数FindControl实现按名称查找控件

public static Control FindControl(Control parentControl, string findCtrlName)

{

  Control _findedControl = null;

  if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)

  {

 foreach (Control ctrl in parentControl.Controls)

 {

   if (ctrl.Name.Equals(findCtrlName))

   {

 _findedControl = ctrl;

 break;

   }

 }

  }

  return _findedControl;

}

 

4.判断窗体是否已打开

方式1:

 foreach (Form frm in Application.OpenForms)

 {
    if (frm is youForm)
    {
       youForm.Activate();
       youForm.WindowState = FormWindowState.Normal;
       return;
     }
 }
 Form youForm = new Form();
 youForm.Show();

方式2:

Form1 F1 ;

 
if(F1 == null || F1.IsDisposed)
{
   F1 = new Form1();
   F1.Show();//未打开,直接打开。
}
else
{
   F1.Activate();//已打开,获得焦点,置顶。
}
 

5.通过名字寻找窗体

public Form FindForm(string name)

{

    foreach (Form f in Application.OpenForms)

    {

        if (f.Name == name) return f;

    }

    return null;

}

 6.以下是代码中创建progressbar的实例

 int count = 0;

        private void button4_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(() => {
                Form form = new Form();
                form.Name="myForm";
                form.Width = 200;
                form.Height = 20;
                form.ControlBox = false;
                //form.ShowInTaskbar = false;
                form.StartPosition = FormStartPosition.CenterScreen;
                ProgressBar pb = new ProgressBar();
                pb.Dock = DockStyle.Fill;
                pb.Maximum = 100;
                pb.Minimum = 0;
                pb.Value = count;
                pb.BringToFront();
                pb.Visible = true;
                pb.Parent = form;
                form.ShowDialog();
            
            });
            th.Start();

            for (int i = 0; i < 1000;i++ )

            {
                Thread.Sleep(5);
                count = Convert.ToInt32(i * 1.0 / 1000 * (100 - 0) + 0);
              Control control=  FindForm("myForm");
              if (control!=null&&control.Name == "myForm" && control.IsHandleCreated)
                {
                    control.Invoke(new Action(() => {
                        ProgressBar pb = control.Controls[0] as ProgressBar;
                        pb.Value = count;
                    }));
                   
                }
            }
            th.Abort();
        }
        public Form FindForm(string name)
        {

            foreach (Form f in Application.OpenForms)

            {

                if (f.Name == name) return f;

            }

            return null;

        }

转载地址:http://vzlws.baihongyu.com/

你可能感兴趣的文章
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>
linux的挂载的问题,重启后就挂载就没有了
查看>>
docker原始镜像启动容器并创建Apache服务器实现反向代理
查看>>
docker容器秒死的解决办法
查看>>
管理网&业务网的一些笔记
查看>>
openstack报错解决一
查看>>
openstack报错解决二
查看>>
linux source命令
查看>>
openstack报错解决三
查看>>