Winform 窗口圆角实现

      项目需要,得把Winform的窗口做成圆角,同时把标题栏等一堆东西去掉,做成如下的样子:

 

      首先,在新建Winform之后,在属性栏中, 把MaximizeBox和MinimizeBox 置为False,同时将ShowIcon置为False,FormBorderStyle置为None。之后,窗口就变成一块白板了。

      然后,PS出一张如上的背景图来,把图做成圆角。将改图设为Winform的背景,BackgroundImageLayout设为Stretch。

      网上有人说此后只需要将Winform的BackColor置为Color.Transparent就Ok了, 但是VS2010会报错,控件背景不允许使用透明色。

      所以只能学习网上有人提到的办法,实现Winform的Paint时间,将窗口重绘成圆角的。

       

ExpandedBlockStart.gif 窗口圆角
  1          private  void Main_Paint( object sender, PaintEventArgs e)
  2         {
  3             List<Point> list =  new List<Point>();
  4              int width =  this.Width;
  5              int height =  this.Height;
  6 
  7              #region 四个圆角
  8 
  9              // 左上  
 10              list.Add( new Point( 047));
 11             list.Add( new Point( 142));
 12             list.Add( new Point( 238));
 13             list.Add( new Point( 336));
 14             list.Add( new Point( 433));
 15             list.Add( new Point( 532));
 16             list.Add( new Point( 629));
 17             list.Add( new Point( 727));
 18             list.Add( new Point( 826));
 19             list.Add( new Point( 924));
 20             list.Add( new Point( 1022));
 21             list.Add( new Point( 1121));
 22             list.Add( new Point( 1220));
 23             list.Add( new Point( 1319));
 24             list.Add( new Point( 1417));
 25             list.Add( new Point( 1516));
 26             list.Add( new Point( 1615));
 27             list.Add( new Point( 1714));
 28             list.Add( new Point( 1913));
 29             list.Add( new Point( 2012));
 30             list.Add( new Point( 2111));
 31             list.Add( new Point( 2210));
 32             list.Add( new Point( 249));
 33             list.Add( new Point( 268));
 34             list.Add( new Point( 277));
 35             list.Add( new Point( 296));
 36             list.Add( new Point( 325));
 37             list.Add( new Point( 334));
 38             list.Add( new Point( 363));
 39             list.Add( new Point( 382));
 40             list.Add( new Point( 421));
 41             list.Add( new Point( 470));
 42 
 43              // 右上  
 44              list.Add( new Point(width -  470));
 45             list.Add( new Point(width -  421));
 46             list.Add( new Point(width -  382));
 47             list.Add( new Point(width -  363));
 48             list.Add( new Point(width -  334));
 49             list.Add( new Point(width -  325));
 50             list.Add( new Point(width -  296));
 51             list.Add( new Point(width -  277));
 52             list.Add( new Point(width -  268));
 53             list.Add( new Point(width -  249));
 54             list.Add( new Point(width -  2210));
 55             list.Add( new Point(width -  2111));
 56             list.Add( new Point(width -  2012));
 57             list.Add( new Point(width -  1913));
 58             list.Add( new Point(width -  1714));
 59             list.Add( new Point(width -  1615));
 60             list.Add( new Point(width -  1516));
 61             list.Add( new Point(width -  1417));
 62             list.Add( new Point(width -  1319));
 63             list.Add( new Point(width -  1220));
 64             list.Add( new Point(width -  1121));
 65             list.Add( new Point(width -  1022));
 66             list.Add( new Point(width -  924));
 67             list.Add( new Point(width -  826));
 68             list.Add( new Point(width -  727));
 69             list.Add( new Point(width -  629));
 70             list.Add( new Point(width -  532));
 71             list.Add( new Point(width -  433));
 72             list.Add( new Point(width -  336));
 73             list.Add( new Point(width -  238));
 74             list.Add( new Point(width -  142));
 75             list.Add( new Point(width -  047));
 76 
 77              // 右下  
 78              list.Add( new Point(width -  0, height -  47));
 79             list.Add( new Point(width -  1, height -  42));
 80             list.Add( new Point(width -  2, height -  38));
 81             list.Add( new Point(width -  3, height -  36));
 82             list.Add( new Point(width -  4, height -  33));
 83             list.Add( new Point(width -  5, height -  32));
 84             list.Add( new Point(width -  6, height -  29));
 85             list.Add( new Point(width -  7, height -  27));
 86             list.Add( new Point(width -  8, height -  26));
 87             list.Add( new Point(width -  9, height -  24));
 88             list.Add( new Point(width -  10, height -  22));
 89             list.Add( new Point(width -  11, height -  21));
 90             list.Add( new Point(width -  12, height -  20));
 91             list.Add( new Point(width -  13, height -  19));
 92             list.Add( new Point(width -  14, height -  17));
 93             list.Add( new Point(width -  15, height -  16));
 94             list.Add( new Point(width -  16, height -  15));
 95             list.Add( new Point(width -  17, height -  14));
 96             list.Add( new Point(width -  19, height -  13));
 97             list.Add( new Point(width -  20, height -  12));
 98             list.Add( new Point(width -  21, height -  11));
 99             list.Add( new Point(width -  22, height -  10));
100             list.Add( new Point(width -  24, height -  9));
101             list.Add( new Point(width -  26, height -  8));
102             list.Add( new Point(width -  27, height -  7));
103             list.Add( new Point(width -  29, height -  6));
104             list.Add( new Point(width -  32, height -  5));
105             list.Add( new Point(width -  33, height -  4));
106             list.Add( new Point(width -  36, height -  3));
107             list.Add( new Point(width -  38, height -  2));
108             list.Add( new Point(width -  42, height -  1));
109             list.Add( new Point(width -  47, height -  0));
110 
111              // 左下  
112              list.Add( new Point( 47, height -  0));
113             list.Add( new Point( 42, height -  1));
114             list.Add( new Point( 38, height -  2));
115             list.Add( new Point( 36, height -  3));
116             list.Add( new Point( 33, height -  4));
117             list.Add( new Point( 32, height -  5));
118             list.Add( new Point( 29, height -  6));
119             list.Add( new Point( 27, height -  7));
120             list.Add( new Point( 26, height -  8));
121             list.Add( new Point( 24, height -  9));
122             list.Add( new Point( 22, height -  10));
123             list.Add( new Point( 21, height -  11));
124             list.Add( new Point( 20, height -  12));
125             list.Add( new Point( 19, height -  13));
126             list.Add( new Point( 17, height -  14));
127             list.Add( new Point( 16, height -  15));
128             list.Add( new Point( 15, height -  16));
129             list.Add( new Point( 14, height -  17));
130             list.Add( new Point( 13, height -  19));
131             list.Add( new Point( 12, height -  20));
132             list.Add( new Point( 11, height -  21));
133             list.Add( new Point( 10, height -  22));
134             list.Add( new Point( 9, height -  24));
135             list.Add( new Point( 8, height -  26));
136             list.Add( new Point( 7, height -  27));
137             list.Add( new Point( 6, height -  29));
138             list.Add( new Point( 5, height -  32));
139             list.Add( new Point( 4, height -  33));
140             list.Add( new Point( 3, height -  36));
141             list.Add( new Point( 2, height -  38));
142             list.Add( new Point( 1, height -  42));
143             list.Add( new Point( 0, height -  47));
144 
145              #endregion
146 
147             Point[] points = list.ToArray();
148 
149             GraphicsPath shape =  new GraphicsPath();
150             shape.AddPolygon(points);
151 
152              this.Region =  new System.Drawing.Region(shape);
153 
154         }

 

       这样就把圆角窗口做出来了。需要说的是,这样设置出来的圆角是又毛刺的,精细程度跟你手动设置的点的个数有关,是个体力活。

       借鉴了别人的工作,致敬~

转载于:https://www.cnblogs.com/YFYkuner/archive/2012/01/06/2314921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public void SetBits() { //绘制绘层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Properties.Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("片必须是32位带Alhpa通道的片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值