C#利用Form实现Messagebox
1操作类:
1 using System; 2 using System.Linq; 3 using System.Collections.Generic; 4 using System.Text; 5 using System.Runtime.InteropServices; 6 using System.Drawing; 7 using System.Windows.Forms; 8 9 namespace MessageLibary 10 { 11 public class Win32 12 { 13 #region Hide Menu 14 public const uint POWER_FORCE = 0x00001000u; 15 public const uint POWER_STATE_RESET = 0x00800000u; // reset state 16 17 [DllImport("coredll.dll")] 18 public static extern uint SetSystemPowerState([MarshalAs(UnmanagedType.LPWStr)]string psState, uint StateFlags, uint Options); 19 20 [DllImport("coredll.dll", EntryPoint = "FindWindow")] 21 22 public static extern int FindWindow(string lpWindowName, string lpClassName); 23 [DllImport("coredll.dll", EntryPoint = "ShowWindow")] 24 25 public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); 26 [DllImport("coredll.dll", EntryPoint = "SystemParametersInfo")] 27 public static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni); 28 29 public const int SPI_SETWORKAREA = 47; 30 public const int SPI_GETWORKAREA = 48; 31 32 public const int SW_HIDE = 0x00; 33 public const int SW_SHOW = 0x0001; 34 public const int SPIF_UPDATEINIFILE = 0x01; 35 #endregion 36 37 #region MouseMove --//-- 38 39 //不能用 40 [DllImport("user32.dll")] 41 public static extern bool ReleaseCapture(); 42 [DllImport("user32.dll")] 43 public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 44 public const int WM_SYSCOMMAND = 0x0112; 45 public const int SC_MOVE = 0xF010; 46 public const int HTCAPTION = 0x0002; 47 48 #endregion 49 50 #region Methods 51 52 public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld) 53 { 54 int Hwnd = 0; 55 Hwnd = Win32.FindWindow("HHTaskBar", null); 56 if (Hwnd == 0) return false; 57 if (fullscreen) 58 { 59 Win32.ShowWindow((IntPtr)Hwnd, Win32.SW_HIDE); 60 Rectangle rectFull = Screen.PrimaryScreen.Bounds; 61 Win32.SystemParametersInfo(Win32.SPI_GETWORKAREA, 0, ref rectOld, Win32.SPIF_UPDATEINIFILE);//get 62 Win32.SystemParametersInfo(Win32.SPI_SETWORKAREA, 0, ref rectFull, Win32.SPIF_UPDATEINIFILE);//set 63 } 64 else 65 { 66 Win32.ShowWindow((IntPtr)Hwnd, Win32.SW_SHOW); 67 Win32.SystemParametersInfo(Win32.SPI_SETWORKAREA, 0, ref rectOld, Win32.SPIF_UPDATEINIFILE); 68 } 69 return true; 70 } 71 72 #endregion 73 } 74 }
MessageLibary 类:
1 using System; 2 using System.Linq; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace MessageLibary 11 { 12 public partial class MessageLibary : Form 13 { 14 public MessageLibary() 15 { 16 InitializeComponent(); 17 } 18 19 private void MessageLibary_Load(object sender, EventArgs e) 20 { 21 //this.title.Image = new Bitmap(); 22 Rectangle rectangle = Screen.PrimaryScreen.Bounds; 23 Win32.SetFullScreen(true, ref rectangle);//false为恢复状态栏 24 } 25 26 private void title_Paint(object sender, PaintEventArgs e) 27 { 28 Control con = (Control)sender; 29 Graphics g = e.Graphics; 30 float x = 0, y = 0; 31 string title = "title message"; 32 Font font = new Font("宋体",9,FontStyle.Regular); 33 SizeF size = g.MeasureString(title,font); 34 y = (con.Height - size.Height) / 2; 35 g.DrawString(title,font,new SolidBrush(Color.White),x,y); 36 g.Dispose(); 37 } 38 39 private void can_Click(object sender, EventArgs e) 40 { 41 this.Close(); 42 Rectangle rectangle = Screen.PrimaryScreen.Bounds; 43 Win32.SetFullScreen(false, ref rectangle);//false为恢复状态栏 44 } 45 46 private void MessageLibary_MouseMove(object sender, MouseEventArgs e) 47 { 48 } 49 Point point; 50 bool leftBnt = false; 51 private void title_MouseMove(object sender, MouseEventArgs e) 52 { 53 if (leftBnt) 54 { 55 Point offset = Control.MousePosition; 56 offset.Offset(point.X, point.Y); 57 this.Location = offset; 58 } 59 } 60 61 private void title_MouseDown(object sender, MouseEventArgs e) 62 { 63 if (e.Button == MouseButtons.Left) 64 { 65 point = new Point(-e.X, -e.Y); 66 leftBnt = true; 67 } 68 } 69 70 private void title_MouseUp(object sender, MouseEventArgs e) 71 { 72 if (e.Button == MouseButtons.Left) 73 { 74 leftBnt = true; 75 } 76 } 77 } 78 }
测试:
MessageLibary.MessageLibary message = new MessageLibary.MessageLibary();
message.ShowDialog();