谷歌谈话风格的Windows窗体

本文介绍如何使用自定义绘制事件处理程序来实现Windows控件的个性化设计,重点讲解GoogleTalkForm类的实现细节,包括双缓冲绘图、鼠标事件处理及系统上下文菜单项的绘制。

下载demo project - 29.1 Kb
下载source - 12.5 Kb

介绍
本文主要解释如何使用自定义绘制事件处理程序来绘制自己的Windows控件(在本例中为窗体控件)。GoogleTalkForm类继承和扩展System.Windows.Forms。表单控件(由Microsoft . net框架提供),用于提供谷歌Talk Windows窗体的外观和感觉。
类属性是windowsnappable, IsResizable, ResizableColor, TitleColor, TitleFont, TitleBackColor, TitleForeColor, TitleStyle, BodyBackColor, BodyStyle, OutlineColor, OutlineSize, IconsNormalColor, IconsHiLiteColor, MinimumHeight,和MinimumWidth可以用来改变表单的标准外观和感觉,以及行为。
GoogleTalkForm类提供:

绘制窗口窗体。
绘图使用双缓冲。
窗体快照到特定的客户端桌面区域。
鼠标事件处理。
绘制系统上下文菜单项。
它是如何工作的
首先,我们需要覆盖表单事件,如OnPaint, OnMouseDown, OnMouseUp, OnMouseMove, OnDoubleClick来处理表单绘制事件和鼠标事件。隐藏,复制Codeprotected override void OnPaint(PaintEventArgs e)
{

}

protected override void OnMouseDown(MouseEventArgs e)
{

}
表单的样式必须使用GoogleTalkForm类构造函数中的SetStyle方法来设置,以反映所需的行为。隐藏,复制Codethis.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.StandardClick, true);
this.SetStyle(ControlStyles.StandardDoubleClick, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, false);
this.UpdateStyles();
isMousePointerInArea方法用于检查当前鼠标位置是否在特定的客户端矩形区域内。必须计算出鼠标相对于Windows窗体的位置,因为控件。鼠标位置返回鼠标相对于桌面的位置。隐藏,复制Codeprivate bool isMousePointerInArea(Point mousePosition, Rectangle area)
{
Point relativePoint = new Point(0, 0);
relativePoint.X = mousePosition.X - this.Location.X;
relativePoint.Y = mousePosition.Y - this.Location.Y;

return area.Contains(relativePoint);

}
自定义的窗体绘制都是通过方法OnFormPaint在一个新创建的具有窗体的精确宽度和高度的位图对象上完成的。从位图创建图形对象,并使用DrawString、DrawImage、DrawLine和DrawArc等方法。一旦所有的绘制完成,缓冲的位图就会被复制到窗体图形实例中,使用drawimageunscaling方法。隐藏,复制Code// Create a new Pen object
p = new Pen(this.OutlineColor, this.OutlineSize);

// Draw the form outline
g.DrawArc(p, rectLeftCorner, 180, 90);
g.DrawArc(p, rectRightCorner, 270, 90);
g.DrawLine(p, edgeRadius, 0, this.Width - edgeRadius, 0);
g.DrawLine(p, 0, edgeRadius, 0, this.Height);
g.DrawLine(p, this.Width - 1, edgeRadius,
this.Width - 1, this.Height);
g.DrawLine(p, 0, this.Height - 1,
this.Width, this.Height - 1);

// Dispose the Pen object
p.Dispose();
p = null;
创建一个自定义区域并应用于窗体,以产生圆角边缘效果。要创建所需的区域,我们必须逐像素迭代,并在当前选择的像素与透明颜色不相同时向GraphicsPath对象(将用于创建区域对象)添加一个1x1的矩形。为了优化代码,只检查窗体的顶部角是否有透明的彩色像素。隐藏,收缩,复制Code// Create GraphicsPath to be used to crop the region required
gpRegion = new GraphicsPath();

// Loop through every pixel in the top left corner.
// Create a 1 x 1 rectangle regions of pixels
// that do not match the transparent color
for (int x = rectLeftCorner.X; x < rectLeftCorner.Width; x++)
{
for (int y = rectLeftCorner.Y; y < rectLeftCorner.Height / 2; y++)
{
if (isSameColor(bmp.GetPixel(x, y),
this.transparentColor) == false)
{
gpRegion.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}

// Loop through every pixel in the top right corner.
// Create a 1 x 1 rectangle regions of pixels
// that do not match the transparent color
for (int x = rectRightCorner.X + 1; x <
rectRightCorner.X +
rectRightCorner.Width + 1; x++)
{
for (int y = rectRightCorner.Y; y <
rectRightCorner.Y + rectRightCorner.Height / 2; y++)
{
if (isSameColor(bmp.GetPixel(x, y),
this.transparentColor) == false)
{
gpRegion.AddRectangle(new Rectangle(x, y, 1, 1));
}
}
}

// Create the remaining rectangular regions
// to complete cover all the windows form area
gpRegion.AddRectangle(new Rectangle(rectLeftCorner.Width, 0,
this.Width - (edgeRadius * 4), rectLeftCorner.Height / 2));
gpRegion.AddRectangle(new Rectangle(0,
rectLeftCorner.Height / 2, this.Width, this.Height));

// Apply region
this.Region = new Region(gpRegion);
历史

第一个版本(2006年5月9日)。
1.1 -(二零零六年五月十一日)

创建窗体设计器以控制窗体(和子控件)行为。
修正了托管在MDI容器中的表单行为。

本文转载于:http://www.diyabc.com/frontweb/news6810.html

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值