MessageBox.Show显示到最顶层

简介:

显示可包含文本、按钮和符号(通知并指示用户)的消息框。MFC的MessageBox的MB_TOPMOST属性可以直接设置。Winform的MessageBox没有直接设置的参数。

 

介绍

  • 显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。
MessageBox.Show 方法 (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)
  • 语法 
public static DialogResult Show(
    string text,
    string caption,
    MessageBoxButtons buttons,
    MessageBoxIcon icon,
    MessageBoxDefaultButton defaultButton,
    MessageBoxOptions options
)

 

参数含义

text

类型: System. String
要在消息框中显示的文本。

caption

类型: System. String
要在消息框的标题栏中显示的文本。

buttons

类型: System.Windows.Forms. MessageBoxButtons
MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。

成员名称说明
OK消息框包含“确定”按钮。
OKCancel消息框包含“确定”和“取消”按钮。
AbortRetryIgnore消息框包含“中止”、“重试”和“忽略”按钮。
YesNoCancel消息框包含“是”、“否”和“取消”按钮。
YesNo消息框包含“是”和“否”按钮。
RetryCancel消息框包含“重试”和“取消”按钮。

icon

类型: System.Windows.Forms. MessageBoxIcon
MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。

成员名称说明
None消息框未包含符号。
Hand该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。
Question该消息框包含一个符号,该符号是由一个圆圈和其中的一个问号组成的。 不再建议使用问号消息图标,原因是该图标无法清楚地表示特定类型的消息,并且问号形式的消息表述可应用于任何消息类型。 此外,用户还可能将问号消息符号与帮助信息混淆。 因此,请不要在消息框中使用此问号消息符号。 系统继续支持此符号只是为了向后兼容。
Exclamation该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的。
Asterisk该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的。
Stop该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。
Error该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。
Warning该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的。
Information该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的。

defaultButton

类型: System.Windows.Forms. MessageBoxDefaultButton
MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。

成员名称说明
Button1消息框上的第一个按钮是默认按钮。
Button2消息框上的第二个按钮是默认按钮。
Button3消息框上的第三个按钮是默认按钮。

options

类型: System.Windows.Forms. MessageBoxOptions
MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。 若要使用默认值,请传入 0。

成员名称说明
ServiceNotification消息框显示在活动桌面上。

调用方是一种服务,用于将事件通知用户。 即使没有用户登录到计算机,该功能也会在当前活动桌面上显示一个消息框。

DefaultDesktopOnly消息框显示在活动桌面上。

此常数与 ServiceNotification 相同,只是系统仅在交互窗口站的默认桌面上显示消息框。

DefaultDesktopOnly 将使引发 MessageBox 的应用程序失去焦点。 显示的 MessageBox 将不使用视觉样式。 有关更多信息,请参见 使用视觉样式呈现控件。

RightAlign消息框文本右对齐。
RtlReading指定消息框文本按从右到左的阅读顺序显示。

返回值

返回值

类型: System.Windows.Forms. DialogResult
DialogResult 值之一。

成员名称说明
None从对话框返回了 Nothing。 这表明有模式对话框继续运行。
OK对话框的返回值是 OK(通常从标签为“确定”的按钮发送)。
Cancel对话框的返回值是 Cancel(通常从标签为“取消”的按钮发送)。
Abort对话框的返回值是 Abort(通常从标签为“中止”的按钮发送)。
Retry对话框的返回值是 Retry(通常从标签为“重试”的按钮发送)。
Ignore对话框的返回值是 Ignore(通常从标签为“忽略”的按钮发送)。
Yes对话框的返回值是 Yes(通常从标签为“是”的按钮发送)。
No对话框的返回值是 No(通常从标签为“否”的按钮发送)。

 

使用

  1. 使用带有MessageBoxOptions形参的MessageBox.Show方法。
  2. 前五个变量根据需要自行更改。置顶时,第六个变量为MessageBoxOptions.ServiceNotification或MessageBoxOptions.DefaultDesktopOnly。
  • 第一种
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
  • 第二种 
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);

 

import tkinter as tk from tkinter import messagebox import pymysql def clear(): en1.delete(0, 'end') en2.delete(0, 'end') def create_new_window(): new_window = tk.Toplevel(top) new_window.title('登录成功') new_window.geometry('200x100') label = tk.Label(new_window, text='恭喜您,登录成功!') label.pack() def login(): username = en1.get() password = en2.get() db = pymysql.connect(host='localhost', user='root', passwd='123456', charset='utf8') cursor = db.cursor() cursor.execute("USE dl") sql = "SELECT * FROM users WHERE username='%s'" % username cursor.execute(sql) result = cursor.fetchone() if result is None: messagebox.showerror(title='登录失败', message='用户名错误,登录失败') else: if result[2] != password: messagebox.showerror(title='登录失败', message='密码错误,登录失败') else: create_new_window() db.close() top = tk.Tk() top.title('登录') top.geometry('300x180') label1 = tk.Label(top, text='用户名:', width=6) label1.place(x=50, y=30) en1 = tk.Entry(top, width=20) en1.place(x=100, y=30) label2 = tk.Label(top, text='密码:', width=6) label2.place(x=50, y=60) en2 = tk.Entry(top, width=20, show='*') en2.place(x=100, y=60) bt1 = tk.Button(top, width=10, text='登录', command=login) bt1.place(x=50, y=100) bt2 = tk.Button(top, width=10, text='清除', command=clear) bt2.place(x=170, y=100) top.mainloop() 添加“打开”菜单按钮的功能,当单击“打开”按钮时,在第5关的顶层窗口中显示图片“zsy.jpg”
06-10
import tkinter as tk from tkinter import messagebox, filedialog import pymysql from PIL import Image, ImageTk def clear(): en1.delete(0, 'end') en2.delete(0, 'end') def create_new_window(): new_window = tk.Toplevel(top) new_window.title('登录成功') new_window.geometry('200x150') label = tk.Label(new_window, text='恭喜您,登录成功!') label.pack() # 添加一个标签来显示图片 img_path = filedialog.askopenfilename(title='选择图片', filetypes=(('/3ec76bb35b3b32b2aaf27e8e343c1c88.jpg', '*.jpg'), ('/3ec76bb35b3b32b2aaf27e8e343c1c88.png', '*.png'))) if img_path: img = Image.open(img_path).resize((150, 150)) photo = ImageTk.PhotoImage(img) label_img = tk.Label(new_window, image=photo) label_img.photo = photo label_img.pack() def login(): username = en1.get() password = en2.get() db = pymysql.connect(host='localhost', user='root', passwd='123456', charset='utf8') cursor = db.cursor() cursor.execute("USE dl") sql = "SELECT * FROM users WHERE username='%s'" % username cursor.execute(sql) result = cursor.fetchone() if result is None: messagebox.showerror(title='登录失败', message='用户名错误,登录失败') else: if result[2] != password: messagebox.showerror(title='登录失败', message='密码错误,登录失败') else: create_new_window() db.close() top = tk.Tk() top.title('登录') top.geometry('300x180') # 添加一个菜单栏 menubar = tk.Menu(top) filemenu = tk.Menu(menubar, tearoff=0) filemenu.add_command(label="打开", command=create_new_window) menubar.add_cascade(label="文件", menu=filemenu) top.config(menu=menubar) label1 = tk.Label(top, text='用户名:', width=6) label1.place(x=50, y=30) en1 = tk.Entry(top, width=20) en1.place(x=100, y=30) label2 = tk.Label(top, text='密码:', width=6) label2.place(x=50, y=60) en2 = tk.Entry(top, width=20, show='*') en2.place(x=100, y=60) bt1 = tk.Button(top, width=10, text='登录', command=login) bt1.place(x=50, y=100) bt2 = tk.Button(top, width=10, text='清除', command=clear) bt2.place(x=170, y=100) top.mainloop()当单击“打开”按钮时,在顶层窗口中显示图片“zsy.jpg
06-10
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值