Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / VC++
Tip/Trick

Simple Magnifier Dialog

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 Jun 2020CPOL2 min read 5.7K   354   6   3
A modeless dialog that allows magnification of a section of a display screen
Designing an application that requires graph manipulation, frequently requires to see the graph displayed in its smallest forms: to observe how the pixels interact with each other, how close they are, how the software is dealing with them. Most of the time, by direct naked eye observation, you get the information needed, except in those individual cases when it is necessary to get a slightly closer look.

Introduction

Writing graphics processing software has occupied a substantial part of my engineering work. I find it to be enjoyable work. Frequently, I need to amplify pixels painted on the screen. There are magnifiers available, and I have used some with satisfying results. But I wanted a small enough object that I could re-use on my other applications, one over which I would have a bit more control.

The second reason for my desire to write this code is trying to use CMemdDC, by Keith Rule, in a dialog. I have previously used CMemDC on a CScrollView::OnDraw() with great results. Using it on a dialog presented a bit of a problem, one that I could not solve at the time, and had left this work for another day.

The third reason is that I wanted to use a modeless dialog from within a View environment. Since I had not used this mechanism before, this seemed a good place to investigate.

Most importantly, I thought this would be a fun project to undertake.

CMagnifierDlg

The operation of the magnifier resides on a modeless dialog, a class I have named CMagnifierDlg.

CMagnifierDlg magnifies a portion of the graph by drawing the representation of one pixel into a rectangle, a "cell." The larger the cell, the higher the magnification. A combination of a group of cells is placed in a control within the dialog. The dialog allows to zoom in by simply displaying larger cells for each pixel.

Since this process will reside on a modeless dialog, I modified the dialog in two ways:

  1. Eliminating the "OK" and "Cancel" buttons on the dialog.
  2. On the "OnCancel" function, I placed a call to ShowWindow(SW_HIDE); to put it out of the way yet ready to be recalled again.

When the user needs to re-activate the magnifier, CScrollView calls m_MagnifierDlg.ShowWindow(SW_SHOW);

When the user clicks on the observed window surface, CMagnifierAppView places a call to the dialog, m_MagnifierDlg.vSetMagnificationPoint(); indicating the new location to enlarge.

Using the Code

In the CSrollView class, I instantiated a CMagnifierDlg object. The dialog will not be operational until you "create" it. When the user clicks on the magnifier button, the code "creates" the dialog, makes a call to the dialog to Create();, and now the dialog comes to life. From then on, the CScrollView will need to communicate the magnifier the location the user has clicked, for a new magnification display.

C++
//
// Magnifier AppView.h
class CMagnifierAppView : public CScrollView
{
…
CMagnifierDlg m_MagnifierDlg;
…
};

//
// Magnifier AppView.cpp 
void CMagnifierAppView::OnButtonEmbeddedDialog() 
{
	if (!m_MagnifierDlg.bDialogWasCreated())
    {
		m_MagnifierDlg.Create(this);
	}
	else
	{
		m_MagnifierDlg.ShowWindow(SW_SHOW);
	}
}

void CMagnifierAppView::OnInitialUpdate() 
{
	CMagnifierAppDoc* pDoc = GetDocument();
	CScrollView::OnInitialUpdate();
	
	CString csFilePath = pDoc->GetPathName();
	if (!csFilePath.IsEmpty())
	{
		if (bReadBMPFile(csFilePath))
			SetScrollSizes( MM_TEXT, CSize(m_bih.biWidth, m_bih.biHeight));
	}
	else
	{
	   SetScrollSizes( MM_TEXT, CSize( 1, 1 ) );
	}

    InvalidateRect(NULL);
}

void CMagnifierAppView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CPoint ptUserClicked = point;
	ptUserClicked.x += GetScrollPos(SB_HORZ);
	ptUserClicked.y += GetScrollPos(SB_VERT);
	m_MagnifierDlg.vSetMagnificationPoint(ptUserClicked);
	
	CScrollView::OnLButtonDown(nFlags, point);
}

The function bool CMagnifierAppView::bReadBMPFile(…) loads the data into a memory buffer from where to paint the graph.

Conclusion and Points of Interest

In this sample, we have shown a simple way to create a magnification of a graph. We have used CMemDC on a dialog, a modeless dialog and we have instantiated the modeless dialog on a CScrollView.

History

  • 8th June, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
QuestionPics? Pin
David Crow15-Jun-20 5:32
David Crow15-Jun-20 5:32 
QuestionApp Crashes Pin
jung-kreidler9-Jun-20 0:22
jung-kreidler9-Jun-20 0:22 
AnswerRe: App Crashes Pin
Manuel Campos9-Jun-20 6:49
Manuel Campos9-Jun-20 6:49 

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.