Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Capturing of the assigned area with mouse button eventFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
System
Skill Level:
Scoring:
Last Update:
06/12/2008
Search Keys:
delphi delphi3000 article borland vcl code-snippet Mouse, screen capture, WH_JOURNALRECORD, WM_CANCELJOURNAL
Times Scored:
8
Visits:
1311
Uploader: Lutfi Baran
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to capture assigned area with mouse button event?
Answer:



if you want to capture on any desktop area by clicking mouse left button in its client area then add two TImage, TTimer, TLabel, TCheckBox and TButton components to the form.

Here is code;
-------------

unit Unit1;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
     AppEvnts, Buttons, Jpeg, StdCtrls, ExtCtrls;  

type
  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Button1: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    CheckBox1: TCheckBox;
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
     MHookGo : Boolean;
  public
  end;

var
  Form1: TForm1;
  MHook: THandle;

implementation

{$R *.DFM}

function JournalProc(Code, wParam: Integer; var EventW: TEventMsg): Integer; stdcall;
label go, jump;
var
  s, tm: string;
  JPGFile: TJPEGImage;
  a1, p: integer;
  Ad, s1, s2, s3: string;
begin
  Result := CallNextHookEx(MHook, Code, wParam, Longint(@EventW));

  if Code < 0 then Exit;
  if Code = HC_SYSMODALON then Exit;
  if Code = HC_ACTION then
  begin
    s := '';
    if EventW.message = WM_LBUTTONDOWN then // mouse left button pressed
    begin
      JPGFile := TJPEGImage.Create;
      s := 'Mouse coordinates:   X: '
            + IntToStr(EventW.paramL)
            + '  Y: ' + IntToStr(EventW.paramH);

      Form1.image2.Picture := Form1.Image1.Picture;
      if Form1.CheckBox1.Checked = True then
      begin
        JPGFile.Assign(Form1.Image2.Picture.Bitmap);
        { as a file name will use current time.
          Therefore, For file name, converting : character to . character }
        tm := DateTimeToStr(Now);
        go:
        Ad := tm;
        a1 := Length(Ad);
        p := Pos(':', Ad);
        if p = 0 then
        begin
          goto jump;
        end
          else
            begin
             s1 := Copy(Ad, 1, p);
             s2 := Copy(Ad, (p + 1), (a1 - p));
            end;
        s1 := Copy(Ad, 1, p - 1);
        s3 := s1 + '.' + s2;
        tm :=  s3;
        goto go;
        jump:
        JPGFile.SaveToFile(s3 + '.jpg'); // saved into the same folder
      end;
    end;
    if s <> '' then Form1.Label1.Caption := s;
  end;
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
begin
  // if Ctrl-Alt-Del or Ctrl-Esc pressed then journal hook will be disable
  Handled := False;
  if (Msg.message = WM_CANCELJOURNAL) and MHookGo then
      MHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, 0, 0);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var Asn1, Asn2, Asn3: TRect;
    aWidth, aHeight: Integer;
    aMusX, aMusY: Real;
    Cv: TCanvas;
    Crs: TPoint;
begin
  if not IsIconic(Application.Handle) then
  begin
    GetCursorPos(Crs);
          Asn3 := Rect(Form1.Left,Form1.Top, Form1.Left
                          + Form1.Width, Form1.Top
                          + Form1.Height);

    if not PtInRect(Asn3, Crs) then
    begin
      aWidth := Image1.Width;
      aHeight := Image1.Height;
      Asn2 := Rect(0, 0, aWidth, aHeight);

      // captured area proportions - you can change it
      aMusX := aWidth / (1 * 2);
      aMusY := aHeight / (1 * 2);

      Asn1 := Rect(Crs.x, Crs.y, Crs.x, Crs.y);
      InflateRect(Asn1, Round(aMusX), Round(aMusY));

      if Asn1.Left < 0 then OffsetRect(Asn1, - Asn1.Left, 0);
      if Asn1.Top < 0 then OffsetRect(Asn1, 0, - Asn1.Top);
      if Asn1.Right > Screen.Width then
         OffsetRect(Asn1, - (Asn1.Right - Screen.Width), 0);
      if Asn1.Bottom > Screen.Height then
         OffsetRect(Asn1, 0, - (Asn1.Bottom - Screen.Height));

      Cv := TCanvas.Create;
      try
        Cv.Handle := GetDC(GetDesktopWindow);
        Image1.Canvas.CopyRect(Asn2, Cv, Asn1);
      finally
        Cv.Free;
      end;
      Application.ProcessMessages;
    end;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // unhook closing
  if MHookGo then UnhookWindowsHookEx(MHook);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := 'Mouse coordinates:';
  CheckBox1.Caption := 'Start for Capture area';
  if MHookGo then Exit; // Mouse Journaled absent
  MHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0);
  // Hook starting
  if MHook > 0 then MHookGo := True
     else Exit; // Journal Hook absent
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // disable hook
  MHookGo := False;
  UnhookWindowsHookEx(MHook);
  MHook := 0;
  Label1.Caption := '';
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
I. Siticov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)