通过enablewindow方法禁用窗口形式 [英] disable window form by enablewindow method

查看:110
本文介绍了通过enablewindow方法禁用窗口形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用此代码进行窗口启用,并且过程可以继续,但是有时它可以工作,或者有时它无法禁用窗口,请帮助我在这段代码中可以做什么.


hello, i use this code for window enable and process can continue, but sometimes it works or sometimes it fails to disable window, please help me what can i do in this code.


[DllImport("user32.dll")]
       public static extern IntPtr FindWindow(String sClassName, String sAppName);
       [DllImport("user32.dll")]
       static extern bool EnableWindow(IntPtr hwnd, bool bEnable);







private void button1_Click(object sender, EventArgs e)
      {
          try
          {
              
              Form2 obj = new Form2();
              IntPtr hwnd = FindWindow(null, "Form1");
              EnableWindow(hwnd, false);
              Refresh();
              obj.Show();
              
              listView2.View = View.Details;
              listView2.Items.Clear();
              for (int j = 0; j <1000; j++)
              {
           
                  listView2.GridLines = true;
                  if (listView2.Items.Count < str)
                  {
                      listView2.Items.Add(j.ToString());
                  }
                 
                  Application.DoEvents();
              }
              obj.Close();

                 EnableWindow(hwnd, true);
             //     this.Show();

          }

推荐答案

代码不正确.
首先,什么是"Form1".是其他一些.NET表单吗,使用P/Invoke根本没有任何意义.顺便看看属性
我很害怕猜测它是否是正在运行的应用程序创建的窗口.如果是这样,那就没什么可谈的了.

您为什么认为按名称查找窗口是您需要的窗口.按名称查找任何东西是完全不可靠的,因此不应该这样做.

其余的没有意义.试图找到一个窗口.当找不到窗口时,代码将完全忽略这种情况.您尝试禁用它,显示并关闭.无论如何,为什么要禁用它呢?在当前表单中,添加了1000个列表项.项目的数量是立即常数,是硬编码的.根据经验,Application.DoEvents不会使用线程,而不必使用线程.

我看不到一条有意义的单行.为什么要那样做?

—SA
The code is bad.
First of all, what is "Form1". Is it is some other .NET Form, using P/Invoke makes no sense at all. By the way, look at the property
I''m terrified to guess if it is a window created by the running application. If it is so, there is nothing to talk about.

Why do you think that finding window by name it is the window you need. Finding anything by name is totally unreliable and should not be done.

The rest makes no sense. There is an attempt to find a window. The code totally ignores the case when the window is not found. You try to disable it, show and close. Why disabling it if you close it, anyway. In the current form, 1000 list items are added. The number of items is immediate constant, hard-coded. Instead of threading, Application.DoEvents is used, which should never be done, as a rule of thumb.

I fail to see a single line which makes sense. Why doing all that?

—SA


这篇关于通过enablewindow方法禁用窗口形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆