Solved

another windows capture quesion

Posted on 2007-12-05
9
462 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
proper way to parse text with delphi 7 119
Virtuailstring tree compare node issue 14 116
Delphi XE10 Round Image 2 120
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

8 Experts available now in Live!

Get 1:1 Help Now