C # program diary

zhaozj2021-02-16  50

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 necessary designer variables. /// The writing of the program, 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 Supports the required method - 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.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, where the sentence is simple to say:

This.BackColor = system.drawing.color.white; makes the form color to White, as editable zone, and

this.autoscrollminsize = new system.drawing.size (550, 450); telling how many forms of formal documents

The following code draws graphical graphics in the document - Preparation: ///

/// 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);

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, before adding this two sentences: graphics dc = this.creategraphics (); this.show ();

However, you will find that after the form is minimized, or after being covered 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, EllipSize); 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 with the following code, you understand (1) just:

Size scrolloffset = new size (this.autoscrollposition);

What is the role of this sentence? 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 below, you will understand:

----------------------------------------- | ... B | ----------------------.--------- | || ........ | ...... ..................................... | || ........ | ------------------------------ .. | || ........ | a ..... ..................................... | || ........ | ... ........................................ | || ....... | ....................................... | || .. ... | -------------------------------- | || ------- -------------------------------- | |

If B is the Document area, A is ClientArea, where the coordinate processing 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.componentmodel; using system.windows.form; using system.data ;. using system.data ;. USING SYSTEM.DATA;

A summary description of Namespace View {////

/// form1. /// public class form1: system.windows.Forms.form {/// // The designer variable is required. /// 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 () {/// Windows Form Designer Support for // InitializeComponent ();

/// / TODO: Add any constructor code // if (! Yn) DrawString = "sample text" after the initializeComponent call.

///

/// 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 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. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose

#Region Windows Form Designer Generated Code ///

/// Designer Supports the required method - 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.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

///

/// The primary entry point of the application. /// [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;}}}

Ok, I am tired, I originally, my family is coming back, I have been 7 o'clock, I am falling asleep on the car. However, I am still willing to talk, huh, if you are useful to you, how happy I should be. If I am gone, I graduated, I don't have the opportunity to do this. What about you?

转载请注明原文地址:https://www.9cbs.com/read-25445.html

New Post(0)