你可以api的findwindow找到,然后发送WM_Close消息,这是跨进程的,同进程也可以同进程还可以传递窗体实例,如frm,直接 frm.Close();

解决方案 »

  1.   

    有sendmessage postmessage,close等方法。
      

  2.   

    两个不同的程序窗体相互关闭,这个必须要用到 WINDOWS MESSAGE 了,也只有 API 可以搞。
      

  3.   

    实例化一个要被关闭的那个窗体,然后窗体名.Close()
      

  4.   

    不能创建新的实例吧,获取要关闭的窗体对应的实例,再调用Close()
      

  5.   

    这当然没用,你新建一个窗体,再关闭之,和原来要关闭的有什么关系呢?瞎误导人。你可以用Application.OpenForms集合找到要关闭的窗口,再Close:
    foreach (var item in Application.OpenForms)
    {
        if (item is Form1) item.Close();
    }
      

  6.   

             up                  up                 求解答 、
      

  7.   

    item 报错  没有CLOSE()方法
      

  8.   

    Application.OpenForms["Form1"].Close();其中的Form1是你窗体的名字,不要写错。
      

  9.   

    foreach (var item in Application.OpenForms)
    {
      if (item is Form1) (item as Form1).Close();
    }
      

  10.   

     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace Wind_Test
    {
        public partial class Form1 : Form
        {
            public static int WM_CLOSE = 0x10;
            public Form1()
            {
                InitializeComponent();
            }
            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); //获取窗体句柄        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //发送消息        private void button1_Click(object sender, EventArgs e)
            {
                IntPtr hwndCalc = FindWindow(null, "计算器"); //查找计算器的句柄(这里你可以改下 比如“Form1”)
                SendMessage(hwndCalc,WM_CLOSE, 0, 0); //发送关闭消息
            }
        }
    }
      

  11.   

    和Application.OpenForms["Form1"].Close();这个方法效果一样
      好像是成功了,但是任务栏还是看的见有那几个关闭的窗体。点击打不开。请问什么情况
      

  12.   

    18L 正解啊 就是用api  FindWindow先找窗体 然后SendMessage或PostMessage发关闭消息..
      

  13.   

    以下是我最近的一个工程用到的代码,是关闭所有此程序打开的窗口,仅供参考。
     Dim frs As Windows.Forms.FormCollection = Application.OpenForms  
     Dim frnm As List(Of String) = New List(Of String)
            For Each r As Form In frs
                If r.Name <> "stmu" Then   
                    frnm.Add(r.Name)
                End If
            Next        If frnm.Count > 0 Then
                For Each tt As String In frnm
                    frs(tt).Close()
                Next
            End If
      

  14.   

    你打开两个窗体的时候 假如B窗体要被A窗体关闭  那你就把当前打开的B窗体传给A嘛  在A里面直接Close()试试
      

  15.   

       获取窗体的句柄,通过windows message  WM_CLOSE消息来关闭窗体
      

  16.   

       findwindow查找窗体 sendmessage 发送消息关闭窗体
      

  17.   

    其实我可以跟你说个方法:比如你要在FormB里面关闭FormA
    定义一个静态类,里面有静态字段 public static FormA;
    你在A的构造函数里或者Load页面里把自己的实例(this)赋值给 静态类.FormA;
    即(静态类.FormA = this;)
    然后你在任何地点都可以  静态类.FormA.Close(); 了~当然findwindow肯定可以的
      

  18.   

    要看是winform还是wpf
    以下代码是WPF中的,亲测有效
    #Region "根据窗体名称对其关闭"
        ''' <summary>
        ''' 根据窗体名称关闭窗体
        ''' </summary>
        ''' <param name="strName">Name of the STR.</param>
        Public Sub ShutDowWindow(ByVal strName As String)
            Dim ws = From wd In Application.Current.Windows _
                   Where wd.name = strName _
                   Select wd
            If ws.Count > 0 Then
                CType(ws.First(), Window).Close()
            End If
        End Sub
    #End Region
      

  19.   

    delegate or override  WndProc with ref Message mprotected override void WndProc(ref Message m)
      

  20.   

      两个窗体同show出来,主要开发窗体的半透明和控件的不透明效果
      

  21.   

    如果没有从属关系这种方式是可以的System.Windows.Forms.Application.OpenForms["Form3"].Close();
    你难道加了什么特殊的处理?
      

  22.   

    我也接触到一个与lz一样的问题
    我的解决办法是这样的:
    有两个窗体FrmA与FrmB,比如我们要在FrmB中关闭FrmA。
    在先建立FrmA,在FrmA中建立一个FrmB的实例,Form FrmB=new Form()
    给FrmB指定父级窗体,FrmB.AddOwnedForm(FrmA)
    显示FrmB,FrmB.Show()
    然后再FrmB中添加一个按钮事件或是别的什么,只需要一句话就关闭了FrmA,隐藏什么的也可以,随意操作。this.Owner.Close()
    如果你的窗体时自建的类,加一个强制转换就行了。
    很简单,希望能帮到你了
      

  23.   

    up  up ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...................正在阅读
      

  24.   

    看看我这个例子吧
    C# 委托实例(跨窗体操作控件) .
    http://blog.csdn.net/bdstjk/article/details/7004035
      

  25.   

    16L可以的,加个break
    foreach (var item in Application.OpenForms)
    {
      if (item is Form1) (item as Form1).Close();
      break;
    }
      

  26.   

    如果两个窗体之间没有主从的关系,把上面的再加个{}
    foreach   (var   item   in   Application.OpenForms)
    {
        if   (item   is   Form1)  
        { 
            (item   as   Form1).Close();
            break;
        }
    }