Read: 1506 Comments: 1 Participate in comments
Title C # Program Diary Percyle [Original] Keyword C # Source C # Program Diary ------ This is an article, it is written to our school's BBS. At that time, I just entered the big three, and the tone is a bit fun? :) Now the third is to end, I really miss the time ... But I wish you a happy learning, should you still have no problem? Learn to suffer, there is also a happy. Simple, share with you; and, as usual, eager for your participation - don't let me call too long? I am eager, because I like to be full of life, I like a group of people for ideals and enthusiastic, while burning days, I like my seniors to go, leave their encouragement to me - I will think when I walk I told me all the happiness, pain, joy, and depressed, I am happy to watch them on my foundation, go faster, better! Oh, come, let's work together, we have a cup for our cause! ! Everyone see the C # program, if it is .NET, better look. Such a comment is an XML style that makes the program itself is a good document: // // The required designer variable. // Program writing, similar to java, is it? This procedure, although it is simple, it is worth talking about it. Oh, if you like, you may wish to follow your post. But first, I ask you a few weak :). 1, the document application of the VC is a typical view structure. Start the frame, so many categories, beginners tend to be confused, huh, can you tell me how it is packaged? 2, everyone writes the drawing function in the vc's onDraw function, but you will find that when the form minimizes or such events, the open graph is resembled, recursively draw, and occupy certain graphics. This is more clear to discover this. why? 3, I have done such an attempt, do the background diagram, then edit the text on the background chart. For example, I started to make a letter paper, typing in the letter paper, I feel very comfortable. I think the lines of the letter paper should be painted, in .. viEW with CDC pointer; perhaps, I have no experience, I do it. However, the input of the text will change the position of the graphic; then use the Ontime non-stop, it is good, very happy. However, when the font difference is too large, or when scrolling occurs in the editing area, it will be a mess. What should you think? Now I try to learn to do that view structure --- From only one form, don't add the richedit control, now just start, but I feel very interesting. It can make people think about the problem. First, edit this function: // // // Designer Support required - Do not use the code editor to modify the /// this method.
/// private void InitializeComponent () {// // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size (6, 14); this.AutoScroll = true; this.AutoScrollMinSize = new System.Drawing.Size (550 , 450; this. brrawing.color = 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);} #ENDREGON, the following sentence is simple to say: this.backcolor = system.drawing.color = system.drawing.color.white; makes the form color to White as editable zones, and this.autoscrollminsize = new system.drawing. Size (550, 450); Tell the form document How big below the code draws graphics in the document - Preparation: /////mber 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 Pe n redPen = new Pen (Color.Red, 2); the drawing: Rectangle rectangleArea = new Rectangle (rectangleTopLeft, rectangleSize); Rectangle ellipseArea = new Rectangle (ellipseTopLeft, ellipseSize); dc.DrawRectangle (bluePen, rectangleArea); dc.DrawEllipse (Redpen, Ellipsearea); Where do you think the above code should be added? In the constructor of Form1? Yes, beforehand plus: Graphics DC = this.creategraphics (); this.show (); but you will find that after the form is minimized, or after it is masked by other forms, the graphic disappears. . Oh, the phenomenon of VC reproduces. In fact, Windows will use the Paint event to notify the application to complete some re-draw requirements.
So, you should overload this function as follows: ///// 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);} The added IF judgment is a small optimization. For this program, if the drawn graphics are not masked, it is not necessary to re-draw, thereby increasing efficiency. However, I will say below, let you understand (1), what is the role of size scrolloffset = new size (this.AutoscrollPosition); If you first remove it, then, then the judgment of the IF will be changed, or simply not optimize, the execution will find that when you use the vertical scroll bar, it will find that the graphic is like that elliptical flower, and I used the letter paper as the same :). well, why? Look at the picture, you will understand: ----------------------------------------- | ... B | ----------------------.--------- | || ...... .. | ....................................... | ||. ....... | ------------------------------- .. | || ...... .. | a ............................................. || ... ........................................... | || ........ | ..................................... .... | || ........ | ---------------------------------- || --------------------------------------- | | If B is the Document area, A Is Clientarea, the coordinate processing here, is done by the above program. (1) Handling the scrolling data, have you found it? The current view is still very incomplete, I try again to solve the problem of text input related processing. Oh, after the results, I will talk about it again. Here is a complete program. There is no part that is huge, practical text processing procedures. Everyone skip is yes :).
Using system.drawing; using system.collections; using system.bomponentmodel; using system.windows.form; using system.data; namespace view {///////// // // // // // // // Form1 summary description. /// public class form1: system.windows.Forms.form {///// The required designer variable.
/// 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 () {//////////// Todo: in InitializeComponent Add any constructor code // if (! Yn) DrawString = "sample text";} ///// 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, Ellips eSize); dc.DrawRectangle (bluePen, rectangleArea); dc.DrawEllipse (redPen, ellipseArea);} // Create string to drawif (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);} / // / / Clean all the resources being used.