Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if Form is obscured.

Status
Not open for further replies.

ROGERDODGE

IS-IT--Management
Aug 13, 2003
40
GB
Hi
I am developing an application in C# .NET which needs to create a bitmap image of a Windows Form, save it to disk and then do some OCR recognition on parts of that screen shot. This is fine and I can do it.
If the window is obscured by another one, then my screen shot may be invalid for the OCR recognition process.
I need to find a way therefore, to determine if some of the window is obscured by another.
I know the Window Handle (IntPtr), its location on the desktop (x,y) and the window size (as RECT)
Any help or inspiration would be gratefully received.

regards

RogerDodge

 
Is the Windows Form a form you've created in C#?

If so how are you creating the bitmap of the form (and I assume, it's controls)?

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
>needs to create a bitmap image of a Windows Form

How are you doing that?
 
The form which I need to Capture is from another application, I have no control over it programmatically.

Essentially, I am trying to "scrape" information from the form to then do some look-ups in a database from predetermined areas (fields) on the form.

I have set up some code within a "dummy" form, so when I click a button, I call this code which then gets the handle of itself and then captures the screen from its position, this works fine.


Code:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

private void cmdOptions_Click(object sender, EventArgs e)
{
RECT rct;
HandleRef lcHnd = new HandleRef(this, this.Handle);
if (!GetWindowRect((IntPtr)lcHnd, out rct))
{
    MessageBox.Show("ERROR");
}
else
{
    int windowWidth = System.Math.Abs(rct.Right - rct.Left);
    int windowHeight = System.Math.Abs(rct.Bottom - rct.Top);
    System.Drawing.Bitmap currBMP = new Bitmap(windowWidth, windowHeight);
    currBMP = CaptureClip(rct.Left,rct.Top, windowWidth, windowHeight);
                currBMP.Save("C:\\OCRSDK\\CaptBMP.bmp");
}
}

The CaptureClip function just "grabs" the defined area from the screen.

I have been exploring the possibilities of using "WindowFromPoint" and "PtInRect" API functions to try to figure something out.

RogerDodge
 
You might want to use the PrintWindow function which captures the window easily and works even if the window is obscured by other windows.
 
>The CaptureClip function just "grabs" the defined area from the screen

It is how the grabbing is being done that I was asking about. Unfortunately what you have posted show this. And I am not familiar with the CaptureClip function. Is this something that you wrote? And if so, can we see it please?
 
I'll try the PrintWindow tomorrow when I'm back @ work - thanks.

strongm - here's the code (for clarity's sake)
(thanks to Danny Battison)

Code:
public static System.Drawing.Bitmap CaptureClip(int x, int y, int width, int height)
{
	System.Drawing.Bitmap BMP = new Bitmap(width, height, 
	                                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);
	System.Drawing.Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
	GFX.CopyFromScreen (x, y, 0, 0, new Size(width, height),
	                    System.Drawing.CopyPixelOperation.SourceCopy);
	
	return BMP;			
}

I'm sure I can tidy this code up a bit, but for the moment I just need the principles to work.

My main issue at the moment is to capture the hidden window (normally it wont be hidden but I cant be certain so I need to either trap it or not care - hopefully the PrintWindow will solve it.



 
Mike - I think I may have misunderstood your question. Being more awake! and looking at the links under your post, it may be that we are looking at the same underlying issues.
The application(s) which I am trying to "screen scrape" have all (as far as I can make out) been developed with Foxpro9. As a consequence, it seems that I am unable to enumerate any objects on the child forms. In some applications, the relevant information which I am trying to obtain, ie client reference number, is present on the main window title text - I can then just use this information to do a lookup of the appropriate database record.
I will be interested to hear from you.

 
I should try to solve this problem on two ways:

Because your CaptureClip-function use the graphic handle of the console-screen (I actually more guess it then I known) I should make/force the window as Top-Level-Window during the capture period.

Or:

I would try to give a repaint-command of the window that you like to capture, but to the canvas of an bitmap instead of those of the Form.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top