如题,只知道这个窗体是其他软件系统抛出的错误的窗体,现在我的工具要实现找到该窗体后关闭它
窗体上显示的名字比如是HuangHePower
第一次接触调用API函数的东西,大家给个例子学习下
主要是如何找到该窗体,然后程序能够关闭找到的窗体

解决方案 »

  1.   

    先找到,然后关    public const int WM_CLOSE = 0x10;    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        private void button1_Click(object sender, System.EventArgs e)
        {
            IntPtr hwnd_win;        hwnd_win = FindWindow(null, "要找的窗体名");
            SendMessage(hwnd_win, WM_CLOSE, 0, 0);
        }
      

  2.   

    //查找窗体
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            private static extern int FindWindow(string lpClassName, string lpWindowName);
            static void Main()
            {             //@Form1根据窗体的Text文本值获得窗体
                    int WINDOW_HANDLER = FindWindow(null, @"Form1");
             }
      

  3.   

    利用2楼的方法测试一个记事本窗口可以关闭
    想问下的是那个WM_CLOSE的定义是什么意思?
      

  4.   

        public const int WM_CLOSE = 0x10;    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        private void button1_Click(object sender, System.EventArgs e)
        {
            IntPtr hwnd_win;        hwnd_win = FindWindow(null, "要找的窗体名");
            SendMessage(hwnd_win, WM_CLOSE, 0, 0);
        }2楼这段测试可行,搂主可以直接用。
      

  5.   

     
    //获取应用程序名称
            public List<string> GetRunApplicationList(Form appForm)
            {
                List<string> appString = new List<string>();
                try
                {
                    int handle = (int)appForm.Handle;
                    int hwCurr;
                    hwCurr = GetWindow(handle, GW_HWNDFIRST);
                    while (hwCurr > 0)
                    {
                        int isTask = (WS_VISIBLE | WS_BORDER);
                        int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
                        bool taskWindow = ((lngStyle & isTask) == isTask);
                        if (taskWindow)
                        {
                            int length = GetWindowTextLength(new IntPtr(hwCurr));
                            StringBuilder sb = new StringBuilder(2 * length + 1);
                            GetWindowText(hwCurr, sb, sb.Capacity);
                            string strTitle = sb.ToString();
                            if (!string.IsNullOrEmpty(strTitle))
                            {
                                appString.Add(strTitle);
                            }
                        }
                        hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取应用程序信息时出错:" + ex.Message);
                }
                return appString;
            }调用List<string> list=GetRunApplicationList(this);
    foreach(string var in list)
    {
    if(var=="HuangHePower")
    ......
    }应该可以的。
      

  6.   

     //关闭不信任的应用程序,如我的电脑,网上邻居
                                                try
                            {
                                IntPtr findPtr = FindWindow(null, "HuangHePower");
                                PostMessage(findPtr, WM_CLOSE, 0, 0);
                            }
                            catch
                            {
                            }