Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic
Tip/Trick

A utility to measure screen area in pixels

Rate me:
Please Sign up or sign in to vote.
4.06/5 (5 votes)
30 Apr 2013CPOL 43.5K   1.2K   31   8
If you want to measure something on the screen, here is a utility.

Screenshot - PixelMeter.jpg

Introduction

This is an utility with which you can measure an area on the desktop. It can be useful if you, for example, want to know the size of a picture.

Using the Code

The application consists of two windows: the "measurer" and then another that displays the current size of the measuring area.

The measuring window can be resized by dragging the borders and corners.

It can be moved by gripping the tiny red line with the mouse and dragging it around. Since it is only 1 pixel wide, a steady hand is needed.

The code for this is:

VB
If e.Button = MouseButtons.Left Then
   ReleaseCapture()
   SendMessage(CType(Me.Handle.ToInt32, IntPtr), _
               WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If

It can also be moved by the arrow keys. The window gets into "keyboard move mode" by right clicking the brown border. The code for this is adapted from a VB6 version I did years ago: http://www.vb-helper.com/howto_move_form_with_arrows.html.

VB
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  Dim skip_it As Boolean
  If m.Msg = WM_NCRBUTTONDOWN Then
     Select Case m.WParam.ToInt32
        Case HTBORDER, HTBOTTOM, _
          HTBOTTOMLEFT, HTBOTTOMRIGHT, _
          HTLEFT, HTRIGHT, HTTOP, _
          HTTOPLEFT, HTTOPRIGHT, HTGROWBOX
           ' Move the form.
           SendMessage(Me.Handle, WM_SYSCOMMAND, SC_MOVE, 0&)
           skip_it = True
     End Select
  End If
  If Not skip_it Then ' Call the original WindowProc.
     MyBase.WndProc(m)
  End If
End Sub

Points of Interest

The viewport creation is done like this:

VB
Dim hRgn As IntPtr
Dim hRgnClient As IntPtr

'forms dimensions
lFormWidthPixels = Me.Width
lFormHeightPixels = Me.Height

'get border and title thickness
mlBorderThickness = CInt((lFormWidthPixels - Me.ClientSize.Width) / 2)
mlTitleThickness = CInt((Me.lFormHeightPixels - _
                   Me.ClientSize.Height)) - (2 * mlBorderThickness)

'get viewport dimensions 
lViewportWidthPixels = lFormWidthPixels - (2 * mlBorderThickness)
lViewportHeightPixels = lFormHeightPixels - _
                        mlTitleThickness - (2 * mlBorderThickness)

'get handle
hRgn = CreateRectRgn(0, 0, lFormWidthPixels, lFormHeightPixels)
hRgnClient = _
CreateRectRgn(mlBorderThickness + 1, mlBorderThickness + 1, _
Me.ClientSize.Width + mlBorderThickness - 1, _
Me.ClientSize.Height + mlBorderThickness - 1)

'combine regions
CombineRgn(hRgn, hRgn, hRgnClient, RGN_DIFF)
DeleteObject(hRgnClient)

'set region
SetWindowRgn(Me.Handle, hRgn, -1)
DeleteObject(hRgn)

License

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


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

Comments and Discussions

 
QuestionCool but I would like a lasoo for measuring the area of non rectangular shapes Pin
timtak30-May-17 14:54
timtak30-May-17 14:54 
GeneralMy vote of 5 Pin
Charles Patton4-Nov-15 8:00
Charles Patton4-Nov-15 8:00 
QuestionShould be a utility Pin
i0029-Apr-13 20:00
i0029-Apr-13 20:00 
AnswerRe: Should be a utility Pin
i003-May-13 3:40
i003-May-13 3:40 
QuestionHow would you change reading a single pixel to a larger area? Pin
jrdnoland23-May-12 22:45
jrdnoland23-May-12 22:45 
Is it possible to change the color reading area to a much larger area? Was thinking something like this would help in comparing the relative colors densities of two different color areas.

What portion of the code controls that part of the program?

Thanks - nice programming effort!
QuestionBuilt Version?? Pin
kevdelkevdel12-Oct-07 9:27
kevdelkevdel12-Oct-07 9:27 
AnswerRe: Built Version?? Pin
Michael Rosqvist14-Oct-07 20:52
Michael Rosqvist14-Oct-07 20:52 

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.