DIY自己的GIS程序(2)——局部刷新
- 绘制线过移动鼠标程中绘制临时线段防闪烁
参考OpenS-CAD想实现绘制线的功能。希望实现绘制线的过程,在移动线的时候没有闪烁和花屏。但是出现了问题,困扰了2天,前天熬的太晚,搞得现在精力都没有恢复。现在终于把问题搞清楚了:
第一个问题:没有给背景图片赋颜色,此时相当于透明。所以每次将图片局部范围重绘产生严重的花屏,绘制的线段残留在屏幕上,将透明的东西绘制是不可能将移动过程中产生的临时线段擦除的。
第二个问题:用当前移动产生的线段包围盒来重绘,其实在本次鼠标移动过程中,应该将上次产生的残留擦除,用上次的包围盒局部重绘。
以下是自己写的一个控件的雏形:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using RGeos.Geometry;
using System.Drawing.Imaging; namespace RGeos.PluginEngine
{
public partial class UcMapControl2 : UserControl, IRMapControl
{
private ITool mCurrentTool = null; public ITool CurrentTool
{
get { return mCurrentTool; }
set { mCurrentTool = value; }
}
public REnvelope mExtent { get; set; }
public Map mMap { get; set; } private PointF m_panOffset = new PointF(, -); public PointF PanOffset
{
get { return m_panOffset; }
set { m_panOffset = value; }
}
private PointF m_dragOffset = new PointF(, ); public PointF DragOffset
{
get { return m_dragOffset; }
set { m_dragOffset = value; }
}
public UcMapControl2()
{
InitializeComponent();
mMap = new Map();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
} bool m_staticDirty = true;
//缓存图片?
Bitmap m_staticImage = null;
System.Drawing.Drawing2D.SmoothingMode m_smoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
protected override void OnPaint(PaintEventArgs e)
{
Tracing.StartTrack(Program.TracePaint);
#region old
e.Graphics.SmoothingMode = m_smoothingMode;
Rectangle cliprectangle = e.ClipRectangle;
if (m_staticImage == null)
{
cliprectangle = ClientRectangle;
m_staticImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
//m_staticImage.Save("D:\\a.png", ImageFormat.Png);
m_staticDirty = true;
}
//绘制在背景图片上
Graphics BitMapGc = Graphics.FromImage(m_staticImage);
BitMapGc.SmoothingMode = m_smoothingMode;
//绘制背景,注意如果不设将为透明色
BitMapGc.Clear(Color.White);
//this.BackgroundLayer.Draw(dcStatic, r);
//if (m_model.GridLayer.Enabled)
// m_model.GridLayer.Draw(dcStatic, r);
//绘制十字丝
RPoint rCenterPoint = new RPoint(, , );
PointF nullPoint = Transform.ToScreen(rCenterPoint, this);
BitMapGc.DrawLine(Pens.Blue, nullPoint.X - , nullPoint.Y, nullPoint.X + , nullPoint.Y);
BitMapGc.DrawLine(Pens.Blue, nullPoint.X, nullPoint.Y - , nullPoint.X, nullPoint.Y + );
if (m_staticDirty)
{
m_staticDirty = false; List<ILayer> layers = mMap.Layers;
for (int layerindex = layers.Count - ; layerindex >= ; layerindex--)
{
//if (layers[layerindex].Visible)
//layers[layerindex].Draw();
}
BitMapGc.Dispose();
}
//绘制背景图片
e.Graphics.DrawImage(m_staticImage, cliprectangle, cliprectangle, GraphicsUnit.Pixel);
//绘制新建对象
//if (m_newObject != null)
// m_newObject.Draw(dc, r);
#endregion
Tracing.EndTrack(Program.TracePaint, "OnPaint complete"); } protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (m_lastCenterPoint != null && Width != )
SetCenterScreen(Transform.ToScreen(m_lastCenterPoint, this), false);
m_lastCenterPoint = CenterPointUnit();
m_staticImage = null;
Invalidate();
}
RPoint m_lastCenterPoint;
/// <summary>
/// 设置画布到屏幕的中心
/// </summary>
/// <param name="rPoint">直角坐标系坐标</param>
public void SetCenter(RPoint unitPoint)
{
PointF point = Transform.ToScreen(unitPoint, this);
m_lastCenterPoint = unitPoint;
SetCenterScreen(point, false);
} protected void SetCenterScreen(PointF screenPoint, bool setCursor)
{
float centerX = ClientRectangle.Width / ;
m_panOffset.X += centerX - screenPoint.X; float centerY = ClientRectangle.Height / ;
m_panOffset.Y += centerY - screenPoint.Y; if (setCursor)
Cursor.Position = this.PointToScreen(new Point((int)centerX, (int)centerY));
Invalidate();
}
public RPoint CenterPointUnit()
{
RPoint p1 = Transform.ToUnit(new PointF(, ), this);
RPoint p2 = Transform.ToUnit(new PointF(this.ClientRectangle.Width, this.ClientRectangle.Height), this);
RPoint center = new RPoint();
center.X = (p1.X + p2.X) / ;
center.Y = (p1.Y + p2.Y) / ;
return center;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e); }
int n = ;
PointF p1;
PointF TempPoint2;//缓存移动过程中产生的第二个点,上一移动过程中
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e); Rectangle invalidaterect = Rectangle.Empty;
if (n < )
{ }
else
{
//REnvelope env = mNewLine.GetBoundingBox();
//mNewLine.P1 = new RPoint(e.X, e.Y, 0); double xmin = Math.Min(p1.X, TempPoint2.X);
double ymin = Math.Min(p1.Y, TempPoint2.Y);
double w = Math.Abs(p1.X - TempPoint2.X);
double h = Math.Abs(p1.Y - TempPoint2.Y);
invalidaterect = new Rectangle((int)xmin, (int)ymin, (int)w, (int)h);
invalidaterect.Inflate(, ); Pen pen = new Pen(Color.Red);
//擦除上次移动绘制的线,通过重绘上次移动范围区域的缓存图片实现
RepaintStatic(invalidaterect);
//Bitmap m_staticImage2 = new Bitmap(invalidaterect.Width, invalidaterect.Height);
//m_staticImage2.Save("D:\\adfghj.png", ImageFormat.Png);
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
Graphics dc = Graphics.FromHwnd(Handle);
dc.SmoothingMode = m_smoothingMode;
Point p2 = new Point(e.X, e.Y);
dc.DrawLine(pen, p1, p2);
dc.Dispose();
TempPoint2 = p2;
//Invalidate();
//DoInvalidate(false, invalidaterect); }
}
protected override void OnMouseDown(MouseEventArgs e)
{
n++;
if (n <= )
{
//mNewLine = new RSegment();
//mNewLine.P0 = new RPoint(e.X, e.Y, 0);
//mNewLine.P1 = new RPoint();
//初始化两个点,注意此处两点相同。
p1 = new PointF(e.X, e.Y);
TempPoint2 = new Point(e.X, e.Y);
Invalidate(true);
}
base.OnMouseDown(e);
}
public void DoInvalidate(bool dostatic, Rectangle rect)
{
if (dostatic)
m_staticDirty = true;
Invalidate(rect);
}
/// <summary>
/// 局部刷新,重新绘制图片Bitmap无效区域
/// </summary>
/// <param name="r"></param>
public void RepaintStatic(Rectangle r)
{
if (m_staticImage == null)
return;
Graphics dc = Graphics.FromHwnd(Handle);
if (r.X < ) r.X = ;
if (r.X > m_staticImage.Width) r.X = ;
if (r.Y < ) r.Y = ;
if (r.Y > m_staticImage.Height) r.Y = ; if (r.Width > m_staticImage.Width || r.Width < )
r.Width = m_staticImage.Width;
if (r.Height > m_staticImage.Height || r.Height < )
r.Height = m_staticImage.Height;
dc.DrawImage(m_staticImage, r, r, GraphicsUnit.Pixel);
Image temp = new Bitmap(m_staticImage);
Graphics gc = Graphics.FromImage(temp);
gc.DrawLine(Pens.Blue, r.Location, new Point(r.Location.X + r.Width, r.Location.Y + r.Height));
gc.Dispose();
temp.Save("d:\\AAAA.png", ImageFormat.Png);
m_staticImage.Save("d:\\BBBB.png", ImageFormat.Png);
dc.Dispose();
}
public float ScreenHeight()
{
return (float)(Transform.ToUnit(this.ClientRectangle.Height, this as IRMapControl));
}
private float mZoom = 1.0f;
public float Zoom
{
get
{
return mZoom;
}
set
{
mZoom = value;
}
}
}
}
UcMapControl2
今年的一个目标:完成一个开源项目
参考:http://www.codeproject.com/Articles/6238/Canvas-implementation-for-C
DIY自己的GIS程序(2)——局部刷新的更多相关文章
- DIY自己的GIS程序(1)——起航
一个GIS系统最重要的功能是绘制图形和关联属性,这里研究二维点线面的绘制过程: 对于一个绘图系统设计,图形的绘制涉及两个重要的方面. 1.一个是绘制或者重新绘制,重绘过程出现在下面情况: a 界面初始 ...
- 微信小程序setData局部刷新列表
利用setData局部刷新列表 当列表管理加载到第几页时,这个list的数据有十几条的,如果重新setData的话就要重新刷新和渲染列表, 这是个比较麻烦的事,当数据量大时,就会造成白屏, 这时就要局 ...
- ArcGIS engine中Display类库 (局部刷新)
转自原文 ArcGIS engine中Display类库 (局部刷新) Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这个类库 ...
- MVC无刷新分页(即局部刷新,带搜索,页数选择,排序功能)
我查看了很多网站,大部分评论分页都是局部刷新的,可大部分电商商品展示分页都是有刷新页面的,于是我便做了一个商品展示无刷新分页的例子.接下来我就将做一个模仿淘宝已买到的宝贝功能,不过我的是无刷新分页的. ...
- android:ListView的局部刷新
1.简介 对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView ...
- Android 结合实际项目学会ListView局部刷新和相关知识《一》
转载本专栏博客,请注明出处:道龙的博客 最近在公司参与的项目中有一个界面需要做局部UI更新处理,把其化烦为简为Demoi形式写在这里.我们还是运行该Demo,知道ListView局部刷新的使用场景:( ...
- .net updatePannel 局部刷新效果实现后,但是仍是全部刷新的修改方法
最近做了一个小例子,就是晚上都有的那种小的updatepannel的局部刷新的小例子,但是发现按照那个例子虽然能够实现label2的局部刷新,但是看上去效果确实整个页面都在刷新,这让人很头疼,所以我在 ...
- 转:android listview局部刷新和模拟应用下载
在 android开发中,listview是比较常用的一个组件,在listview的数据需要更新的时候,一般会用 notifyDataSetChanged()这个函数,但是它会更新listview中所 ...
- 安卓易学,爬坑不易——腾讯老司机的RecyclerView局部刷新爬坑之路
针对手游的性能优化,腾讯WeTest平台的Cube工具提供了基本所有相关指标的检测,为手游进行最高效和准确的测试服务,不断改善玩家的体验.目前功能还在免费开放中. 点击地址:http://wetest ...
随机推荐
- 怎样用MathType创建竖式算法
在使用MathType编辑公式时,有时将最简单的表达式变成Word文档也会出现一些问题.比如MathType竖式.下面介绍MathType竖式的一些编辑方法. 步骤如下: 步骤一:在MathType底 ...
- 【matlab】使用VideoReader提取视频的每一帧,不能用aviread函数~
这个问题是matlab版本问题,已经不用aviread函数了~ VideoReader里面没有cdata这个函数! MATLAB不支持avireader了,而且没有cdata这个属性了,详情去官网ht ...
- Animy.js,自己编写的功能丰富的html动画库
近期由于项目须要.本人制作了一款js的动画插件.能够用于完毕各种js动画.比方移动.拉升.变色等等,全部动画经常使用的功能都已经实现,比方播放.暂停.停止.循环.加速.减速.反向播放.缓动.路径动画. ...
- Java集合----Set集合
Set集合 Set 集合不允许包含相同的元素,如果试把两个相同的元素加入同一个 Set 集合中,则添加操作失败. Set 判断两个对象是否相同不是使用 == 运算符,而是根据 equals 方法 Ha ...
- swift--使用 is 和 as 操作符来实现类型检查和转换 / AnyObject与Any的区别
声明几个类: //动物类 class Animal{ } //陆地动物类 class terricole: Animal { } //海洋动物类 class SeaAnimals: Animal { ...
- 端口占用处理(Windows)
1. netstat -ano 看看 443端口被占用没 ,一般是本地地址加上端口号,那个端口号就是你要找的,同时最后一列是PID,即进程号. (常用的软件有虚拟机.VisualSVNServer)2 ...
- OnGlobalLayoutListener用法
1.implements ViewTreeObserver.OnGlobalLayoutListener{} 2.mContentView.getViewTreeObserver().addOnGlo ...
- 基于Cocos2d-x学习OpenGL ES 2.0系列——OpenGL ES渲染之Shader准备(7)
Cocos2d-x底层图形绘制是使用OpenGL ES协议的.OpenGL ES是什么呢? OpenGL ES(OpenGl for Embedded System)是OpenGL三维图形API的子集 ...
- php学习六:字符串
前言:越来越觉得php的强大之处了,不紧是数组,在字符串方面也可以看出它的优势,第一:方法多,集合了js,c,c#等多门语言的方法:第二:有许多方法是其他语言不具备的,如他的模糊比较,就是其他语言所没 ...
- JDBC--Result 获取返回集合
package jdbc.chap05; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql. ...