c#程序日记

c#程序日记

------这是以前的一篇文章,写的时候是给我们学校的bbs发的。当时刚进入大三,口气有点好玩对吗?:)现在大三要结束了,真怀念那段时光……可是,祝愿大家快乐的学,应该仍没问题是吗?


学有所苦,亦有所乐。
简单一记,与大家分享;并且,照例的,渴望着您的参与---不要让我太久的呼唤好吗?我渴望,因为我喜欢充满憧憬的生活,我喜欢一群人为理想而热烈、而燃烧的日子,我喜欢我的前辈同学走了,留下他们的鼓励给我---我会想着当我走时,我把所有的快乐、痛苦、欢欣、郁闷都告诉我的ddmm们,我快乐的看着他们在我的基础之上,走的更快、更好!呵呵,来吧,我们一起努力啊,我们为我们的一世,为我们的事业而干杯!!

大家看c#的程序,如果是.net里,更好看些。诸如这样的注释,是xml风格,使得程序本身就是很好的文档:
/// <summary>
  /// 必需的设计器变量。
  /// </summary>
程序的书写,类似与java,是不是?

本程序,虽然简单,但有点东西值得说一说。呵呵,如是愿意,您不妨跟帖。却首先,我问您几个有点弱:)的问题。

1,vc的文档应用程序,是典型的视图结构。开始的框架,那么多类,初学者往往被搞糊涂了,呵呵,你能否说说它是如何封装的?
2,大家在vc的ondraw函数里,都写过绘图函数,可是,您会发现,当窗体最小化或诸如此类事件发生时,再打开图形是重新绘制的,递归绘制而占用时间的某些图形能更清楚的发现这一点。为什么?
3,我做过这样的尝试,做背景图,然后在背景图上编辑文字。比如我开始作的是信纸,在信纸里打字,感觉很舒服。我觉得,信纸的线条,应是画上去的,在..view里用cdc的指针;或许思路不对,我没有经验,就动手做了。可是,文字的输入,会改变图形的位置;后来用ontime不停的重画,对某种大小的字是可以了,挺高兴。但字体差别太大时,或当编辑区发生滚动时,又会一塌糊涂。你以为该如何呢?

现在我尝试用c#来学做那个视图结构---从仅有的一个窗体开始,不加richedit控件,现在刚开始,却感觉很有意思。很能让人去想问题。


首先,对此函数编辑:
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(550, 450);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(576, 373);
//this.Name = "Form1";   
this.Text = "Scroll view";

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion
其中,下面的句子简单说说:

this.BackColor = System.Drawing.Color.White;
使窗体颜色为white,作为可编辑区,且

this.AutoScrollMinSize = new System.Drawing.Size(550, 450);
告诉了窗体文档有多大

下面的代码在文档里绘制图形----
之准备工作:
/// <summary>
/// member fileds
/// </summary>
private Point rectangleTopLeft= new Point(50,50);
private Size rectangleSize= new Size(200,200);
private Point ellipseTopLeft=new Point(50,200);
private Size ellipseSize=new Size(200,150);
private Pen bluePen=new Pen(Color.Blue,3);
private Pen redPen=new Pen(Color.Red,2);

之绘制:
Rectangle rectangleArea=new Rectangle(rectangleTopLeft,rectangleSize);
Rectangle ellipseArea=new Rectangle(ellipseTopLeft,ellipseSize);
dc.DrawRectangle(bluePen,rectangleArea);
dc.DrawEllipse(redPen,ellipseArea);

你觉得上面的代码应加在哪里?在Form1的构造函数里?可以,前面加上这样两句:
Graphics dc=this.CreateGraphics();
this.Show();

可是,你会发现,窗体最小化后,或被其它窗体掩盖后,再出现图形就消失了。呵呵,vc里的现象重现。事实上,windows会利用paint事件来通知应用程序需要完成一些重新绘制的要求。所以,应该重载这个函数如下:

///<summary>
///override OnPaint
///</summary>
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc= e.Graphics;
Size ScrollOffset=new Size(this.AutoScrollPosition);
if(e.ClipRectangle.Top+ScrollOffset.Width<350||
 e.ClipRectangle.Left+ ScrollOffset.Height<250)
{
//(1)
Rectangle rectangleArea=new Rectangle(rectangleTopLeft+ScrollOffset,rectangleSize);
Rectangle ellipseArea=new Rectangle(ellipseTopLeft+ScrollOffset,ellipseSize);
dc.DrawRectangle(bluePen,rectangleArea);
dc.DrawEllipse(redPen,ellipseArea);
}

base.OnPaint(e);
}
加的if判断是个小小的优化。对于本程序,如果所绘制的图形没有被掩盖,就不需要重新绘制,从而提高效率。然而,和下面的代码有关的我下面再说,您明白(1)就好:

Size ScrollOffset=new Size(this.AutoScrollPosition);

这一句有什么作用呢?如果你先把它去掉,然后,再把if里的判断捎加改动,或干脆不优化,执行就会发现,当你使用竖直滚动条时,就会发现,图形如那个椭圆花了,和我以前那个信纸一模一样:)。
呵呵,为什么?看下图,你就会明白:

-----------------------------------------
|......B|----------------------.---------| |
|........|...........................................| |
|........|-------------------------------..| |
|........|A........................................| |
|........|...........................................| |
|........|...........................................| |
|........|------------------------...------| |
|---------------------------------------| |

如果B是document区,A是clientarea,这里的坐标处理,由上面的程序完成。(1)处理了滚动的数据,你发现在哪儿了吗?

现在的视图还非常不完整,我再试图解决文字输入相关的处理问题。呵呵,有成果后,我会再谈谈感想的。下面是完整的程序。没有说到的部分,是一些无所害、在实践文字处理的程序。大家跳过就是了:)。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace view
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// member fileds
/// </summary>
private Point rectangleTopLeft= new Point(50,50);
private Size rectangleSize= new Size(200,200);
private Point ellipseTopLeft=new Point(50,200);
private Size ellipseSize=new Size(200,150);
private Pen bluePen=new Pen(Color.Blue,3);
private Pen redPen=new Pen(Color.Red,2);
private String drawString;
private bool yn=false;
private float x = 150.0F;
private float y = 50.0F;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
if(!yn) drawString = "Sample text";
}

///<summary>
///override OnPaint
///</summary>
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc= e.Graphics;
Size ScrollOffset=new Size(this.AutoScrollPosition);
if(e.ClipRectangle.Top+ScrollOffset.Width<350||
 e.ClipRectangle.Left+ ScrollOffset.Height<250)
{
Rectangle rectangleArea=new Rectangle(rectangleTopLeft+ScrollOffset,rectangleSize);
Rectangle ellipseArea=new Rectangle(ellipseTopLeft+ScrollOffset,ellipseSize);
dc.DrawRectangle(bluePen,rectangleArea);
dc.DrawEllipse(redPen,ellipseArea);
}
// Create string to draw
if(!yn) drawString = "Sample Text";
//String drawString = "Sample Text";

// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create point for upper-left corner of drawing.
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;

// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
base.OnPaint(e);
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(550, 450);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(576, 373);
//this.Name = "Form1";
this.Text = "Scroll view";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
  
}

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//yn=true;
//drawString = e.KeyCode.ToString();
}

private void Form1_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
   
yn=true;
drawString = e.KeyChar.ToString();
x+=1;
//Form1.ActiveForm.BackColor=Color.Blue;
   
}
}
}

好啦,我也累了,本来,家教回来的时候,已经7点多了,我在车上都睡着了。可是,我还是愿意谈谈,呵呵,如果对您有用处,我该是多么的高兴。
如果我走了,毕业了,我想这样也没机会了。
你呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值