VB Mania

Text Box: Previous Example
Text Box: Next Example
Text Box: VB-Mania Core

Visualize - Monitor your Desktop

Information:

In this example, you will be able to have a small copy version of your desktop that can be used to visualize whatever changes happen. You will figure out many ways how to use it ranging from simple programs to more complicated ones that can let you visualize the activity on other computers.

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

 

Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long

 

Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

 

Private Desk_hDC As Long

 

Private Sub Form_Load()

New_DC

End Sub

 

Private Sub Timer1_Timer()

DoEvents

StretchBlt Form1.Picture1.hdc, 0, 0, Form1.Picture1.Width, Form1.Picture1.Height, Desk_hDC, 0, 0, Screen.Width, Screen.Height, vbSrcCopy

End Sub

 

Private Sub New_DC()

'Is a Desktop(Hwnd)

 Desk_hDC = GetDC(0)

' Set the Picture1 to HALFTONE

SetStretchBltMode Form1.Picture1.hdc, 4

End Sub

Illustration:

 

What You Need?:

1. One PictureBox named: Picture1

2. One Timer: named Timer1 (Enabled = true, Interval = 25)

Code:

 

Explanation:

Any commands and procedures written under Form_Load will be executed, as the name implies, whenever the form is loaded into the memory. In this example, we created a procedure called named Private Sub New_DC() that will get the get the handle display device context (here it is the whole desktop) and saved to Desk_hDC to be dictated to the private function stretchblt. stretchblt will now take a picture of the specific window and compress or stretch it into picture1 to be viewed. Being in a timer with an interval of 25, this will be repeated every 25 millisecond to give a smooth picture of what is going on the window that is viewed.

Hint:

Surely, you have figured out many uses of this example. You may use it in a program that monitor the activity on your computer by designing a program that take a screen-shots every specific time and save them to viewed later.

VB-Pedia:

· Timer: a member of VB Library that can execute code at regular intervals by causing a Timer event

Properties used:

¨ Property Interval as Long

It returns/sets the number of milliseconds between calls to a Timer control's Timer event.

 

· getDC: a private function of the windows Library "user32" that retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen

Parameters:

¨ hWnd

A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen.

 

· SetStretchBltMode: a private function of the windows Library "gdi32" that sets the bitmap stretching mode in the specified device context

Parameters

¨ hdc

It is the handle to the device context.

¨ iStretchMode

it Specifies the stretching mode. We used the HALFTONE mode which maps pixels from the source rectangle into blocks of pixels in the destination rectangle. The average color over the destination block of pixels approximates the color of the source pixels.

 

· Stretchblt: a private function of the windows Library "gdi32" that copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.

Parameters:

¨ hdcDest

it is the handle to the destination device context.

¨ nXOriginDest

Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.

¨ nYOriginDest

Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.

¨ nWidthDest

Specifies the width, in logical units, of the destination rectangle.

¨ nHeightDest

Specifies the height, in logical units, of the destination rectangle.

¨ hdcSrc

Handle to the source device context.

¨ nXOriginSrc

Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.

¨ nYOriginSrc

Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.

¨ nWidthSrc

Specifies the width, in logical units, of the source rectangle.

¨ nHeightSrc

Specifies the height, in logical units, of the source rectangle.

¨ dwRop

Specifies the raster operation to be performed. Raster operation codes define how the system combines colors in output operations that involve a brush, a source bitmap, and a destination bitmap.

Visualize Your Desktop

Sponsored Links

Google
 
Web vbmania2006.brinkster.net
planet-source-code.com support.microsoft.com