Control Study -> 用获取路径方法得到圆形窗体(如将: PictureBox变成圆形)

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

(一).功能

有时候由于显示效果,需要将某个控件变一下形状.

本文举例将PictureBox[]数组变成圆形.

(二).代码

(这里说明一下,我个人将它变形是因为我用PictureBox表示象棋棋子,

由于PictureBox默认是方 的, 所以得把它变成圆的)

大家可以将这个功能作为其它用途使用.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace 智能象棋游戏

{

/// <summary>

/// 功能:将所有picturebox控件变为圆形

/// 特点:使旗子移动出现延迟,看起来没有以前效果好,因此本程序为了保证旗子走动平滑,没有采用

/// 难度:1 所用时间:1个小时

/// </summary>

public class Class7

{

[System.Runtime.InteropServices.DllImport("gdi32")]

private static extern IntPtr BeginPath(IntPtr hdc);

[System.Runtime.InteropServices.DllImport("gdi32")]

private static extern int SetBkMode(IntPtr hdc,int nBkMode);

const int TRANSPARENT=1;

[System.Runtime.InteropServices.DllImport("gdi32")]

private static extern IntPtr EndPath(IntPtr hdc);

[System.Runtime.InteropServices.DllImport("gdi32")]

private static extern IntPtr PathToRegion(IntPtr hdc);

[System.Runtime.InteropServices.DllImport("gdi32")]

private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2);

[System.Runtime.InteropServices.DllImport("user32")]

private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);

[System.Runtime.InteropServices.DllImport("user32")]

private static extern IntPtr GetDC(IntPtr hwnd);

public Class7()

{

}

public void MakeToPictureBoxsToCircle(PictureBox[] pb)

{

IntPtr dc;

IntPtr region;

for(int i=0;i<pb.Length;i++)

{

dc=GetDC(pb[i].Handle);

BeginPath(dc);

SetBkMode(dc,TRANSPARENT);

Ellipse(dc,0,0,pb[i].Width-3,pb[i].Height-2);

EndPath(dc);

region=PathToRegion(dc);

SetWindowRgn(pb[i].Handle,region,false);

}

}

}

}

谢谢阅读!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航