C# 弹出框DialogForm

1.新建一个MyMessageBoxForm窗体,在界面中添加确认和取消两个按钮。

复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class MyMessageBoxForm : Form
    {
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern IntPtr GetActiveWindow();//获取当前窗体的活动状态
        String msg = "";

        public delegate void TextEventHandler(string strText);
        public TextEventHandler TextHandler;
        public MyMessageBoxForm(String msg)
        {
            InitializeComponent();

            // 设置窗体为无边框样式
            this.FormBorderStyle = FormBorderStyle.None;

            // 设置透明键颜色(这里使用洋红色,你可以选择任何不常用的颜色)
            this.BackColor = Color.Magenta;
            this.TransparencyKey = Color.Magenta;
            this.msg = msg;
        }

        private void MyMessageBoxForm_Load(object sender, EventArgs e)
        {
            //要显示的消息
           Console.WriteLine(msg);
        }

       private void btn_ensure_Click(object sender, EventArgs e)
		{
   			if (null != TextHandler)
   			 {
    			//要返回的结果数据
      			String rsultData = "我是结果";
       			TextHandler.Invoke(rsultData);
      			DialogResult = DialogResult.OK;
 			}
		}

        private void btn_cancl_Click(object sender, EventArgs e)
        {
            TextHandler.Invoke(null);
            DialogResult = DialogResult.Cancel;
        }
    }
    public static class MyMessageBoxDialog
    {
        public static DialogResult Show(String message, out string strText)
        {
            string strTemp = string.Empty;

            MyMessageBoxForm inputDialog = new MyMessageBoxForm(message);
            inputDialog.TextHandler = (str) => { strTemp = str; };

            DialogResult result = inputDialog.ShowDialog();
            strText = strTemp;

            return result;
        }
    }
}

2.调用

复制代码
string result = string.Empty;
DialogResult dr = MyMessageBoxDialog.Show("aaaaa", out result);
if (dr == DialogResult.OK)
{
    Console.WriteLine("DialogResult"+ dr);
    Console.WriteLine("result = " + result);
}
else {
    Console.WriteLine("DialogResult" + dr);
}
相关推荐
mudtools1 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的2 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21882 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi2 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django