winform无标题窗体点击任务栏最小化、还原功能

做winform开发的时候,常常为了美化界面,把窗体的FormBorderStyle属性设置为None,即所谓的无标题窗体,这个时候,在任务栏鼠标右击是没有菜单的,那么如何吧默认的系统菜单还原回来呢?

第一步,引用命名空间 

using System.Windows.Forms;
using System.Runtime.InteropServices;

第二步,把以下代码添加到Form的类中

#region 右击菜单
        [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
        public static extern int GetWindowLong(HandleRef hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

        const int WS_CLIPCHILDREN = 0x2000000;
        const int WS_MINIMIZEBOX = 0x20000;
        const int WS_MAXIMIZEBOX = 0x10000;
        const int WS_SYSMENU = 0x80000;
        const int CS_DBLCLKS = 0x8;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;

                cp.Style = WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU;
                cp.ClassStyle = CS_DBLCLKS;

                return cp;
            }
        }


        #endregion

 OK。

posted @ 2012-05-23 15:03  吸血程序猿  阅读(417)  评论(0编辑  收藏  举报