![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
1>新建windows应用程序
2>选中新建的窗体,设置其相应属性:
(1)。将 FormBorderStyle 属性设置为 None。
(2)。将窗体的 BackgroundImage 属性设置为先前创建的位图文件。不必将文件添加到项目系统中;这将在指定该文件作为背景图像时自动完成。
(3)。将 TransparencyKey 属性设置为位图文件的背景色,本例中为黄色。(此属性告诉应用程序窗体中的哪些部分需要设置为透明。 )
这时你就可以按F5测试你的程序,可以看到如图所示的窗体。现在窗体还不能拖动,只能通过结束程序,或者alt+F4关闭。下面我们编写相应的代码来实现标题栏的相应功能。
3.编写窗体相关代码
(要实现窗口的关闭,移动等操作)
(1)。实现窗口关闭
从工具栏中拖进一个按钮,设置其按钮文字为“×”,设置其大小为合适大小。双击该按钮进入其触发时间函数。
写入如下代码:
this.Close(); //关闭本窗体
(2)。设置窗体的移动操作,我们要用到两个全局的变量
private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下
创建该窗体 MouseDown事件的相应处理程序。
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
创建该窗体的 MouseMove事件的相应处理程序
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
创建该窗体的MouseUp事件的相应处理程序
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
(3)。加入相应的其他的控件
其他的就是看你自己的需要,来添加控件,实现自己想要实现的功能。本例中添加了一文本框,设置其背景为黄色,所以显示时也成了透明的。别忘了把事件绑定到窗体 !!!
现在,我们就可以生成程序,看一下最后的效果了。
由于 时间的 关系,我就只贴代码吧:
public GraphicsPath SetAlltypeWindow(Control form,string regionBMP,Color transparentColor)
{
GraphicsPath path = new GraphicsPath();
if(File.Exists(regionBMP))
{
Bitmap bitmap = new Bitmap(regionBMP);
form.BackgroundImage = bitmap;
form.Width = bitmap.Width;
form.Height = bitmap.Height;
for(int x=0;x<bitmap.Width;x++)
{
for(int y=0;y<bitmap.Height;y++)
{
if(bitmap.GetPixel(x,y)!=transparentColor)
{
path.AddRectangle(new Rectangle(x,y,1,1));
}
}
}
}
return path;
}
string regionBMP = System.Configuration.ConfigurationSettings.AppSettings["bitmap"];
int transparentColorR = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["transparentColorR"]);
int transparentColorG = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["transparentColorG"]);
int transparentColorB = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["transparentColorB"]);
Color transparentColor = Color.FromArgb(transparentColorR,transparentColorG,transparentColorB);
alltypeWindow alltype = new alltypeWindow();
GraphicsPath path = alltype.SetAlltypeWindow(this,regionBMP,transparentColor);
this.Region = new Region(path);
exe.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- 此处显示用户应用程序和配置的属性设置。-->
<!-- 示例:<add key="settingName" value="settingValue"/> -->
<add key="index.ShowInTaskbar" value="False" />
<add key="index.TopMost" value="False" />
<add key="bitmap" value="异型窗体.gif" />
<add key="transparentColorR" value="255" />
<add key="transparentColorG" value="255" />
<add key="transparentColorB" value="255" />
</appSettings>
</configuration>
参考资料:http://elovenana.spaces.live.com/blog/cns!3816F973ACB0280A!153.entry
参考资料:http://hi.baidu.com/wx_weas/blog/item/20c074c650d6a5dbd0006067.html
参考资料: http://www.baidu.com/s?wd=C%23%BB%E6%D6%C6%B2%BB%B9%E6%D4%F2%B4%B0%CC%E5
{
GraphicsPath FormPath = new GraphicsPath();
FormPath.AddLine(5, 230, 5, 200);
FormPath.AddLine(5, 200, 95, 200);
FormPath.AddLine(95, 200, 95, 30);
FormPath.AddLine(95, 30, 250, 30);
FormPath.AddLine(250, 30, 250, 200);
FormPath.AddLine(250, 200, 340, 200);
FormPath.AddLine(340, 200, 340, 230);
FormPath.AddLine(340, 230, 5, 230);
//闭合当前图形并开始新的图形
FormPath.CloseFigure();
this.Region = new Region(FormPath);
}
画圆角一样的
用这个FormPath.AddArc()//向当前图形追加一段椭圆弧
具体坐标懒得试了,你自己试试,不懂查MSDN,或再问