110,499
社区成员
发帖
与我相关
我的任务
分享
第3,使用单例只出现一个窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
private Form2()
{
InitializeComponent();
}
private static Form2 MyForm = new Form2();
public static void MyShow()
{
if (MyForm == null || MyForm.IsDisposed)
MyForm = new Form2();
MyForm.Show();
MyForm.Activate();
}
}
}
第2指定标题
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd,
int Msg,
int wParam,
int lParam
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string
lpWindowName);
private static int WM_CLOSE = 0x10;
private void button1_Click(object sender, EventArgs e)
{
SendMessage(FindWindow(null,"输入要关闭窗体的标题"), WM_CLOSE, 0, 0);
}
第1:指定句柄
private static extern int SendMessage(
int hWnd,
int Msg,
int wParam,
int lParam
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string
lpWindowName);
private static int WM_CLOSE = 0x10;
private void button1_Click(object sender, EventArgs e)
{
SendMessage(输入要关闭的句子柄, WM_CLOSE, 0, 0);
}