且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WinForm如何实现同时让两个窗体激活的效果

更新时间:2022-09-01 09:06:19

利用上了windows api,当一个窗体激活的时候会给另外一个发消息,具体实现如下:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication43
{
public partial class Form1 : Form
{
Form frm =null;
public Form1()
{
InitializeComponent();
this.Activated += Form_Activated;
}

const int WM_NCACTIVATE = 0x86;

const int WA_ACTIVE = 0x1;

[DllImport("user32.dll", EntryPoint = "SendMessage")]

public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
frm = new Form();
frm.Text = "jinjazz";
frm.Activated += Form_Activated;
frm.Show();
frm.Location = new System.Drawing.Point(this.Left + this.Width, this.Top);
SendMessage(this.Handle, WM_NCACTIVATE, WA_ACTIVE, 0);
}
void Form_Activated(object sender, EventArgs e)
{
SendMessage(this.Handle, WM_NCACTIVATE, WA_ACTIVE, 0);
if (frm != null)
SendMessage(frm.Handle, WM_NCACTIVATE, WA_ACTIVE, 0);
}
}
}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/09/1563033.html,如需转载请自行联系原作者