| ||||||||||
c#程序日记 ------这是以前的一篇文章,写的时候是给我们学校的bbs发的。当时刚进入大三,口气有点好玩对吗?:)现在大三要结束了,真怀念那段时光……可是,祝愿大家快乐的学,应该仍没问题是吗?
大家看c#的程序,如果是.net里,更好看些。诸如这样的注释,是xml风格,使得程序本身就是很好的文档: /// 必需的设计器变量。 /// 程序的书写,类似与java,是不是? 本程序,虽然简单,但有点东西值得说一说。呵呵,如是愿意,您不妨跟帖。却首先,我问您几个有点弱:)的问题。 1,vc的文档应用程序,是典型的视图结构。开始的框架,那么多类,初学者往往被搞糊涂了,呵呵,你能否说说它是如何封装的? 现在我尝试用c#来学做那个视图结构---从仅有的一个窗体开始,不加richedit控件,现在刚开始,却感觉很有意思。很能让人去想问题。
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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.BackColor = System.Drawing.Color.White; this.AutoScrollMinSize = new System.Drawing.Size(550, 450); 下面的代码在文档里绘制图形---- /// member fileds /// 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); 之绘制: 你觉得上面的代码应加在哪里?在Form1的构造函数里?可以,前面加上这样两句: 可是,你会发现,窗体最小化后,或被其它窗体掩盖后,再出现图形就消失了。呵呵,vc里的现象重现。事实上,windows会利用paint事件来通知应用程序需要完成一些重新绘制的要求。所以,应该重载这个函数如下: /// ///override OnPaint /// 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); Size ScrollOffset=new Size(this.AutoScrollPosition); 这一句有什么作用呢?如果你先把它去掉,然后,再把if里的判断捎加改动,或干脆不优化,执行就会发现,当你使用竖直滚动条时,就会发现,图形如那个椭圆花了,和我以前那个信纸一模一样:)。 ----------------------------------------- 如果B是document区,A是clientarea,这里的坐标处理,由上面的程序完成。(1)处理了滚动的数据,你发现在哪儿了吗? 现在的视图还非常不完整,我再试图解决文字输入相关的处理问题。呵呵,有成果后,我会再谈谈感想的。下面是完整的程序。没有说到的部分,是一些无所害、在实践文字处理的程序。大家跳过就是了:)。 using System; namespace view /// Form1 的摘要说明。 /// public class Form1 : System.Windows.Forms.Form { /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; /// /// member fileds /// 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() // /// ///override OnPaint /// 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. // Create point for upper-left corner of drawing. // Draw string to screen. /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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); } /// /// 应用程序的主入口点。 /// [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) private void Form1_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e) 好啦,我也累了,本来,家教回来的时候,已经7点多了,我在车上都睡着了。可是,我还是愿意谈谈,呵呵,如果对您有用处,我该是多么的高兴。 |
c#程序日记
最新推荐文章于 2023-03-03 16:57:09 发布