C # Invalidate () method usage

Source: Internet
Author: User

Reference: Source

 

Invalidate () is a member of System. Windows. Forms. Form. It marks the client window area as invalid. Therefore, it ensures that a Paint event is triggered when you need to redraw the window. Invalidate () has two overload Methods: you can send a rectangle to it to specify the window area to be re-drawn (using page coordinates). If no parameter is provided, it marks the entire customer region as invalid.

Why do you want to do this? If you know that you need to draw some content, why not call OnPaint () or other methods that directly complete the drawing task? Generally, it is best not to directly call the drawing routine, if the code is to complete some drawing tasks, you should call Invalidate (). The reason is as follows:

 

● Plotting is always a processor-intensive task that can be executed by a GDI + application. Plotting in the middle of other jobs may impede other jobs. In the previous example, if you call a method directly in the LoadFile () method to complete the drawing, the LoadFile () method will be returned after the drawing is completed. During this period, the application will not respond to other events. On the other hand, by calling Invalidate (), you can have Windows trigger a Paint event before returning from LoadFile. In Windows, you can check the waiting events. The internal working mode is that an event is treated as a message in the message queue. Windows checks the queue regularly. If there is an event in the queue, Windows selects it and calls the corresponding event handler. Currently, the Paint event is the only event in the queue, so OnPaint () is called immediately. However, in a complicated application, there may be other events, some of which have higher priority than OnPaint. Especially if the user has decided to exit the application, the event will be marked with the message WM_QUIT.

● It is related to the first reason. If there is a complicated multi-threaded application, you will want to use a thread to process all the drawing operations. Using Invalidate () can pass all the drawing operations to the information queue, which helps ensure that no matter what drawing operations other threads request, all the drawing operations are completed by the same thread (no matter what thread is responsible for the information queue, it is done by the thread Application. run () to process drawing operations ).

● There is another reason related to performance. Assuming that there are several different Screen Drawing requests at a time, the Code may only be able to modify the document to ensure that the updated document is displayed, and the user just removed another window that covers some customer regions. Call Invalidate () to notify Windows of events. Windows will merge the painting event as needed, and merge the invalid areas so that the drawing operation will be executed only once.

● Finally, the code to execute the drawing may be the most complex part of the code in the application, especially when there is a professional user interface. People who need to maintain the code for a long time want us to put all the drawing code in one place and make it as simple as possible-if there are not too many paths in other parts of the program to enter the code section, maintenance is easier.

The bottom line is that it is best to put all the drawing code in the OnPaint () routine or other methods called in this method. But maintain a balance. If you want to replace one character on the screen, it is best not to affect the other content that has been drawn. At this time, you may not need to use Invalidate (), but just write an independent drawing routine.

Note:

In very complex applications, you can even write a complete class specifically responsible for drawing on the screen. A few years ago, MFC was still a standard technology for GDI-intensive applications, and it followed this pattern to use a C ++ class CView for plotting. Even so, this class has a member function OnDraw () that serves as the entry point for most drawing requests.

From: http://www.tzwhx.com/newOperate/html/1/12/124/10475.html

Invalidate () method in C #

Reload list
Invalidates a specific area of the Control and sends a draw message to the control.

Supported by the. NET Framework Lite version.

[C #] public void Invalidate (); [C ++] public: void Invalidate (); invalidates a specific area of the Control and sends a draw message to the control. The sub-control assigned to the control is invalid.

[C #] public void Invalidate (bool); invalidates the specified area of the control (add it to the update area of the control, and re-draw the update area next time during the painting operation ), and send a draw message to the control.

Supported by the. NET Framework Lite version.

[C #] public void Invalidate (Rectangle); invalidates the specified area of the control (add it to the update area of the control, and re-draw the update area next time during the painting operation ), and send a draw message to the control.

[C #] public void Invalidate (Region); invalidates the specified area of the control (add it to the update area of the control, and re-draw the update area next time during the painting operation ), and send a draw message to the control. The sub-control assigned to the control is invalid.

[C #] public void Invalidate (Rectangle, bool); makes the specified area of the Control invalid (add it to the update area of the control, and re-draw the update area next time during the Painting Operation ), and send a draw message to the control. The sub-control assigned to the control is invalid.

[C #] public void Invalidate (Region, bool); example
[Visual Basic, C #, C ++] the following example enables you to drag an image or image file to a form and display it at a placement point. Every time you draw a form, override the OnPaint method to re-draw the image; otherwise, the image will be redrawn until the next time. The DragEnter event processing method determines the Data Type dragged to the form and provides appropriate feedback. If the Image can be created from the data, the DragDrop event processing method will display the Image on the form. Because the values of DragEventArgs. X and DragEventArgs. Y are screen coordinates, the PointToClient method is used in this example to convert them into workspace coordinates.
From: http://www.tzwhx.com/newOperate/html/1/12/122/16848.html

MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate (VS.71). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.