Solved

another windows capture quesion

Posted on 2007-12-05
9
460 Views
Last Modified: 2010-04-04
hi experts,

need an function to get a bitmap of an desktop window from my delphi application.
the problem is somtimes the windows is overlapt or a part of. and i still need the picture out of that.

i create a windows preview like in vista.
0
Comment
Question by:dMa
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:CodedK
ID: 20411044
Hi

In general there is no problem to capture a hidden OR partially hidden window.
The problem appears when its minimized !
Read this :
http://www.codeproject.com/useritems/CapturingMinimizedWindow.asp
This article says how you can grab a window that is minimized.

Now about your problem the trick is to bring it in front and then put it back as it was...


use

SetWindowPos(WndHandle,HWND_TOPMOST,0,0,500,200,SWP_SHOWWINDOW or SWP_NOSIZE);

OR

SetForegroundWindow(handle);
 
 

then use the grab function
 

Hope this helps.

Open in new window

0
 
LVL 16

Accepted Solution

by:
CodedK earned 300 total points
ID: 20411063
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20411540
As CodedK mentions, bring your window to the forefront and do the form copy. Here si one way to bring the application to the forefront or bring it active if hidden or minimized...

var
  WindowHandle : HWND;

function EnumWindowsProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall;
const
  MAX_WINDOW_NAME_LEN = 80;
var
  WindowName : array[0..MAX_WINDOW_NAME_LEN] of char;
begin
  {Can't test GetWindowText's return value since some windows don't have a title}
  GetWindowText(WHandle,WindowName,MAX_WINDOW_NAME_LEN);
  Result := (StrLIComp(WindowName,PChar(lParam), StrLen(PChar(lParam))) <> 0);
  If (not Result) then WindowHandle:=WHandle;
end;

function AppActivate(WindowName : PChar) : boolean;
begin
  try
    Result:=true;
    WindowHandle:=FindWindow(nil,WindowName);
    If (WindowHandle=0) then EnumWindows(@EnumWindowsProc,Integer(PChar(WindowName)));
    If (WindowHandle<>0) then begin
      SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
      SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
    end else Result:=false;
  except
    on Exception do Result:=false;
  end;
end;

end.

Use AppActivate('WindowName');

WindowName is the text in the caption or a portion of it.

Now take your window snapshot.

In rereading your question, are you wanting to make a bmp of the entire desktop? If so and you have a window overlapping another, that's how the snapshot will be. Exactly what you see on the desktop unless you move one of the overlapping windows. What you see is what you'll get... WYSIWYG!

0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20411586
I am not certain if you now want to capture that window, so here is some code that simulates the capture screes, puts it in the clipboard then into a TImage.

Uses
  ClipBrd;

procedure CopyActiveWindow;
begin
  keybd_event(VK_SNAPSHOT, 1, 0, 0);
  Application.ProcessMessages;
  Image1.AutoSize := True;
  Image1.Picture.Bitmap.Assign(Clipboard);
end;

John
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:dMa
ID: 20411698
no sorry.... there is a problem.

to CodedK:
1. delphi do not support the printwindow command.

to Johnjces:
2. thats a bad hack just becouse of events thats are runnign in the time you bring the window to front
for example onactivate...

but ok i could live with that if
1. it's fast enougth so the user did't see it

2.(i cann live with it to ...)  i do not loos the focus for the user (for example i runn it every 2 secound and user trys to write somthing in word the same time :)  )


3. AND THE MOST IMPORTAND THINK is
show me a code thats brings me the window back to the same positions it was before, so hidden is not the problem but if it was just in background of some over windows.....


0
 
LVL 2

Author Comment

by:dMa
ID: 20411712
to CodedK:
1. delphi do not support the printwindow command.

i mean it do not support delphi forms
0
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 200 total points
ID: 20411889
dMa.

One simple way here would be to, after you take your picture, make the other window the active one using the Appactivate.

But in any event the user is going to see something happen. A flicker at least.

If this is your delphi application that you need an image of, you could do something like a Form.GetFormImage and capture its image.

John

0
 
LVL 16

Expert Comment

by:CodedK
ID: 20412480
I know delphi does not support this.
But i gave you the link to see the example exe file that brings up all the windows and captures them.
Test it while running your sensitive code to check if there is any issue like the one you fear with onactivate. You want even notice the windows...
I didnt give you a capture code coz i thought you are allready running yours.
0
 
LVL 16

Expert Comment

by:CodedK
ID: 20412516
I've tested the full source code posted in CodeProject.
The tricks mentioned there.. We can implement this easily in Delphi.
I dont thing you need to mess with the registry section because you didnt mentioned minimized windows...
Also duringthe test there was no flicker :) ... nothing.
Just test it.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now