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();

 

转载于:https://www.cnblogs.com/fmblzf/p/3632876.html

你可以使用C#中的InputBox类来实现带输入的MessageBox弹窗,这个类需要自己编写实现,以下是一个简单的实现示例: ```csharp using System; using System.Windows.Forms; public static class InputBox { public static string Show(string prompt, string title = "", string defaultValue = "") { Form form = new Form(); Label promptLabel = new Label(); TextBox textBox = new TextBox(); Button okButton = new Button(); Button cancelButton = new Button(); form.Text = title; promptLabel.Text = prompt; textBox.Text = defaultValue; okButton.Text = "OK"; cancelButton.Text = "Cancel"; okButton.DialogResult = DialogResult.OK; cancelButton.DialogResult = DialogResult.Cancel; promptLabel.SetBounds(9, 20, 372, 13); textBox.SetBounds(12, 36, 372, 20); okButton.SetBounds(228, 72, 75, 23); cancelButton.SetBounds(309, 72, 75, 23); promptLabel.AutoSize = true; textBox.Anchor = textBox.Anchor | AnchorStyles.Right; okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; form.ClientSize = new System.Drawing.Size(396, 107); form.Controls.AddRange(new Control[] { promptLabel, textBox, okButton, cancelButton }); form.ClientSize = new System.Drawing.Size(Math.Max(300, promptLabel.Right + 10), form.ClientSize.Height); form.FormBorderStyle = FormBorderStyle.FixedDialog; form.StartPosition = FormStartPosition.CenterScreen; form.MinimizeBox = false; form.MaximizeBox = false; form.AcceptButton = okButton; form.CancelButton = cancelButton; DialogResult dialogResult = form.ShowDialog(); string result = textBox.Text; return dialogResult == DialogResult.OK ? result : ""; } } ``` 使用方法: ```csharp string input = InputBox.Show("请输入内容:", "标题", "默认值"); if (!string.IsNullOrEmpty(input)) { MessageBox.Show("您输入的内容是:" + input); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值