7.2.2 displaying drawings on a form

Source: Internet
Author: User

7.2.2 displaying drawings on a form


The drawing is similar to the example in the fourth chapter. Because the drawing takes a certain amount of time, we will create a bitmap in memory, draw a good document, and then display the bitmap on the form instead of drawing the document each time the form is invalidated. Let's take a look at the very useful functional programming patterns that will be used in this section.


"Hole in the middle (middle hole)" mode


[Really don't know, what does Hole in the middle mean? ]
A common scenario for writing code is to initialize it first, then the core part of the function, and finally, some cleanup work; in multiple locations in the program, similar operations are repeated, and the initialization and cleanup parts do not change, and only the core parts are different. For an example of drawing a bitmap in memory, code written in C # might look like this:


var bmp = new Bitmap (width, height)
using (var gr = graphics.fromimage (BMP)) {
(...)
}


Here, the core part of the code is the placeholder for the third row, and we will draw with the value Gr. The problem is that just using object-oriented programming concepts, you can't simply wrap the code that performs initialization and cleanup into a subroutine that is shared everywhere you draw different graphics. The C # language supports this pattern, which is well known, and typically uses various types of initialization and scavenging, which is the case with a using construct. How do we implement similar functionality for our code patterns?
With functional programming, it is easy to solve; You can write a higher-order function, wrap the core part into a lambda function, and use it as the parameter value of a higher-order function:


var bmp = DrawImage (width, height, gr = =
(...)
});


From a functional programming point of view, this is just a boring example that uses a higher-order function, but it describes a situation where some initialization, then the core, then cleanup, and this is very common, so you need to have a special name. The name was first used by Brian hurt in his blog, "Hole in the middle (middle hole)" mode [Hurt, 2007], which describes the situation well, that is, each time the code is used, only the middle part needs to be populated with different functions.


Listing 7.7 is a function implemented in F #, similar to the DrawImage in the supplementary material "Hole in the middle (middle hole) mode", with a little difference, specifying the size of the bitmap created with two parameters, and the ability to specify the margins of the image border.


Listing 7.7 functions for drawing graphics (F # Interactive)

> Let DrawImage (Width:int, height:int) Space Coredrawingfunc =
Let BMP = new Bitmap (width, height)
Use GR = Graphics.fromimage (BMP) [1]
Gr. Clear (Color.White)
Gr. TranslateTransform (space, space) <--moving the entire image
Coredrawingfunc (GR) [2]
Bmp
;;
Val drawimage:int * int, float32 (Graphics, Unit) –> Bitmap [3]


We use this function to draw the image, the core part of the drawing is the function given as the last parameter, from the type signature [3], it can be seen that the parameter of this function is a Graphics value, does not return the result; in the middle of the code [2], after creating the bitmap and Graphics object, is called. During the initialization phase, we also call TranslateTransform, which provides the margins for the image.
The Graphics object we created implements the IDisposable, so it needs to be released after the drawing is finished. In C #, we use well-known using constructs; in F #, you can do similar work with the USE keyword [1]. In Listing 7.7, the Graphics object is released automatically before the bitmap that returns the result. The Use keyword is a bit different from the using, and we'll discuss it in the Nineth chapter.
Finally, we need to look at the actual effect of the code. Now we'll create and test the form interactively. Listing 7.8 shows how to draw the screen elements of listing 7.5 and display the document on the form.


Listing 7.8 drawing a document using WinForms (F # Interactive)
> Let docimage = DrawImage (20.0f) (drawelements elements) [1]
Val Docimg:bitmap


> Open System.Windows.Forms
Let main = new Form (Text = "Document", BackgroundImage = Docimage, | Create a document
Width = docimage.width, Height = docimage.height) | The form
Main.                                                    Show ();; |


The line that draws the bitmap [1] may have to be explained. We call DrawImage, whose last argument is the function, which specifies the core part of the drawing. Since we have implemented this function in drawelements, you might want to be able to pass it directly as the last parameter value, however, Drawelements has two parameters, and the DrawImage function has only one parameter (the Graphics object that needs to be drawn). We use the hash function application to specify a list of screenelement values, the result of a hash application is a function, a Graphics object, and a document is drawn, which is exactly what we need (Figure 7.2).


Figure 7.2 The sample document saved in the list of screen elements, using the Drawelements function to draw on the form.



The way we used to document representations, although making the code to create the document was a bit clumsy, it was easy to implement drawing. In functional programming, we often find that different contexts require different data structures: To some extent, the desired usage determines a good representation, and for functional programs, the same information, in the same program, has different representations, not uncommon. Now that we have found a representation that is suitable for drawing images, we also need to design a representation that is suitable for construction and processing, and then write a conversion function that implements the conversion of two representations.

7.2.2 displaying drawings on a form

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.