Exemplo n.º 1
0
 /// <summary>
 /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮和图标,并且返回结果
 /// </summary>
 /// <param name="messageBoxText">一个 String,用于指定要显示的文本</param>
 /// <param name="caption">一个 String,用于指定要显示的标题栏标题</param>
 /// <param name="button">一个 MessageBoxButton 值,用于指定要显示哪个按钮或哪些按钮</param>
 /// <param name="icon">一个 CharmMessageBoxIcon 值,用于指定要显示的图标</param>
 /// <returns>一个 DialogResult 值,用于指定用户单击了哪个消息框按钮</returns>
 public DialogResult Show(
     string messageBoxText,
     string caption,
     MessageBoxButtons button,
     CharmMessageBoxIcon icon)
 {
     return(Show(messageBoxText, caption, button, icon, DialogResult.OK));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮和图标,并接受默认消息框结果和返回结果
        /// </summary>
        /// <param name="messageBoxText">一个 String,用于指定要显示的文本</param>
        /// <param name="caption">一个 String,用于指定要显示的标题栏标题</param>
        /// <param name="button">一个 MessageBoxButton 值,用于指定要显示哪个按钮或哪些按钮</param>
        /// <param name="icon">一个 CharmMessageBoxIcon 值,用于指定要显示的图标</param>
        /// <param name="defaultResult">一个 DialogResult 值,用于指定消息框的默认结果</param>
        /// <returns>一个 DialogResult 值,用于指定用户单击了哪个消息框按钮</returns>
        public DialogResult Show(
            string messageBoxText,
            string caption,
            MessageBoxButtons button,
            CharmMessageBoxIcon icon,
            DialogResult defaultResult)
        {
            #region 旧版的文本换行写法
            //using (Graphics g = Graphics.FromImage(base.BackgroundImage))
            //{
            //    byte[] data = Encoding.Default.GetBytes(text);
            //    int index = 0;
            //    byte[] cache;
            //    int minLength;

            //    while (data.Length >= (index + 1) * _textWidth)
            //    {
            //        cache = new byte[_textWidth];
            //        minLength = data.Length > (index + 1) * _textWidth ? (index + 1) * _textWidth : data.Length - 1;
            //        for (int i = index * _textWidth; i < minLength; i++)
            //        {
            //            cache[i - index * _textWidth] = data[i];
            //        }
            //        g.DrawString(Encoding.Default.GetString(cache), new Font("微软雅黑", 12), Brushes.Black,
            //            new Point(_textPoint.X, _textPoint.Y + index * 15));
            //        index++;
            //    }

            //    cache = new byte[_textWidth];
            //    minLength = data.Length > (index + 1) * _textWidth ? (index + 1) * _textWidth : data.Length - 1;
            //    for (int i = index * _textWidth; i <= minLength; i++)
            //    {
            //        cache[i - index * _textWidth] = data[i];
            //    }
            //    g.DrawString(Encoding.Default.GetString(cache), new Font("微软雅黑", 12), Brushes.Black,
            //        new Point(_textPoint.X, _textPoint.Y + index * 15));
            //}
            #endregion

            // 计算需要绘制的文本高度
            Graphics g  = CreateGraphics();
            SizeF    sf = g.MeasureString(messageBoxText, new Font("微软雅黑", 9), Width - 90);
            // 修改窗体高度
            Height = (int)sf.Height + 120;
            // 根据消息框类型设置背景图像资源并修改尺寸
            switch (MessageBoxType)
            {
            case MessageBoxType.BlueSky:
                BackgroundImage = Resources.messagebox_bluesky;
                BackgroundImage =
                    ImageOperation.ResizeImageWithoutBorder(BackgroundImage, 2, 30, 2, 2, Size);
                break;

            case MessageBoxType.Customize:
                if (mCustomizeBackgourndImage == null)      // 检查用户是否完成对消息框的初始化
                {
                    throw new ArgumentNullException(
                              "CharmControlLibrary.CharmMessageBox:未指定自定义消息框类型的背景图像资源.\n" +
                              "Name: " + Name);
                }
                BackgroundImage = new Bitmap(mCustomizeBackgourndImage, Size);
                break;
            }

            using (g = Graphics.FromImage(BackgroundImage))
            {
                // 绘制标题栏文本
                g.DrawString(caption, new Font("微软雅黑", 10, FontStyle.Bold), new SolidBrush(mTitleColor), new Point(8, 6));
                // 绘制消息框图标
                switch (icon)
                {
                case CharmMessageBoxIcon.None:
                    break;

                case CharmMessageBoxIcon.Question:
                    g.DrawImage(Resources.messageboxicon_question,
                                new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_question.Size));
                    break;

                case CharmMessageBoxIcon.Error:
                    g.DrawImage(Resources.messageboxicon_error,
                                new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_error.Size));
                    break;

                case CharmMessageBoxIcon.Infomation:
                    g.DrawImage(Resources.messageboxicon_info,
                                new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_info.Size));
                    break;

                case CharmMessageBoxIcon.Ok:
                    g.DrawImage(Resources.messageboxicon_ok,
                                new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_ok.Size));
                    break;

                case CharmMessageBoxIcon.Warning:
                    g.DrawImage(Resources.messageboxicon_warning,
                                new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_warning.Size));
                    break;
                }
                // 绘制消息文本
                RectangleF rf = new RectangleF(75, 60, sf.Width, sf.Height);
                g.DrawString(messageBoxText, new Font("微软雅黑", 9), new SolidBrush(mTextColor), rf);
            }

            // 设置按钮类型及坐标
            mButtonType = button;
            CharmControls[0].Location = new Point(89, (int)sf.Height + 85);
            CharmControls[1].Location = new Point(178, (int)sf.Height + 85);
            CharmControls[2].Location = new Point(265, (int)sf.Height + 85);
            // 设置检查框坐标
            CharmControls[3].Location = new Point(30, (int)sf.Height + 84);

            // 显示消息框
            ShowDialog();
            return(mDialogResult);
        }
        /// <summary>
        /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮和图标,并接受默认消息框结果和返回结果
        /// </summary>
        /// <param name="messageBoxText">一个 String,用于指定要显示的文本</param>
        /// <param name="caption">一个 String,用于指定要显示的标题栏标题</param>
        /// <param name="button">一个 MessageBoxButton 值,用于指定要显示哪个按钮或哪些按钮</param>
        /// <param name="icon">一个 CharmMessageBoxIcon 值,用于指定要显示的图标</param>
        /// <param name="defaultResult">一个 DialogResult 值,用于指定消息框的默认结果</param>
        /// <returns>一个 DialogResult 值,用于指定用户单击了哪个消息框按钮</returns>
        public DialogResult Show(
            string messageBoxText,
            string caption,
            MessageBoxButtons button,
            CharmMessageBoxIcon icon,
            DialogResult defaultResult)
        {
            #region 旧版的文本换行写法
            //using (Graphics g = Graphics.FromImage(base.BackgroundImage))
            //{
            //    byte[] data = Encoding.Default.GetBytes(text);
            //    int index = 0;
            //    byte[] cache;
            //    int minLength;

            //    while (data.Length >= (index + 1) * _textWidth)
            //    {
            //        cache = new byte[_textWidth];
            //        minLength = data.Length > (index + 1) * _textWidth ? (index + 1) * _textWidth : data.Length - 1;
            //        for (int i = index * _textWidth; i < minLength; i++)
            //        {
            //            cache[i - index * _textWidth] = data[i];
            //        }
            //        g.DrawString(Encoding.Default.GetString(cache), new Font("微软雅黑", 12), Brushes.Black,
            //            new Point(_textPoint.X, _textPoint.Y + index * 15));
            //        index++;
            //    }

            //    cache = new byte[_textWidth];
            //    minLength = data.Length > (index + 1) * _textWidth ? (index + 1) * _textWidth : data.Length - 1;
            //    for (int i = index * _textWidth; i <= minLength; i++)
            //    {
            //        cache[i - index * _textWidth] = data[i];
            //    }
            //    g.DrawString(Encoding.Default.GetString(cache), new Font("微软雅黑", 12), Brushes.Black,
            //        new Point(_textPoint.X, _textPoint.Y + index * 15));
            //}
            #endregion

            // 计算需要绘制的文本高度
            Graphics g = CreateGraphics();
            SizeF sf = g.MeasureString(messageBoxText, new Font("微软雅黑", 9), Width - 90);
            // 修改窗体高度
            Height = (int)sf.Height + 120;
            // 根据消息框类型设置背景图像资源并修改尺寸
            switch (MessageBoxType)
            {
                case MessageBoxType.BlueSky:
                    BackgroundImage = Resources.messagebox_bluesky;
                    BackgroundImage =
                        ImageOperation.ResizeImageWithoutBorder(BackgroundImage, 2, 30, 2, 2, Size);
                    break;
                case MessageBoxType.Customize:
                    if (mCustomizeBackgourndImage == null)  // 检查用户是否完成对消息框的初始化
                        throw new ArgumentNullException(
                            "CharmControlLibrary.CharmMessageBox:未指定自定义消息框类型的背景图像资源.\n" +
                            "Name: " + Name);
                    BackgroundImage = new Bitmap(mCustomizeBackgourndImage, Size);
                    break;
            }

            using (g = Graphics.FromImage(BackgroundImage))
            {
                // 绘制标题栏文本
                g.DrawString(caption, new Font("微软雅黑", 10, FontStyle.Bold), new SolidBrush(mTitleColor), new Point(8, 6));
                // 绘制消息框图标
                switch (icon)
                {
                    case CharmMessageBoxIcon.None:
                        break;
                    case CharmMessageBoxIcon.Question:
                        g.DrawImage(Resources.messageboxicon_question,
                            new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_question.Size));
                        break;
                    case CharmMessageBoxIcon.Error:
                        g.DrawImage(Resources.messageboxicon_error,
                            new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_error.Size));
                        break;
                    case CharmMessageBoxIcon.Infomation:
                        g.DrawImage(Resources.messageboxicon_info,
                            new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_info.Size));
                        break;
                    case CharmMessageBoxIcon.Ok:
                        g.DrawImage(Resources.messageboxicon_ok,
                            new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_ok.Size));
                        break;
                    case CharmMessageBoxIcon.Warning:
                        g.DrawImage(Resources.messageboxicon_warning,
                            new Rectangle(new Point(27, (int)sf.Height / 2 + 50), Resources.messageboxicon_warning.Size));
                        break;
                }
                // 绘制消息文本
                RectangleF rf = new RectangleF(75, 60, sf.Width, sf.Height);
                g.DrawString(messageBoxText, new Font("微软雅黑", 9), new SolidBrush(mTextColor), rf);
            }

            // 设置按钮类型及坐标
            mButtonType = button;
            CharmControls[0].Location = new Point(89, (int)sf.Height + 85);
            CharmControls[1].Location = new Point(178, (int)sf.Height + 85);
            CharmControls[2].Location = new Point(265, (int)sf.Height + 85);
            // 设置检查框坐标
            CharmControls[3].Location = new Point(30, (int)sf.Height + 84);

            // 显示消息框
            ShowDialog();
            return mDialogResult;
        }
 /// <summary>
 /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮和图标,并且返回结果
 /// </summary>
 /// <param name="messageBoxText">一个 String,用于指定要显示的文本</param>
 /// <param name="caption">一个 String,用于指定要显示的标题栏标题</param>
 /// <param name="button">一个 MessageBoxButton 值,用于指定要显示哪个按钮或哪些按钮</param>
 /// <param name="icon">一个 CharmMessageBoxIcon 值,用于指定要显示的图标</param>
 /// <returns>一个 DialogResult 值,用于指定用户单击了哪个消息框按钮</returns>
 public DialogResult Show(
     string messageBoxText,
     string caption,
     MessageBoxButtons button,
     CharmMessageBoxIcon icon)
 {
     return Show(messageBoxText, caption, button, icon, DialogResult.OK);
 }