Winform如何做像QQ天气一样,若天气窗体获得鼠标则不关闭,否则过会就关闭
Winform如何做像QQ天气一样,若天气窗体获得鼠标则不关闭,否则过会就关闭。并且像天气窗体一样无法移动。停靠在主窗体边上。 QQ 天气 停靠
[解决办法]
主窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form2 m_frm2=null; //天气窗体
private void Form1_Load(object sender, EventArgs e)
{
this.MouseMove += Form1_MouseMove;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.X + "," + e.Y;
if (m_frm2 != null)
return;
if (e.X > 100 && e.Y > 100 && e.X < 200 && e.Y < 200) //主窗体(Form1)鼠标进入[100,100]-[200,200]区域显示天气窗体(Form2)
{
m_frm2 = new Form2();
m_frm2.m_frm1 = this;
m_frm2.Show();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form1 m_frm1 = null; //主窗体
private void Form2_Load(object sender, EventArgs e)
{
if (m_frm1 != null)
{
this.Left = m_frm1.Left - this.Width;
this.Top = m_frm1.Top;
}
this.FormBorderStyle = FormBorderStyle.None;
this.MouseLeave += Form2_MouseLeave;
}
void Form2_MouseLeave(object sender, EventArgs e) //鼠标移出,关闭天气窗体
{
if (m_frm1 != null)
m_frm1.m_frm2 = null;
this.Close();
}
}
}