在窗体标题栏上模拟关闭按钮.

  [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();
        protected override void WndProc(ref Message m)
        {
            //标题栏按钮的矩形区域。
            int iCloseX = this.Width - 28;
            Rectangle m_rect = new Rectangle(iCloseX, 6, 20, 20);

            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.DrawImage(pictureBox4.Image, m_rect);
                        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))
                        {
                            try
                            {
                                DevExpress.XtraBars.BarItemLink link = barSubItemExit.Links[0];
                                DevExpress.XtraBars.ItemClickEventArgs e = new DevExpress.XtraBars.ItemClickEventArgs(barSubItemExit, link);
                                barSubItemExit_ItemClick(new object(), e);
                            }
                            catch
                            { }
                        }
                        break;
                    }
            }
        }

你可能感兴趣的:(.net,object,user,X86)