RibbonContrl 画背景图片

来源:互联网 发布:北京数据恢复价格表 编辑:程序博客网 时间:2024/06/16 11:02
效果图如下:

具体代码如下:
 #region 设置标题栏背景图片        //注册事件         this.ribbonControl1.Paint += RibbonControl1_Paint;
        private void RibbonControl1_Paint(object sender, PaintEventArgs e)        {            Image img = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "image//welcome.png");            DrawRibbonLogo(ribbonControl1, img, 0, e.Graphics);        }        /// <summary>        /// RibbonControl 画背景图(如果图片高度太大,会不美观)        /// </summary>        /// <param name="ribbonCtrl">RibbonControl 控件</param>        /// <param name="img">图片</param>        /// <param name="marginRight">居右间距</param>        /// <param name="graphics">画布</param>        private void DrawRibbonLogo(RibbonControl ribbonCtrl, Image img, int marginRight, Graphics graphics)        {            RibbonViewInfo ribbonViewInfo = ribbonCtrl.ViewInfo;            //page            RibbonPanelViewInfo panelViewInfo = ribbonViewInfo.Panel;            //group            RibbonPageGroupViewInfoCollection groups = panelViewInfo.Groups;            if (ribbonViewInfo == null || panelViewInfo == null || groups == null || groups.Count == 0)                return;            //Rectangle            Rectangle bounds = panelViewInfo.Bounds;            //x轴坐标最小值            int minX = groups[groups.Count - 1].Bounds.Right;            //取最小值作为宽度            int width = Math.Min(bounds.Width - minX - 2, img.Width);            int height = Math.Min(bounds.Height, img.Height);            bounds.X = bounds.Width - width - marginRight;            int addHeight = (bounds.Height - height) / 2;            bounds.Y += addHeight - 2 > 0 ? addHeight - 2 : addHeight;            bounds.Width = width;            bounds.Height = height;            graphics.DrawImage(img, bounds.Location);        }        #endregion 设置标题栏背景图片

原创粉丝点击