15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

OC40 ❘ ChaPTer 48 GrAphics with Gdi+<br />

}<br />

if (position.X > margin + theLine.Width)<br />

{<br />

return -1;<br />

}<br />

// all is OK. We can return answer<br />

return index;<br />

Finally, on occasion, you also need to convert between line index <strong>and</strong> page, rather than world, coordinates.<br />

The following methods achieve this:<br />

private Point LineIndexToPageCoordinates(int index)<br />

{<br />

return LineIndexToWorldCoordinates(index) +<br />

new Size(AutoScrollPosition);<br />

}<br />

private int PageCoordinatesToLineIndex(Point position)<br />

{<br />

return WorldCoordinatesToLineIndex(position-new<br />

Size(AutoScrollPosition));<br />

}<br />

Note that when converting to page coordinates, you add the AutoScrollPosition, which is negative.<br />

Although these methods by themselves do not look particularly interesting, they do illustrate a general<br />

technique that you will probably need to use often. With GDI+, you will often find yourself in a situation<br />

where you have been given specific coordinates (for example the coordinates of where the user has clicked<br />

the mouse), <strong>and</strong> you will need to figure out what item is being displayed at that point. Or it could<br />

happen the other way around — where should a particular display item be displayed Hence, if you are<br />

writing a GDI+ application, you will probably find it useful to write methods that do the equivalent of the<br />

coordinate transformation methods illustrated here.<br />

responding to user input<br />

So far, with the exception of the File menu in the CapsEditor sample, everything you have done in this<br />

chapter has been one way: the application has talked to the user by displaying information on the screen.<br />

Almost all software of course works both ways: the user can talk to the software as well. You are now going<br />

to add that functionality to CapsEditor.<br />

Getting a GDI+ application to respond to user input is actually a lot simpler than writing the code to draw<br />

to the screen. (Chapter 39 covers how to h<strong>and</strong>le user input.) Essentially, you override methods from the<br />

Form class that are called from the relevant event h<strong>and</strong>ler, in much the same way that OnPaint() is called<br />

when a Paint event is raised.<br />

The following table lists the methods you might want to override when the user clicks or moves the mouse.<br />

meThod<br />

OnClick(EventArgs e)<br />

OnDoubleClick(EventArgs e)<br />

OnMouseDown(MouseEventArgs e)<br />

OnMouseHover(MouseEventArgs e)<br />

OnMouseMove(MouseEventArgs e)<br />

OnMouseUp(MouseEventArgs e)<br />

Called When …<br />

Mouse is clicked.<br />

Mouse is double-clicked.<br />

Left mouse button is pressed.<br />

Mouse stays still somewhere after moving.<br />

Mouse is moved.<br />

Left mouse button is released.<br />

If you want to detect when the user types in any text, then you will probably want to override the methods<br />

listed in the following table.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!