C# WinForm 中在視窗標題列上加按鈕)

來源:互聯網
上載者:User
作者:袁曉輝
首頁:http://www.farproc.com
BLOGhttp://blog.csdn.net/uoyevoli/

    在視窗標題列上加按鈕本來不是什麼新鮮事了,我在VC++下早也實現過了(相信很多人也都實現過了)。今天一個朋友問我C# WinForm下可否實現,我就順便拿C#寫了一個。
    原理是一樣的,都是重寫視窗過程(WndProc),處理一些非客戶區訊息(WM_NCxxxx),可以說本來沒有什麼新意,可是從寫這個程式的過程中,我也學到了兩個技巧:
    1)、C#中重寫視窗過程不用再調用SetWindowLong API了,直接overide一個WndProc就可以了。
    2)、Windows API中的HDC可以通過Graphics.FromHdc()轉換為(建立出)System.Drawing.Graphics,然後就可以用.NET Framework (GID+??)提供的繪圖功能方便地進行畫圖了。終於可以拋開討厭的GDI API了(說實在話,在C#中調用Windows API真的太麻煩了:)).
代碼如下:using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsApplication2
{
    /// <summary>
    /// Form1 的摘要說明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 表單設計器支援所必需的
            //
            InitializeComponent();
            //
            // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
            //
        }

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows 表單設計器產生的程式碼

        /// <summary>
        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
        }
        #endregion

        /// <summary>
        /// 應用程式的主進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

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

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

        [DllImport("Kernel32.dll")]
        private static extern int GetLastError();

        //標題列按鈕的矩形地區。
        Rectangle m_rect = new Rectangle(205, 4, 16, 16);

        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);
                        gs.FillRectangle(new LinearGradientBrush(m_rect, Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal), m_rect);
                        StringFormat strFmt = new StringFormat();
                        strFmt.Alignment = StringAlignment.Center;
                        strFmt.LineAlignment = StringAlignment.Center;
                        gs.DrawString("√", this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);
                        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;
                    }
            }
        }

        //在視窗大小改變時及時更新按鈕的地區。
        private void Form1_SizeChanged(object sender, System.EventArgs e)
        {
            m_rect.X = this.Bounds.Width - 95;
            m_rect.Y = 4;
            m_rect.Width = m_rect.Height = 16;
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.