Link to home
Start Free TrialLog in
Avatar of Murdoc
Murdoc

asked on

Canvas and thread problem :)

Hi

I'm trying to develope simple application that will simulate scanning on the monitor display. i did it allready but if i'm checkin CPU consumption while programm is running i'm getting headache  
(70 to 80 %) ......any ideas ??

The code:

--------------------------thread---------------------------------
procedure obraz.xx(k:integer);
var cn:Tcanvas;
tb:Tbitmap;
w,h,i,t:integer;
begin
cn:=tcanvas.Create;
Cn.Handle:=GetWindowDC(GetDesktopWindow);
w:= Screen.Width;
h:= Screen.Height;
        tb:=tbitmap.Create;
        tb.Height:=h;
        tb.width:=w;
        tb.Canvas.Brush.Color:=clblack;
        tb.Canvas.pen.Color:=clwhite;
        tb.canvas.FillRect(rect(0,0,w,h));
        tb.canvas.Pen.Width:=10;
        tb.canvas.MoveTo(k,0);
        tb.canvas.lineto(k,h);
        Cn.Draw(0,0,tb);
        tb.Free;
        cn.Free;
        if t=w then
        begin
        ShowCursor(true);
        //cn.Free;
        UpdateWindow(GetDesktopWindow);
        terminate;
        end;
        end;


end.
--------------------end of thread ----------------------


..and in main listening:


procedure TForm1.timerTimer(Sender: TObject);
var
a:integer;
fv:array [0..2] of obraz;
begin

inc(s);
if s<screen.Width then
begin
a:=1;
//fv.FreeOnTerminate:=true;
fv[0].xx(s);
inc(s);
fv[1].xx(s);
inc(s);
fv[2].xx(s);
s:=s
end
else
begin
timer.Enabled:=false;
application.Terminate;
end;
end;




thank you in advance


 



Avatar of LRHGuy
LRHGuy

Maybe I just don't see it, but where does "t" get it's value in the thread routine?

        if t=w then

If t is random or zero, you might not be getting the expected results.
In the timer routine you show a local variable of fv, but you don't show the assignment. Are you creating them on every timer trigger?

Maybe you're just not showing enough code, but I don't see the threads in action. It looks like it'a all happening serially.
Avatar of Murdoc

ASKER

Propably there will be better LRHGuy if i will post whole code here :


----------------------thread-----------------------------------
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  obraz = class(TThread)
  private

  procedure dawaj;

  public
    can:Tcanvas;
  bit:tbitmap;
  procedure xx(k:integer);
    { Private declarations }
  protected
    procedure Execute; override;
  end;

implementation
uses unit1;
var
s:integer;


procedure obraz.Execute;
begin
freeonterminate:=true;
synchronize(dawaj);  { Place thread code here }

end;
procedure obraz.dawaj;
var
w,h,i,t:integer;


begin

ShowCursor(False);
w:= Screen.Width;
h:= Screen.Height;
t:=-1;
for i:=0 to w do
        begin
        can:=tcanvas.Create;
Can.Handle:=GetWindowDC(GetDesktopWindow);
        inc(t);
        bit:=tbitmap.Create;
        bit.Height:=h;
        bit.width:=w;
        bit.Canvas.Brush.Color:=clblack;
        bit.Canvas.pen.Color:=clwhite;
        bit.canvas.FillRect(rect(0,0,w,h));
        bit.canvas.Pen.Width:=10;
        bit.canvas.MoveTo(t,0);
        bit.canvas.lineto(t,h);
        Can.Draw(100,100,bit);
        can.Free;
        bit.free;
        if t=w then
        begin
        ShowCursor(true);
        UpdateWindow(GetDesktopWindow);
        terminate;
        end;
        end;


end;

procedure obraz.xx(k:integer);
var cn:Tcanvas;
tb:Tbitmap;
w,h,i,t:integer;
begin
cn:=tcanvas.Create;
Cn.Handle:=GetWindowDC(GetDesktopWindow);
w:= Screen.Width;
h:= Screen.Height;
        tb:=tbitmap.Create;
        tb.Height:=h;
        tb.width:=w;
        tb.Canvas.Brush.Color:=clblack;
        tb.Canvas.pen.Color:=clwhite;
        tb.canvas.FillRect(rect(0,0,w,h));
        tb.canvas.Pen.Width:=10;
        tb.canvas.MoveTo(k,0);
        tb.canvas.lineto(k,h);
        Cn.Draw(0,0,tb);
        tb.Free;
        cn.Free;
        if t=w then
        begin
        ShowCursor(true);
        UpdateWindow(GetDesktopWindow);
        terminate;
        end;
        end;


end.

-----------------------------------------end of thread---------------


-------------------------Main code--------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,unit2, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    timer: TTimer;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure timerTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   s:integer;
implementation

{$R *.dfm}



procedure TForm1.Button2Click(Sender: TObject);
var
f:obraz;
begin
f:=obraz.Create(false);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
s:=-1;
timer.Enabled:=true;
end;

procedure TForm1.timerTimer(Sender: TObject);
var
fv:array [0..2] of obraz;
begin
inc(s);
if s<screen.Width then
begin
fv[0].xx(s);
inc(s);
fv[1].xx(s);
inc(s);
fv[2].xx(s);
s:=s
end
else
begin
timer.Enabled:=false;
application.Terminate;
end;
end;

end.

---------------------------end of main code------------------------

Regards
hello  Murdoc , , I can not understand your question - -

" application that will simulate scanning on the monitor display"

what do you mean by scanning, , , , , I looked at your code and I can not see anthing in it that I would think of as Scanning?

I am wondering why you are doing a thread for this, is it nessarry for this type of thing?

and your

procedure obraz.xx(k:integer);

procedure, , as far as I can tell it creates a  TBitmap and then paints that bitmap Black, and then draws a single vertical line (10 pixels wide) from top to bottom at the  K location, and then paints (draws) this Bitmap onto the screen. . . But I do not know what you are trying to do with this? ? ? Does this have something to do with scanning? Whatever you mean by that? ? ?
as far as I can tell a black screen is shown, and a 10 pixel line moves across the screen at the timer interval, if you use a 10 pixel line, why move it only one pixel?

for me,  I do not think you need the bitmap at all , why don't you just draw directly on the screen?
and I do not think that the GetWindoDC( ) function is correct for this, the Desktop window is a virtual window, kind of left over from the win 16 bit days. .


procedure obraz.xx(k:integer);
var cn:Tcanvas;
w,h,i,t:integer;
begin
w:= Screen.Width;
h:= Screen.Height;
cn:=tcanvas.Create;
Cn.Handle:=GetDC(0);
 cn.Canvas.Brush.Color:=clblack;
 cn.Canvas.pen.Color:=clwhite;
 cn.canvas.FillRect(rect(0,0,w,h));
 cn.canvas.Pen.Width:=10;
 cn.canvas.MoveTo(k,0);
 cn.canvas.lineto(k,h);
 cn.Free;
 //if t=w then  // where is t assigned any value ? ? ?
 if k=w then
   begin
   ShowCursor(true);
   terminate;
   end;
end;

 - - - - - - - -  - - - -

anyway, I might ask, what are you trying to do? ?
Avatar of Murdoc

ASKER

I'm trying do develope application that will run automatically with windows , then the application will show message on the black screen ("Touch the screen.You fingertips will be scanning"), some kind of scanning simulation (that's why on the black screen white line moves pixel by pixel)  :) and finally appllication will terminate allowing user to to his job on the computer.
Reasumming:  application is just for fun, to laugh on somebody
:)))

Regards
Avatar of Murdoc

ASKER

btw Slick812  while drawing line on the canvas the line is flickering that's why i was creating bitmap


Regards
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murdoc

ASKER

Slick812  --->>> you're code is what i was looking for.

One more time thanks
btw. you're code is exactly what i was trying to do :)

Regards