Click here to Skip to main content
Click here to Skip to main content

Window's Region User Mode Monitoring

, , 4 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes the method to perform user mode region monitoring of the specified window.

Introduction

In this article, we will describe the method to perform user mode region monitoring of the specified window.

This article will be useful for software developers who need run-time monitoring information about specified window position and its region on Windows OS.

General Description

A region is a rectangle, polygon, or ellipse (or a combination of two or more of these shapes) that can be filled, painted, inverted, framed, and used to perform hit testing (testing for the cursor location). In general, the window's region is the set of the rectangles that construct a visible part of the window.

The specified window's region monitoring, for example, can be used in Virtual channels development.

Virtual channels are software extensions that can be used to add functional enhancements to a Remote Desktop Services application. Examples of functional enhancements may include support of the special types of hardware, audio, or other additions to the core functionality provided by the Remote Desktop Services Remote Desktop Protocol (RDP).

To use virtual channels, you should provide the server-side and client-side modules of the virtual channels application. The server-side module can be a user-mode application or a kernel-mode driver. The client-side module should be a DLL.

Client side module as a rule is mstsc add-in. Sometimes such application needs to perform some action at the window's region changes. So we can monitor the server window and send information about its region to the client. On the client we can use the information to get visible parts of the server window.

For example, we need to improve video rendering in the terminal session. We can create some optimal scheme for data transporting from the server to the client and show the movie window on the client side instead of the server. This technique allows user to watch movie as on the local machine. The client video window should be drawn over the server one. But if the server window is partially hidden by some other windows, we should update our client window.

In this article, we present user adopted library to get run-time information about the window region.

Library Description

Input information: Handle of the window to monitor, time out, observers to hold region and errors notifications.

Output information: Region data structure RGNDATA, errors reports.

The separate thread starts in the library core. According to timeout, it collects information about the window's region and sends it to the observer. If we get some problems in the thread, we use NotifyZero() method to send zero size region. Windows region APIs were used to collect this information (see references). Main steps are shown below:

  1. Get device context:
    HDC hDC =  ::GetDC(m_window);
  2. Create empty region:
    HRGN hRgn =  ::CreateRectRgn(0, 0, 0, 0);
  3. Copy region of the selected window:
    ::GetRandomRgn(hDC,  hRgn, SYSRGN);
  4. Determine the size of region data:
    DWORD size =  GetRegionData(hRgn, 0, 0);
  5. Reserve buffer:
    std::vector<char>  rgnDataBuf(size);
  6. Get region data:
    RGNDATA  * pRgnData = 
        reinterpret_cast<RGNDATA*>(&rgnDataBuf.front());
    GetRegionData(hRgn,  size, pRgnData)
  7. Notify client via observer:
    pEventObserver->WinEventNotify(pRgnData);

Public interface description

Callback interface to send region data to the client code:

struct IWinRegionObserver
{
    virtual ~IWinEventObserver(){}
    virtual void WinEventNotify(const _RGNDATA *) = 0;
};

Callback interface to inform about errors:

struct IErrorObserver
{
    virtual ~IWinEventErrorObserver(){}
    virtual void OnWinEventError(__in const char *) = 0;
};

Restrictions

This method doesn't work on Windows 7 and higher systems if aero theme is selected.

In aero theme, window region always consists of one rectangle, even if a window is partially hidden by other windows. Windows 7 uses its core mechanism to show only visible parts. So the library will always report about one rectangle in the window region.

How To Use

To use this library, you should create core object and events observers. To create the core, use the next constructor:

CWinRegionObserver(
			HWND hWnd,
			IWinRegionObserver * pEventObserver,
			IErrorObserver* pErrorObserver,
			DWORD TimeOut = 200
			);

All data will be sent to the application by IWinRegionObserver and IErrorObserver interfaces.

In your client code, you can parse region information to determine visible parts of the window, for example. In our test application, we just write the number of rectangles, which construct the window region, to the log file.

Sample Description

MFC dialog-based application was created to demonstrate the library work. The main window of this application is monitored by the library. To see monitoring results, use log file in the sample folder.
For example, let’s suppose that we have a window.

We will use our library to monitor its region rectangles.

img_1.jpg

In this image, the visible region of the window consists of 1 rectangle.

In the next image, the region still consists of 1 rectangle.

img_2.jpg

But in the next image, the window region consists of 2 regions.

img_3.jpg

So, using this library, we can find visible regions and use this information in your program.

Library Build Requirements

Software

Environment Variables

  • BOOST_ROOT - should contain the path to the Boost directory

Preparing Build System

Before building the sample solution from the source code, you should perform several simple preliminary steps. First of all, you should install all the applications specified in the Software section, and set mentioned environment variables.

After that, you should build Boost libraries from the source code. To do this, go to the Boost directory (BOOST_ROOT) and invoke the following two commands one after another:

  bootstrap.bat
  bjam.exe toolset=msvc --build-type=complete

For more detailed information, see the Boost documentation.

Links

History

  • 4th November, 2010: Initial post

License

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

Share

About the Authors

Sergey Popenko
Software Developer (Junior) ApriorIT
Ukraine Ukraine
Sergey Popenko.
22 years old.
The Driver Team`s software developer.
Master of the Applied Math faculty, the Dnipropetrovsk National University, Ukraine.

Apriorit Inc
Apriorit Inc.
Ukraine Ukraine
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.
 
Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.
 
Official site http://www.apriorit.com
Group type: Organisation

31 members

Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Apr-12 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Nov 2010
Article Copyright 2010 by Sergey Popenko, Apriorit Inc
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid