消息框:
using System.Runtime.InteropServices; namespace Windows_API_实现屏幕右下角_消息框_ { public partial class Msg : Form { public Msg() { InitializeComponent(); Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 3, Screen.PrimaryScreen.WorkingArea.Height - this.Height - 3); this.PointToScreen(p); this.Location = p; } [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void FrmLogin_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } private Thread td; delegate void SetTextCallBack(string txt); private void Msg_Load(object sender, EventArgs e) { OnLoad(); } private void SetText(string txt) { if (string.IsNullOrEmpty(txt)) return; string first = txt.Substring(0, 1); string second = txt.Substring(1, txt.Length - 1); string thrid = second + first; if (lblMsg.InvokeRequired) { SetTextCallBack cb = new SetTextCallBack(SetText); BeginInvoke(cb, thrid); } else lblMsg.Text = thrid; } private void Msg_FormClosing(object sender, FormClosingEventArgs e) { td.Abort(); } internal void OnLoad() { td = new Thread(() => { while (true) { SetText(lblMsg.Text); Thread.Sleep(200); } }); td.Start(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }
namespace Windows_API_实现屏幕右下角_消息框_ { partial class Msg { /// <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() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Msg)); this.lblMsg = new System.Windows.Forms.Label(); this.btnClose = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lblMsg // this.lblMsg.BackColor = System.Drawing.Color.Transparent; this.lblMsg.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblMsg.ForeColor = System.Drawing.Color.DarkGreen; this.lblMsg.Location = new System.Drawing.Point(3, 162); this.lblMsg.Name = "lblMsg"; this.lblMsg.Size = new System.Drawing.Size(269, 23); this.lblMsg.TabIndex = 0; this.lblMsg.Text = "Hello~ WinAPI 实现屏幕右下角“消息框” "; this.lblMsg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // btnClose // this.btnClose.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnClose.BackgroundImage"))); this.btnClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.btnClose.Location = new System.Drawing.Point(244, 0); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(39, 26); this.btnClose.TabIndex = 1; this.btnClose.UseVisualStyleBackColor = true; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // Msg // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.ClientSize = new System.Drawing.Size(285, 190); this.Controls.Add(this.btnClose); this.Controls.Add(this.lblMsg); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "Msg"; this.Text = "Msg"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Msg_FormClosing); this.Load += new System.EventHandler(this.Msg_Load); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.FrmLogin_MouseDown); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Label lblMsg; private System.Windows.Forms.Button btnClose; } }