两个不同的程序窗体相互关闭,这个必须要用到 WINDOWS MESSAGE 了,也只有 API 可以搞。
实例化一个要被关闭的那个窗体,然后窗体名.Close()
不能创建新的实例吧,获取要关闭的窗体对应的实例,再调用Close()
这当然没用,你新建一个窗体,再关闭之,和原来要关闭的有什么关系呢?瞎误导人。你可以用Application.OpenForms集合找到要关闭的窗口,再Close: foreach (var item in Application.OpenForms) { if (item is Form1) item.Close(); }
foreach (var item in Application.OpenForms) { if (item is Form1) (item as Form1).Close(); }
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); //发送关闭消息 } } }
以下是我最近的一个工程用到的代码,是关闭所有此程序打开的窗口,仅供参考。 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
要看是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
delegate or override WndProc with ref Message mprotected override void WndProc(ref Message m)
foreach (var item in Application.OpenForms)
{
if (item is Form1) item.Close();
}
{
if (item is Form1) (item as Form1).Close();
}
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); //发送关闭消息
}
}
}
好像是成功了,但是任务栏还是看的见有那几个关闭的窗体。点击打不开。请问什么情况
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
定义一个静态类,里面有静态字段 public static FormA;
你在A的构造函数里或者Load页面里把自己的实例(this)赋值给 静态类.FormA;
即(静态类.FormA = this;)
然后你在任何地点都可以 静态类.FormA.Close(); 了~当然findwindow肯定可以的
以下代码是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
你难道加了什么特殊的处理?
我的解决办法是这样的:
有两个窗体FrmA与FrmB,比如我们要在FrmB中关闭FrmA。
在先建立FrmA,在FrmA中建立一个FrmB的实例,Form FrmB=new Form()
给FrmB指定父级窗体,FrmB.AddOwnedForm(FrmA)
显示FrmB,FrmB.Show()
然后再FrmB中添加一个按钮事件或是别的什么,只需要一句话就关闭了FrmA,隐藏什么的也可以,随意操作。this.Owner.Close()
如果你的窗体时自建的类,加一个强制转换就行了。
很简单,希望能帮到你了
C# 委托实例(跨窗体操作控件) .
http://blog.csdn.net/bdstjk/article/details/7004035
foreach (var item in Application.OpenForms)
{
if (item is Form1) (item as Form1).Close();
break;
}
foreach (var item in Application.OpenForms)
{
if (item is Form1)
{
(item as Form1).Close();
break;
}
}