日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#中获取控件窗体句柄,获取窗体等的一些操作

發布時間:2023/12/18 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#中获取控件窗体句柄,获取窗体等的一些操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;

? ? ? ? }

總結

以上是生活随笔為你收集整理的c#中获取控件窗体句柄,获取窗体等的一些操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。