This is simple enough. There are two unrelated aspects you have to put together 1) handling of keyboard and mouse events and checking keyboard status, 2) transformation of graphics. You need to put them together.
First of all, you need to handle the event
MouseWheel
or override a virtual method
OnMouseWheel
for the control you need to respond to mouse wheel:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousewheel.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousewheel.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx[
^].
Pay attention that the event arguments give you the information on the value scrolled by the wheel in the property
Delta
.
In the code of the handler, you also need to check if Ctrl is pressed. You may also need to check if some other
modifier keys, such as Alt or Shift are
not pressed. You don't need to check each key though. Instead, you override the virtual methods
OnKeyDown
and
OnKeyUp
or handle the events
KeyDown
and
OnKeyUp
which will respond to all key events. In each event for each key, the state of modifier keys may or may not be changed. The current state of such keys is passed to you in event arguments in the value of the property
Modifiers
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.modifiers.aspx[
^].
Store this value on each event and check the last modifier state in your handler of the mouse wheel event. If Ctrl is pressed (and maybe other modifier keys are not), do the zoom.
Now, the actual zoom. It really depends on what your graphics is and how you render it. If you are trying to work with
PictureBox
, it could be the bitmap, which should not really be zoomed with enlargement: it would produce unacceptable quality, pixellation. You can only reduce the size with acceptable quality, or even enlarge, but only slightly. You still can do it, if your actual bitmap data is prepared for maximum possible size, so at all scales it could only be reduced in size.
However, I don't know, maybe you data is actually vector, and you just render it on a
PictureBox
by same weird reason (which is actually a usual big mistake of many beginners). Then you can get perfect graphics at any scale, only you should get rid of
PictureBox
, even if you actually use some bitmap. Instead, you should directly render your graphics on some control (including a
Form
), through overriding the virtual method
OnPaint
or handling the event
Paint
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx[
^].
You rendering method should depend on some data you store in your class field(s), including the zoom factor. When you change zoom (or anything else), you call one of the
Invalidate
methods, to cause redrawing:
http://msdn.microsoft.com/en-us/library/598t492a.aspx[
^],
http://msdn.microsoft.com/en-us/library/xz8ytzt0.aspx[
^],
http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx[
^],
http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx[
^].
If this is not a bitmap, you don't have to re-calculate all the graphics using the zoom factor. You should better use the property
System.Drawing.Graphics.Transform
:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[
^].
The instance of the class
System.Drawing.Graphics
will be passed to your paint handler in the event arguments.
If you want to understand why
PictureBox
is so bad for the purpose, please see my past answers:
Append a picture within picturebox[
^],
draw a rectangle in C#[
^],
How do I clear a panel from old drawing[
^].
On GDI+ rendering and invalidation, see also my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
capture the drawing on a panel[
^],
How to speed up my vb.net application?[
^],
Drawing Lines between mdi child forms[
^].
[EDIT]
Please see the most recent answer providing more detail on zoom:
How to know the mouse position for control inside UserControl[
^].
—SA