功能—效果展示
在主程序最小化时,希望显示一个浮窗(支持拖拽),显示当前的运行信息,功能如下:

点击最小化,主界面关闭,浮窗出现点击浮窗主界面按钮,浮窗关闭,主界面出现切歌,可根据具体业务逻辑添加内容退出,退出当前程序
浮窗代码
新建一个Winform(.NetFramework)项目,添加一个窗体FloatingWindow.cs

代码如下:
FloatingForm.Designer.cs
partial class FloatingForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainPanel = new System.Windows.Forms.Panel();
this.exitButton = new System.Windows.Forms.Button();
this.nextMusic = new System.Windows.Forms.Button();
this.mainFormButton = new System.Windows.Forms.Button();
this.lbl_music = new System.Windows.Forms.Label();
this.mainPanel.SuspendLayout();
this.SuspendLayout();
//
// mainPanel
//
this.mainPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(120)))), ((int)(((byte)(230)))), ((int)(((byte)(240)))), ((int)(((byte)(250)))));
this.mainPanel.Controls.Add(this.exitButton);
this.mainPanel.Controls.Add(this.nextMusic);
this.mainPanel.Controls.Add(this.mainFormButton);
this.mainPanel.Controls.Add(this.lbl_music);
this.mainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.mainPanel.Location = new System.Drawing.Point(6, 7);
this.mainPanel.Margin = new System.Windows.Forms.Padding(4);
this.mainPanel.Name = "mainPanel";
this.mainPanel.Size = new System.Drawing.Size(361, 132);
this.mainPanel.TabIndex = 0;
this.mainPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_MouseDown);
this.mainPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_MouseMove);
//
// exitButton
//
this.exitButton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(53)))), ((int)(((byte)(69)))));
this.exitButton.FlatAppearance.BorderSize = 0;
this.exitButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.exitButton.ForeColor = System.Drawing.Color.White;
this.exitButton.Location = new System.Drawing.Point(263, 81);
this.exitButton.Name = "exitButton";
this.exitButton.Size = new System.Drawing.Size(75, 29);
this.exitButton.TabIndex = 3;
this.exitButton.Text = "退出";
this.exitButton.UseVisualStyleBackColor = false;
this.exitButton.Click += new System.EventHandler(this.ExitButton_Click);
//
// nextMusic
//
this.nextMusic.BackColor = System.Drawing.Color.ForestGreen;
this.nextMusic.FlatAppearance.BorderSize = 0;
this.nextMusic.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.nextMusic.ForeColor = System.Drawing.Color.White;
this.nextMusic.Location = new System.Drawing.Point(133, 81);
this.nextMusic.Name = "nextMusic";
this.nextMusic.Size = new System.Drawing.Size(94, 29);
this.nextMusic.TabIndex = 2;
this.nextMusic.Text = "切歌";
this.nextMusic.UseVisualStyleBackColor = false;
this.nextMusic.Click += new System.EventHandler(this.ChangeMusicButton_Click);
//
// mainFormButton
//
this.mainFormButton.BackColor = System.Drawing.Color.SteelBlue;
this.mainFormButton.FlatAppearance.BorderSize = 0;
this.mainFormButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.mainFormButton.ForeColor = System.Drawing.Color.White;
this.mainFormButton.Location = new System.Drawing.Point(21, 81);
this.mainFormButton.Name = "mainFormButton";
this.mainFormButton.Size = new System.Drawing.Size(75, 29);
this.mainFormButton.TabIndex = 1;
this.mainFormButton.Text = "主界面";
this.mainFormButton.UseVisualStyleBackColor = false;
this.mainFormButton.Click += new System.EventHandler(this.MainFormButton_Click);
//
// lbl_music
//
this.lbl_music.BackColor = System.Drawing.Color.White;
this.lbl_music.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_music.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl_music.Location = new System.Drawing.Point(21, 21);
this.lbl_music.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.lbl_music.Name = "lbl_music";
this.lbl_music.Size = new System.Drawing.Size(317, 40);
this.lbl_music.TabIndex = 0;
this.lbl_music.Text = "孤勇者——陈奕迅";
this.lbl_music.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// FloatingWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(373, 146);
this.Controls.Add(this.mainPanel);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "FloatingWindow";
this.Padding = new System.Windows.Forms.Padding(6, 7, 6, 7);
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "FloatingWindow";
this.TopMost = true;
this.mainPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel mainPanel;
private System.Windows.Forms.Label lbl_music;
private System.Windows.Forms.Button mainFormButton;
private System.Windows.Forms.Button exitButton;
private System.Windows.Forms.Button nextMusic;
}
FloatingForm.cs
public partial class FloatingForm: Form
{
// Windows API 用于设置窗体圆角
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(
int nLeftRect, int nTopRect, int nRightRect, int nBottomRect,
int nWidthEllipse, int nHeightEllipse);
public FloatingWindow()
{
InitializeComponent();
}
// 主窗体引用
private MainForm mainForm;
public FloatingWindow(MainForm mainForm)
{
InitializeComponent();
// 保存主窗体引用
this.mainForm = mainForm;
// 设置圆角
this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
// 初始位置设为屏幕右下角
Screen screen = Screen.PrimaryScreen;
this.Location = new Point(
screen.WorkingArea.Width - this.Width - 20,
screen.WorkingArea.Height - this.Height - 100
);
}
// 按钮点击事件处理
private void MainFormButton_Click(object sender, EventArgs e)
{
if (mainForm != null)
{
// 显示主窗体并恢复正常状态
mainForm.Show();
mainForm.WindowState = FormWindowState.Normal;
// 隐藏悬浮窗
this.Hide();
}
}
/// <summary>
/// 退出界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ExitButton_Click(object sender, EventArgs e)
{
// 确认退出
if (MessageBox.Show("确定要退出程序吗?", "退出确认",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
// 关闭主窗体
if (mainForm != null)
{
mainForm.Close();
}
// 关闭悬浮窗
this.Close();
// 退出应用程序
Application.Exit();
}
}
#region 无边框拖拽
private Point mPoint;
private void panel_MouseDown(object sender, MouseEventArgs e)
{
mPoint = new Point(e.X, e.Y);
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
}
#endregion
/// <summary>
/// 切换歌曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChangeMusicButton_Click(object sender, EventArgs e)
{
}
}
主程序代码

MainForm.Design.cs
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(278, 89);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(135, 24);
this.label1.TabIndex = 0;
this.label1.Text = "主程序界面";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(688, 281);
this.Controls.Add(this.label1);
this.Name = "MainForm";
this.Text = "Form1";
this.Load += new System.EventHandler(this.frmMain_Load);
this.SizeChanged += new System.EventHandler(this.frmMain_SizeChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 窗体初始状态
/// </summary>
private FormWindowState fwsPrevious;
/// <summary>
/// 悬浮窗体
/// </summary>
private FloatingForm floatingWindow;
/// <summary>
/// 主窗体的Load事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_Load(object sender, EventArgs e)
{
fwsPrevious = this.WindowState;
floatingWindow = new FloatingForm(this);
}
/// <summary>
/// 主窗体的SizeChanged事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
// 隐藏主窗体
this.Hide();
this.ShowInTaskbar = false;
// 显示悬浮窗
if (floatingWindow != null && !floatingWindow.Visible)
{
floatingWindow.Show();
}
}
else if (this.WindowState != fwsPrevious)
{
fwsPrevious = this.WindowState;
}
}
/// <summary>
/// 还原窗口方法,即供悬浮窗口进行调用的。
/// </summary>
public void RestoreWindow()
{
this.WindowState = fwsPrevious;
this.ShowInTaskbar = true;
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...


