国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C# WinForm 中在窗口標題欄上加按鈕

2024-07-21 02:17:33
字體:
供稿:網(wǎng)友


在窗口標題欄上加按鈕本來不是什么新鮮事了,我在vc++下早也實現(xiàn)過了(相信很多人也都實現(xiàn)過了)。今天一個朋友問我c# winform下可否實現(xiàn),我就順便拿c#寫了一個。
   原理是一樣的,都是重寫窗口過程(wndproc),處理一些非客戶區(qū)消息(wm_ncxxxx),可以說本來沒有什么新意,可是從寫這個程序的過程中,我也學(xué)到了兩個技巧:
    1)、c#中重寫窗口過程不用再調(diào)用setwindowlong api了,直接overide一個wndproc就可以了。
    2)、windows api中的hdc可以通過graphics.fromhdc()轉(zhuǎn)換為(創(chuàng)建出)system.drawing.graphics,然后就可以用.net framework (gid+??)提供的繪圖功能方便地進行畫圖了。終于可以拋開討厭的gdi api了(說實在話,在c#中調(diào)用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>        /// 必需的設(shè)計器變量。        /// </summary>        private system.componentmodel.container components = null;         public form1()        {            //            // windows 窗體設(shè)計器支持所必需的            //            initializecomponent();            //            // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼            //        }        /// <summary>        /// 清理所有正在使用的資源。        /// </summary>        protected override void dispose( bool disposing )        {            if( disposing )            {                if (components != null)                 {                    components.dispose();                }            }            base.dispose( disposing );        }        #region windows 窗體設(shè)計器生成的代碼        /// <summary>        /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改        /// 此方法的內(nèi)容。        /// </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>        /// 應(yīng)用程序的主入口點。        /// </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();            //標題欄按鈕的矩形區(qū)域。        rectangle m_rect = new rectangle(205, 6, 20, 20);        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轉(zhuǎn)換為.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;                }            }        }        //在窗口大小改變時及時更新按鈕的區(qū)域。        private void form1_sizechanged(object sender, system.eventargs e)        {            m_rect.x = this.bounds.width - 95;            m_rect.y = 6;            m_rect.width = m_rect.height = 20;        }    }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿克苏市| 界首市| 松桃| 腾冲县| 沂源县| 公安县| 丰镇市| 镇远县| 广西| 广州市| 柳林县| 永定县| 湘潭市| 高唐县| 册亨县| 泽库县| 宽甸| 安乡县| 齐齐哈尔市| 绥江县| 邵阳市| 铁岭市| 东光县| 曲麻莱县| 宝兴县| 南汇区| 平舆县| 寿阳县| 手游| 昌都县| 南投县| 明光市| 新田县| 绥化市| 衢州市| 临邑县| 惠东县| 岗巴县| 陈巴尔虎旗| 兰州市| 榆林市|