65.9K
CodeProject 正在变化。 阅读更多。
Home

雕刻对话框

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.12/5 (17投票s)

2004 年 1 月 21 日

2分钟阅读

viewsIcon

27799

downloadIcon

455

一个顶部是雕刻状、标题栏是渐变填充的对话框类。

Sample Image - CarvedDialog.jpg

引言


本文指定以下区域
1) 使用图形路径类制作具有雕刻顶部的对话框
2) 如何使用渐变填充。
3) 从相应的资源文件中检索资源。
4) 如何移动FormBorderStyle属性设置为none的对话框
使用代码


雕刻

雕刻是使用Graphics path和Region类完成的

 

Rectangle rect = this.ClientRectangle;
使用 (GraphicsPath path = new GraphicsPath ())
 {
const int diameter = 25;
Rectangle arcRect = new Rectangle (rect.Location, new Size (diameter, diameter));
path.AddArc (arcRect, 180, 90);
arcRect.X = rect.Right - diameter;
path.AddArc (arcRect, 270, 90);
path.AddLine (rect.Width, rect.Height, rect.X, rect.Height);
path.CloseFigure ();
this.Region = new Region (path);
 }

 

Resource


资源管理器类实例是使用当前类型的资源管理器实例化的。
我使用资源编辑器将图像嵌入到.resx文件中。您可以使用类以编程方式执行此操作
用于写入资源文件

resManager = new ResourceManager(this.GetType());
imageClose = (Bitmap)resManager.GetObject("Image_Close");

我注意到的一个问题是,我使用资源编辑器添加了一个资源。在那之后,假设我要从
设计器视图进行更改,已添加的资源会自动删除


渐变填充

以下是用于生成渐变填充的代码片段。 我们在浮点数数组中的相应点混合颜色数组中指定的颜色。将此代码添加到paint事件的实现中

Brush = new LinearGradientBrush(new Point(0,0),new  Point(0,this.Height) Color.FromArgb(0xA3,0xD7,0xFF),Color.FromArgb(0x49,0x99,0xFF));  
System.Drawing.Drawing2D.ColorBlend clrBlend = new ColorBlend();
Color []clrs = { Color.FromArgb(0x49,0x99,0xFF),  Color.FromArgb (0xA3,0xD7,0xFF),Color.FromArgb(0x49,0x99,0xFF),
                    Color.FromArgb(0x49,0x99,0xFF),Color.FromArgb(0x49,0x99,0xFF)};
float []flts = { 0.0f,0.2f,0.5f,0.75f,1.0f } ;
clrBlend.Colors    = clrs;
clrBlend.Positions = flts;
Brush.InterpolationColors = clrBlend;
g.FillRectangle(Brush,0,0,this.Width,this.Height);


移动对话框


以下代码必须添加到鼠标按下事件中

if (e.Button == MouseButtons.Left)
{
 if(e.Button == MouseButtons.Left)
 {
  if((e.X > this.Width-25) && (e.X < (this.Width-25+imageClose.Width))
   && (e.Y > 2) && (e.Y < (2+imageClose.Height)))
  {
   this.Close();
  }
 }
 int xOffset = e.X;
 int yOffset = e.Y;
 isMouseDown = true;
 }         
}

 

以下代码必须添加到鼠标移动事件中

if (isMouseDown)
{
 Point mousePos = Control.MousePosition;
 mousePos.Offset(mouseOffset.X, mouseOffset.Y);
 Location = mousePos;
}


以下代码必须添加到鼠标弹起事件中

if (e.Button == MouseButtons.Left)
{
 isMouseDown = false;
}
  

 

© . All rights reserved.