Click here to Skip to main content
6,634,665 members and growing! (16,479 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

An utility to measure a screenarea in pixels.

By kurtsune

If you want to measure something on the screen, here is an utility.
VB 8.0, Windows, .NET 2.0VS2005, Dev
Posted:12 Oct 2007
Views:10,839
Bookmarked:15 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.75 Rating: 3.67 out of 5

1
1 vote, 33.3%
2

3
1 vote, 33.3%
4
1 vote, 33.3%
5
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 2 windows, one is the "measurer" and the other displays the current size of the measuring area.

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

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

The code for this is:

      
      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 rightclicking 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.

 
   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

I would be very grateful for suggestions of improvement here.

Points of Interest

The viewport creating is like this:

 
      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)

I would be very grateful for hints on how to do this in a more "nettish way.

History

--

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

About the Author

kurtsune


Member

Occupation: Web Developer
Location: Sweden Sweden

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralBuilt Version?? Pinmemberkevdelkevdel10:27 12 Oct '07  
GeneralRe: Built Version?? Pinmemberkurtsune21:52 14 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2007
Editor:
Copyright 2007 by kurtsune
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project