首页 > 开发 > 综合 > 正文

C# WinForm 中在窗口标题栏上加按钮

2024-07-21 02:17:33
字体:
来源:转载
供稿:网友


在窗口标题栏上加按钮本来不是什么新鲜事了,我在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, 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转换为.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 = 6;            m_rect.width = m_rect.height = 20;        }    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表