Click here to Skip to main content
15,881,615 members
Articles / Multimedia / GDI+
Article

Simple zoom functionality for custom controls

Rate me:
Please Sign up or sign in to vote.
2.41/5 (26 votes)
13 Apr 20062 min read 117.4K   2.7K   52   32
How to add simple zoom functionality for custom controls.

Introduction

Initially, I wrote a small article to describe what is necessary to add zoom functionality to a custom control (see Details below). There were many users who had problems using the described routines in their own code. That is the reason I decided to program a custom control library (UniversalControl.dll) and provide it with a demo project. I also added a vertical and horizontal scrollbar to the control, because it seemed to be an indispensable feature.

Usage (see also the demo project)

  1. Add the UniversalControl.dll library to your toolbox.
  2. Drag the CUniversalControl item in your application.
  3. Configure your CUniversalControl (set colors, enable/disable scrollbars, ...).
  4. Write your own drawing routine (DrawContents(Graphics grc)).
  5. Add it to the control drawing event.
C#
cUniversalControl1.EDraw += new DDrawContents(DrawContents);

Details

The user selects the zoom area with his left mouse button, see the screenshots below:

Sample Image

Sample Image

The Code

Three routines to handle the mouse events are added to the control. The OnMouseDown method is called when a mouse button is pressed. It sets the starting point for the new zoom area and resets its size to zero.

C#
/// <summary>
/// event handler for mouse button pressed
/// sets zoom area start coordinates
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnMouseDown(object sender, 
          System.Windows.Forms.MouseEventArgs e)
{
  // reset zoom state
  m_blnZoom = false;
  // sets the starting point of the zoomed area
  m_ZoomRect = new Rectangle(new Point(e.X, e.Y), 
                             new Size(0, 0));
}

The OnMouseMove updates the area to zoom based on the current mouse position as long as the left mouse button is pressed. It calls the Invalidate method of the control to update the chosen zoom rectangle (black rectangle). Drawback: Invalidate called without parameters updates the whole control, that may result in some flickering during the selection of the area to zoom (solution: invalidate only the necessary region).

C#
/// <summary>
/// handles mouse move events
/// updates current zoom area
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnMouseMove(object sender, 
          System.Windows.Forms.MouseEventArgs e)
{
  // updates the current zoom area
  // during mouse mouvements 
  // so it can be drawn by the OnPaint method
  if (e.Button != MouseButtons.Left) return;
  if (m_ZoomRect.X >= e.X)
      m_ZoomRect.Width += Math.Abs(e.X - m_ZoomRect.X);
  else m_ZoomRect.Width = Math.Abs(e.X - m_ZoomRect.X);
  if (m_ZoomRect.Y >= e.Y)
      m_ZoomRect.Height += Math.Abs(e.Y - m_ZoomRect.Y);
  else m_ZoomRect.Height = Math.Abs(e.Y - m_ZoomRect.Y);
  if (m_ZoomRect.X > e.X) m_ZoomRect.X = e.X;
  if (m_ZoomRect.Y > e.Y) m_ZoomRect.Y = e.Y;
  this.Invalidate();
}

The OnMouseUp method finally is called when a mouse button is released. If the left mouse button is released, the next call to the OnPaint method will draw the chosen area onto the whole control (zoom).

C#
/// <summary>
/// event handler for mouse button released
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnMouseUp(object sender, 
          System.Windows.Forms.MouseEventArgs e)
{
  // left mouse button released,
  // zoom to the chosen area
  if (e.Button == MouseButtons.Left)
      m_blnZoom = true; 
  this.Refresh();
}

Last but not least, the code that needs to be added to the OnPaint method. If the control is in “zoom-mode”, the necessary scaling and translation on the Graphics are executed. After that, all the drawing wanted for the control is done (DrawContents()). Finally the rectangle for the area to zoom (black rectangle) is drawn. That’s all.

C#
/// <summary>
/// redraws the control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnPaint(object sender, 
          System.Windows.Forms.PaintEventArgs e)
{
  if (m_blnZoom)
  {
    // scale and translate the transform
    // that the selected area (m_ZoomRect)
    // is drawn on the whole control (zoom)
    e.Graphics.ScaleTransform((float)this.ClientSize.Width/m_ZoomRect.Width, 
      (float)this.ClientSize.Height/m_ZoomRect.Height);
    e.Graphics.TranslateTransform(-m_ZoomRect.X, -m_ZoomRect.Y);
  }
  
  // draw control specific contents (data to be zoomed)
  DrawContents(e.Graphics);

  if (!m_blnZoom)
  {
    // draws the selected zoom area during mouse movements
    e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), 
                                                         m_ZoomRect);
  }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCould you send me( elliottwavechina@gmail.com) the source codes of UniversalControlLibrary.dll? thanks a lot Pin
Elliottwave China23-Jun-21 19:50
Elliottwave China23-Jun-21 19:50 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Feb-12 22:41
professionalManoj Kumar Choubey28-Feb-12 22:41 
GeneralMy vote of 1 Pin
alleph27-Feb-12 11:05
alleph27-Feb-12 11:05 
Generalmissing DLL Pin
kouroy8-Sep-10 20:17
kouroy8-Sep-10 20:17 
GeneralRe: missing DLL Pin
willl_agu4-Aug-11 12:01
willl_agu4-Aug-11 12:01 
QuestionNeed Help over Zooming Pin
Khurram_riaz30-Aug-10 22:41
Khurram_riaz30-Aug-10 22:41 
GeneralMy vote of 1 Pin
Meurisse7-Feb-10 21:22
Meurisse7-Feb-10 21:22 
GeneralMy vote of 1 Pin
general.failure27-Jan-10 6:36
general.failure27-Jan-10 6:36 
QuestionCould you please send the missing DLL ? Pin
Mor Vered20-Sep-09 23:17
Mor Vered20-Sep-09 23:17 
GeneralMissing DLL Pin
Frank Gennaro 24872195-Sep-09 6:08
Frank Gennaro 24872195-Sep-09 6:08 
GeneralZoom On An Image File Pin
kjward26-Nov-08 7:05
kjward26-Nov-08 7:05 
i have an image viewer app which displays tiff's, jpg's, etc. how to use your library on an retrieved image rather than on a drawing?

and i'd sure like to get the source code so i can add enhancements for my specific needs. (this is the point of "open source", to extend it for the use of the community as a whole.)

thanks in advance

kjward

Generalsource code Pin
danny westfall15-Nov-08 15:16
danny westfall15-Nov-08 15:16 
GeneralUniversal Control Library Pin
jjjay2-Jul-08 3:49
jjjay2-Jul-08 3:49 
GeneralSource code for UniversalControl.dll Pin
Member 32842919-May-08 3:45
Member 32842919-May-08 3:45 
GeneralSource code of UniversalControl.dll Pin
waths1-Apr-08 0:14
waths1-Apr-08 0:14 
GeneralRe: Source code of UniversalControl.dll Pin
Grofi6-Apr-08 1:52
Grofi6-Apr-08 1:52 
GeneralRe: Source code of UniversalControl.dll Pin
Member 32842919-May-08 3:43
Member 32842919-May-08 3:43 
QuestionCan i please have UniversalControl.dll? Pin
andredani30-Mar-08 21:36
andredani30-Mar-08 21:36 
AnswerRe: Can i please have UniversalControl.dll? Pin
Saysa8-Jul-09 21:39
Saysa8-Jul-09 21:39 
GeneralwebBrowser control Pin
hostopolis27-Aug-07 3:15
hostopolis27-Aug-07 3:15 
Generalplease help me Pin
Mausumi10-Jan-07 18:19
Mausumi10-Jan-07 18:19 
GeneralCode for UniversalControl.dll Pin
Michael Kämpf29-Oct-06 2:56
Michael Kämpf29-Oct-06 2:56 
QuestionThe Code.. Pin
deltaht917-Jun-06 2:32
deltaht917-Jun-06 2:32 
GeneralThis 'article' kind of sucks Pin
fwsouthern15-Apr-06 10:35
fwsouthern15-Apr-06 10:35 
Generalscroll bars Pin
M i s t e r L i s t e r16-Dec-05 19:00
M i s t e r L i s t e r16-Dec-05 19:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.