- The Fly weight Pattern
- Discussion
- Example Code
- Handling the Mouse and Paint Events
- Flyweight Uses in C #
- Sharable Objects
- Copy-on-Write Objects
Handling the Mouse and Paint Events
In C# we intercept the paint and mouse events by adding event handlers. To do the painting of the folders, we add a paint event handler to the picture box.
Pic.Paint += new PaintEventHandler (picPaint);
The picPaint handler we add draws the folders, as we showed above. We added this code manually because we knew the signature of a paint routine.
private void picPaint(object sender, PaintEventArgs e ) {
While the mouse move event handler is very much analogous, we might not remember its exact form. So, we use the Visual Studio IDE to generate it for us. While displaying the form in design mode, we click on the PictureBox and in the Properties window we click on the lightning bolt to display the possible events for the PictureBox, as shown in Figure 19-4.
Figure 19-4. Selecting the MouseMove event from the Properties window
Then we double-click on MouseMove, and it generates the correct code for the mouse move event and adds the event handler automatically. The generated empty method is as follows.
private void Pic_MouseMove(object sender, MouseEventArgs e) { }
The code generated to add the event handler is inside the Windows Form Designer generated section. It amounts to this.
Pic.MouseMove += new MouseEventHandler(Pic_MouseMove);