示例#1
0
        /// <summary>
        /// 引发 Paint 事件
        /// </summary>
        /// <param name="e">包含事件数据的 PaintEventArgs</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            // 获取绘制对象
            Graphics g = e.Graphics;

            // 过程中需要用到的变量
            MenuItem menuItem;
            int      formHeight = 0;

            // 轮询每个菜单项,确定窗体需要的长度和宽度
            for (int i = 0; i < mItems.Count; i++)
            {
                menuItem = mItems[i];
                // 判断菜单项类型
                if (menuItem.MenuItemType == MenuItemType.Spliter)
                {
                    // 分割线
                    // 判断当前菜单类型是否支持分割线
                    if (mMenuType != MenuType.QQ)
                    {
                        throw new ArgumentException(
                                  "CharmControlLibrary.CharmMenu:当前菜单类型不支持分割线.\n" +
                                  "Name: " + Name);
                    }
                    // 绘制分割线
                    g.DrawImage(Resources.menu_spliter,
                                new Rectangle(new Point(32, formHeight + 1), new Size(Width - 39, 2)));
                    formHeight += 3;
                }
                else
                {
                    // 文本项
                    // 判断是否为选择项,是则需要绘制背景
                    if (mSelectedIndex == i)
                    {
                        Bitmap imgTemp = Resources.mun_select_bkg;
                        imgTemp = ImageOperation.ResizeImageWithoutBorder(
                            imgTemp, 3, 1, 3, 1, new Size(Width - 2, 21));
                        g.DrawImage(imgTemp, new Rectangle(3, formHeight + 2, Width - 5, 21));
                        // 绘制菜单项文本
                        g.DrawString(menuItem.Text, Font, Brushes.White, new Point(32, formHeight + 3));
                    }
                    else
                    {
                        g.DrawString(menuItem.Text, Font, Brushes.Black, new Point(32, formHeight + 3));
                    }

                    // 判断是否需要绘制图标
                    if (menuItem.Icon != null)
                    {
                        g.DrawImage(menuItem.Icon,
                                    new Rectangle(new Point(14 - menuItem.Icon.Width / 2, formHeight + 12 - menuItem.Icon.Height / 2),
                                                  menuItem.Icon.Size));
                    }

                    formHeight += 22;
                }
            }
        }
示例#2
0
        /// <summary>
        /// 初始化 CharmTextBox 类的新实例
        /// </summary>
        public CharmTextBox()
            : base()
        {
            // * 初始化属性 *
            Size             = new Size(160, 26);
            BackColor        = Color.White;
            mStatusImages    = new Image[2];
            mStatusImages[0] = Resources.textbox_bkg_normal;
            // 去除常态背景空白部分
            mStatusImages[0] =
                ImageOperation.GetPartOfImage(mStatusImages[0], mStatusImages[0].Width, mStatusImages[0].Height - 2, 0, 1);
            mStatusImages[1] = Resources.textbox_bkg_hover;
            // 重新设置文本框大小
            mStatusImages[0] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[0], 2, 1, 2, 1, base.Size);
            mStatusImages[1] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[1], 2, 2, 2, 2, base.Size);

            // * 文本框外观样式设置 *
            mTextbox             = new TextBox();
            mTextbox.BorderStyle = BorderStyle.None;                             // 无边框
            mTextbox.Font        = new Font("微软雅黑", 9.75F);                      // 字体
            mTextbox.Size        = new Size(156, 20);                            // 大小
            mTextbox.Location    = new Point(2, (Height - mTextbox.Height) / 2); // 位置
            mTextbox.ImeMode     = ImeMode.NoControl;                            // 输入法设置
            // 设置水印
            if (mWatermark != null)
            {
                mTextbox.ForeColor = Color.DarkGray;
                mTextbox.Text      = mWatermark;
            }

            // * 控件属性设置 *
            mInputMode = InputMode.Normal;   // 输入模式

            // * 关联控件事件 *
            mTextbox.MouseEnter  += new EventHandler(TextBox_MouseEnter);
            mTextbox.MouseLeave  += new EventHandler(TextBox_MouseLeave);
            mTextbox.KeyPress    += new KeyPressEventHandler(TextBox_KeyPress);
            mTextbox.LostFocus   += new EventHandler(TextBox_LostFocus);
            mTextbox.GotFocus    += new EventHandler(TextBox_GotFocus);
            mTextbox.DoubleClick += new EventHandler(mTextbox_DoubleClick);
            mTextbox.TextChanged += new EventHandler(mTextbox_TextChanged);

            // * 加载控件到控件集合中 *
            Controls.Add(mTextbox);
        }
示例#3
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);
        }
示例#4
0
        // 窗体可见性改变事件,用于重新设置窗体属性
        private void CharmMenu_VisibleChanged(object sender, EventArgs e)
        {
            // 判断窗体是否变为可见
            if (Visible)
            {
                // 窗体长度及宽度
                int formWidth  = 0;
                int formHeight = 0;

                // 根据菜单项数组长度决定窗体属性
                Graphics g = CreateGraphics();
                SizeF    sf;
                MenuItem menuItem;

                // 轮询每个菜单项,确定窗体需要的长度和宽度
                for (int i = 0; i < mItems.Count; i++)
                {
                    menuItem = mItems[i];
                    // 判断菜单项类型
                    if (menuItem.MenuItemType == MenuItemType.Spliter)
                    {
                        // 判断当前菜单类型是否支持分割线
                        if (mMenuType != MenuType.QQ)
                        {
                            throw new ArgumentException(
                                      "CharmControlLibrary.CharmMenu:当前菜单类型不支持分割线.\n" +
                                      "Name: " + Name);
                        }
                        formHeight += 3;    // 分割线
                    }
                    else
                    {
                        // 文本项
                        formHeight += 22;
                        // 计算绘制文本所需的像素
                        sf = g.MeasureString(menuItem.Text, Font);
                        // 判断当前项文本所需像素是否大于之前项
                        if ((int)sf.Width > formWidth)
                        {
                            formWidth = (int)sf.Width;
                        }
                    }
                }

                Bitmap imgTemp = null;
                // 设置窗体长度及宽度
                Width  = formWidth + 28 + 18;
                Height = formHeight + 4;

                // 根据菜单类型设置窗体背景
                switch (mMenuType)
                {
                case MenuType.QQ:
                    imgTemp = new Bitmap(Resources.menu_bkg_qq);
                    imgTemp = ImageOperation.ResizeImageWithoutBorder(
                        imgTemp, 29, 2, 2, 2, new Size(Width, Height));
                    break;

                case MenuType.QQSoftMgr:
                    imgTemp = new Bitmap(Resources.menu_bkg_qqsoftmgr);
                    imgTemp = ImageOperation.ResizeImageWithoutBorder(
                        imgTemp, 29, 2, 2, 2, new Size(Width, Height));
                    break;
                }
                BackgroundImage = new Bitmap(imgTemp);

                // 释放系统资源
                g.Dispose();
                imgTemp.Dispose();

                // 设置窗体显示的位置
                Rectangle rect = Screen.GetWorkingArea(this);
                // 判断是否有足够空间在光标右侧弹出菜单
                if ((rect.Width - MousePosition.X) > Width)
                {
                    Left = MousePosition.X;     // 有足够空间
                }
                else
                {
                    Left = MousePosition.X - Width;   // 没有足够空间
                }
                // 判断是否有足够空间在光标下方弹出菜单
                if ((rect.Height - MousePosition.Y) > Height)
                {
                    Top = MousePosition.Y;     // 有足够空间
                }
                else
                {
                    Top = MousePosition.Y - Height;   // 没有足够空间
                }
            }
        }