Click here to Skip to main content
15,868,306 members
Articles / Programming Languages / C++
Article

A better Zoomin utility

Rate me:
Please Sign up or sign in to vote.
4.93/5 (32 votes)
17 Jun 20054 min read 81K   1.2K   56   19
A better Zoomin utility.

Introduction

This project came about as the result of a question in one of the newsgroups. Someone was asking about how to write a color-picker.

Far too many times, I've needed a color-picker. I've also needed to capture pieces of images on the screen to put into other programs, such as icons. The problem is that ZoomIn doesn't support most of what I need to do.

What's wrong with Zoomin?

  • I have to get the image right the first time. If I don't have it selected properly, I can't nudge it over.
  • I have to get the scaling right. Otherwise I might get too much of the image, or not enough.
  • Once having gotten the image, I can't select a piece of it.
  • Copy-to-clipboard copies the image in the Zoomin window, not the actual pixels that were on the screen..

The original questioner had posed some complicated ways of dealing with the problem, such as using timers and hook functions. These aren't necessary.

This project produces an image much like the one below:

Image 1

In order to create this, I set the image size, controlled by the spin control at the top, to 64 pixels. I then took the picker tool, Image 2, and dragged it over the screen until I was over the light bulb in the heading of a page. I wasn't quite where I wanted, so I used the "nudge" arrows to move it around a bit.

While moving around, there is an RGB value displayed at the top right. In addition, the actual R, G and B colors are shown. This represents the actual point where the cursor is placed (although you probably can't see it, there is a little tiny white dot in the middle of the picker tool that represents the cursor hotspot position). The way this is seen on the expanded image is that the dotted square in the center is the hotspot, expanded. The lines extending from the edges are just to make it interesting to find.

The ? key in the middle will display a box around the area that is selected.

The copy icon button, Image 3, will copy the contents of the selection to the clipboard.

However, there are additional features.

Once the image is captured, moving the mouse over it will put into the RGB display the actual pixel value for the pixel under the mouse.

Clicking and dragging within the image area will select a sub-region. This will then be marked with a selection rectangle. Note that the old rectangle does not disappear until the new one is created. This is deliberate, so you can see the old selection while making a new, larger or smaller, selection.

This is shown in the figure below. Note that this image is smaller. I did that by resizing the dialog. In this example, there is a selection box around the base of the lamp.

What's interesting about this code?

Like most of my examples, this is intended to show some ideas about using Windows and/or MFC. Ideas that are included in this code are:

Image 4

  • Creation of a window DC that covers the entire desktop window.
  • Grabbing a piece of the window and saving it as a local bitmap.
  • Selecting a piece of that bitmap.
  • Rubber-banding selection box.
  • Displaying color swatches.
  • Copying a bitmap to the clipboard.
  • Having a "picker" tool (much like you would recognize from Spy++).
  • Noting that you can in some cases interchange cursors and icons, which is what I did here.
  • Creating a CWnd-derived popup window (the implementation of the ? button).
  • Having a window that can be created and which will disappear on its own (the ? button's display window).
  • The use of the implicit SetBkMode(OPAQUE) to create lines that are alternating colors, so the line shows up whether the background is light or dark.
  • Computing points in logical space and translating them to device space (the array of points used to draw the highlighting square in the first image). Note that the horizontal and vertical lines are actually a "fractional pixel" in logical space, and there is a trick to accomplishing that.
  • Owner-draw buttons (those arrow buttons, and the copy button).

There isn't much code here, but the code that is here illustrates several useful techniques.

The views expressed in this essay are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

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
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
GeneralAnother Zoom program Pin
Lee Alexander23-Jun-05 2:54
Lee Alexander23-Jun-05 2:54 
GeneralAnd another Zoomin... Pin
Anonymous21-Jun-05 6:59
Anonymous21-Jun-05 6:59 
GeneralRe: And another Zoomin... Pin
Joseph M. Newcomer21-Jun-05 7:06
Joseph M. Newcomer21-Jun-05 7:06 
GeneralDoes it work with MediaPlayer Pin
Uwe Keim17-Jun-05 11:04
sitebuilderUwe Keim17-Jun-05 11:04 
GeneralRe: Does it work with MediaPlayer Pin
Joel Holdsworth17-Jun-05 13:02
Joel Holdsworth17-Jun-05 13:02 
GeneralRe: Does it work with MediaPlayer Pin
Toby Opferman24-Jun-05 11:52
Toby Opferman24-Jun-05 11:52 
GeneralRe: Does it work with MediaPlayer Pin
Joseph M. Newcomer24-Jun-05 15:36
Joseph M. Newcomer24-Jun-05 15:36 
GeneralRe: Does it work with MediaPlayer Pin
Toby Opferman24-Jun-05 19:19
Toby Opferman24-Jun-05 19:19 
GeneralRe: Does it work with MediaPlayer Pin
Joseph M. Newcomer17-Jun-05 18:00
Joseph M. Newcomer17-Jun-05 18:00 
GeneralRe: Does it work with MediaPlayer Pin
Rage20-Jun-05 5:50
professionalRage20-Jun-05 5:50 
GeneralRe: Does it work with MediaPlayer Pin
Michael Dunn22-Jul-05 12:44
sitebuilderMichael Dunn22-Jul-05 12:44 
AnswerRe: Does it work with MediaPlayer [modified] Pin
Synetech12-Aug-06 12:12
Synetech12-Aug-06 12:12 
GeneralAdditional ZoomIn Pin
Blake Miller17-Jun-05 4:03
Blake Miller17-Jun-05 4:03 
GeneralRe: Additional ZoomIn Pin
Joseph M. Newcomer17-Jun-05 6:17
Joseph M. Newcomer17-Jun-05 6:17 
GeneralRe: Additional ZoomIn Pin
Blake Miller17-Jun-05 6:36
Blake Miller17-Jun-05 6:36 
GeneralNice! Pin
ThatsAlok17-Jun-05 2:25
ThatsAlok17-Jun-05 2:25 
GeneralRe: Nice! Pin
Stlan17-Jun-05 3:04
Stlan17-Jun-05 3:04 
GeneralRe: Nice! Pin
Stlan17-Jun-05 3:11
Stlan17-Jun-05 3:11 
GeneralRe: Nice! Pin
ThatsAlok17-Jun-05 19:34
ThatsAlok17-Jun-05 19:34 

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.