Winform C#標題列自訂樣式

來源:互聯網
上載者:User

#region 改變視窗樣式

        #if !DEBUG

        [DllImport ("User32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);

        [DllImport ("User32.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        protected override void WndProc(ref Message m)

        ...{
   
                base.WndProc(ref m);
        
                switch(m.Msg)

       
                ...{

                    case 0x86://WM_NCACTIVATE

                        goto case 0x85;

                    case 0x85://WM_NCPAINT
                    ...{
                       
         
                        IntPtr hDC = GetWindowDC(m.HWnd);
    
                        //把DC轉換為.NET的Graphics就可以很方便地使用Framework提供的繪圖功能了
                        Graphics gs = Graphics.FromHdc(hDC);

                        處理色彩#region 處理色彩

                        int ibox = 1;

                        if (this.MaximizeBox) ibox ++;
                        if (this.MinimizeBox) ibox ++;

                        //得到相關背景圖片
                        Image imgpm = Image.FromFile( Application.StartupPath+ @"Main.bmp");

                    
                       
                      //gs.DrawImage(imgbg,3,0,this.Width - (ibox * iBoxWidh),SystemInformation.CaptionHeight + 2);    //顯示背景圖片

                        //標題列顯示背景顏色
                        int xPos    = this.Width    -    iBoxWidh * ibox - 2 - ibox*3;

                        Rectangle excludeRect;
                        //標題列中不需要填充的位置
                        for(int i=0;i<ibox;i++)
                        ...{
                            if( i>0)
                            ...{
                                xPos = xPos + iBoxWidh + 2;
                            }
                            
                              excludeRect = new Rectangle(xPos, 5, iBoxWidh, iBoxWidh+1);
                            gs.ExcludeClip(excludeRect);
                        }
                       
                   
                        //背景地區
                        Rectangle rBackground = new Rectangle(0,0,this.Width,SystemInformation.CaptionHeight+3);
                        //背景顏色 上下漸層
                        System.Drawing.Drawing2D.LinearGradientBrush bBackground
                            = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, Color.FromArgb(100,145,175), Color.FromArgb(45,80,120),LinearGradientMode.Vertical);

                        //標題列填充背景顏色
                        gs.FillRectangle(bBackground,rBackground );
                        //顯示表徵圖檔案
                      gs.DrawImage(imgpm,4,4,24,24);   

                        #endregion

                        StringFormat strFmt = new StringFormat();

                        //strFmt.Alignment = StringAlignment.Center;
                        //strFmt.LineAlignment = StringAlignment.Center;

                        //gs.DrawString(this.Text, this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);
                        //設定標題字型
                        Font drawFont = new Font("宋體", 10,System.Drawing.FontStyle.Bold);
                        //設定標題顏色
                        SolidBrush drawBrush = new SolidBrush(Color.White);
                    
                        //重畫標題
                        gs.DrawString(this.Text, drawFont, drawBrush, 30,8);

                        gs.Dispose();

                        //釋放GDI資源

                        ReleaseDC(m.HWnd, hDC);
                        
                        break;

                    }

                    case 0xA1://WM_NCLBUTTONDOWN
                    ...{

                        Point mousePoint = new Point((int)m.LParam);

                        mousePoint.Offset(-this.Left, -this.Top);

                        //                    if(m_rect.Contains(mousePoint))
                        //                    {
                        //
                        //                        MessageBox.Show("hello");
                        //
                        //                    }

                        break;

                    }
    
                }

            }
        
        #endif

        #endregion

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.