Recent

Author Topic: First draw of user created components blacked out.  (Read 7532 times)

Andyk

  • Jr. Member
  • **
  • Posts: 65
First draw of user created components blacked out.
« on: April 01, 2012, 12:23:42 am »
I have my own visual components created in delphi which I wish to port to lazarus.

They do work OK by and large but the first time they draw on the screen I just get an all black rectangle. I have to change something like resizing the form to make them draw properly.

Where should I put an initial paint command, I can't put it in the constructor since it doesn't have an owner at that point.


lainz

  • Guest
Re: First draw of user created components blacked out.
« Reply #1 on: April 01, 2012, 12:38:55 am »
Depending from wich descends.

For TGraphicControl in the Paint procedure.

Check CustomDrawnControls.

Andyk

  • Jr. Member
  • **
  • Posts: 65
Re: First draw of user created components blacked out.
« Reply #2 on: April 01, 2012, 12:42:41 am »
Depending from wich descends.

For TGraphicControl in the Paint procedure.

Check CustomDrawnControls.

Yes, it has a paint procedure, but Lazarus doesn't seem to call it on first display.

 

lainz

  • Guest
Re: First draw of user created components blacked out.
« Reply #3 on: April 01, 2012, 12:47:40 am »
Depending from wich descends.

For TGraphicControl in the Paint procedure.

Check CustomDrawnControls.

Yes, it has a paint procedure, but Lazarus doesn't seem to call it on first display.

Try to create a new TGraphicControl descendant, draw something in the Paint event and check if it works.

Andyk

  • Jr. Member
  • **
  • Posts: 65
Re: First draw of user created components blacked out.
« Reply #4 on: April 01, 2012, 01:04:56 am »
Depending from wich descends.

For TGraphicControl in the Paint procedure.

Check CustomDrawnControls.

Yes, it has a paint procedure, but Lazarus doesn't seem to call it on first display.

Try to create a new TGraphicControl descendant, draw something in the Paint event and check if it works.

It derived from Tcustomcontrol, I have other derived from TGraphiccontrol and they all have the same problem.

They work fine except for the very first time, once something changes which forces them to repaint they work OK and from then on they work properly.

So the paint procedures are working properly, its just the very first time they are called that's the problem.

This is on WinXP by the way.


Andyk

  • Jr. Member
  • **
  • Posts: 65
Re: First draw of user created components blacked out.
« Reply #5 on: April 01, 2012, 11:48:53 pm »
I have been trying to track down this problem and it appears it is caused by the method I use to draw the control.

In all my components I draw the control onto a Tbitmap first and then use the controls canvas.draw method to copy the bitmap to the controls canvas.

This is pretty standard practice in delphi to reduce screen flicker but it doesn't seem to work properly in lazarus.

The problem seems to be that no handle is allocated to the bitmap canvas which stops the bitmaps width and height from being queried.

Seems like it is an LCL bug, but not sure of the root cause of it yet.

I can't disable the bitmap.canvas HandleAllocated function because it affects visual component canvas's as well.



felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: First draw of user created components blacked out.
« Reply #6 on: April 03, 2012, 08:28:54 am »
To start with: Always mention your operating system version, lazarus and fpc versions and always post code snipets. It is impossible to guess what is wrong without seeing some code.

For sure you are doing something unusual here which leads to the problem, because I have written hundreds of TCustomControl descendents and never, ever have I had this issue. I always follow the wiki instructions which I created:

http://wiki.lazarus.freepascal.org/Developing%20with%20Graphics#Create_a_custom_control_which_draws_itself

Drawing first to a bitmap also works fine. And everywhere: Windows, Linux, Mac OS X, Android, Windows CE. Everywhere it works perfectly for me.

Without you posting your code it is impossible to guess what you are doing different then I am, but I recommend that you read the linked wiki article. You can also see example programs, like:

http://lazarus-ccr.svn.sourceforge.net/viewvc/lazarus-ccr/applications/lazeyes/
http://lazarus-ccr.svn.sourceforge.net/viewvc/lazarus-ccr/applications/lazclock/

This is pretty standard practice in delphi to reduce screen flicker but it doesn't seem to work properly in lazarus.

For sure it works, because I also utilized this method often.

Andyk

  • Jr. Member
  • **
  • Posts: 65
Re: First draw of user created components blacked out.
« Reply #7 on: April 03, 2012, 02:09:13 pm »
Hi Filipe.

I did mention above that this was windows XP.

The problem was caused because the first thing i do is clear the canvas with a fillrect.

like this

Quote
 
with DrwBmp, Canvas do
  begin
    brush.color:=FBGcolor;
    pen.color:=clblack;
    brush.style:=bssolid;
    fillrect(0,0,Width,Height);     // Clear canvas 

The problem with the above code snippet is that its getting width and height values from drwbmp.canvas and these routines always return a value of zero unless the canvas has a handle allocated, which I'm guessing is not used when the canvas is attached to a bitmap.

In this case, the workaround is to make sure drwbmp.width and drwbmp.height are explicitly used.

Andy

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: First draw of user created components blacked out.
« Reply #8 on: April 03, 2012, 02:27:13 pm »
Your problem seems a little similar to my old issue (it was related to transparency and maybe resizing IIRC).
http://www.lazarus.freepascal.org/index.php/topic,14440.msg76461.html#msg76461

Try:
Code: [Select]
fillrect(0, 0, DrwBmp.Width, DrwBmp.Height);
Maybe it will help.
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BlueIcaro

  • Hero Member
  • *****
  • Posts: 576
Re: First draw of user created components blacked out.
« Reply #9 on: April 03, 2012, 02:29:21 pm »
Hi Filipe.

I did mention above that this was windows XP.

The problem was caused because the first thing i do is clear the canvas with a fillrect.

like this

Quote
 
with DrwBmp, Canvas do
  begin
    brush.color:=FBGcolor;
    pen.color:=clblack;
    brush.style:=bssolid;
    fillrect(0,0,Width,Height);     // Clear canvas 

The problem with the above code snippet is that its getting width and height values from drwbmp.canvas and these routines always return a value of zero unless the canvas has a handle allocated, which I'm guessing is not used when the canvas is attached to a bitmap.

In this case, the workaround is to make sure drwbmp.width and drwbmp.height are explicitly used.

Andy
Which version os Lazarus/FPC?.
Try to remove "with DrwBmp, Canvas do" and write down all sentece complety. May be the compiler has a mess. Sometime I have  unexpected situations, and I solved this way.

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

Andyk

  • Jr. Member
  • **
  • Posts: 65
Re: First draw of user created components blacked out.
« Reply #10 on: April 03, 2012, 02:44:47 pm »
Hi BluIcaro

oops, sorry, yes its Laz V 0.9.30.4 and FPC v 2.6.0.

and there is no need to try that as I already posted the solution above, which worked.

You just have to be careful in the code to get a bitmaps height and width from the bitmap itself and not its canvas.

I'm not sure if this is intentional behaviour or a bug, but it is different behaviour to delphi.

Actually I found some other issues which worked in Delphi and not in Lazarus and which did turn out to be real bugs in my code. Its weird how it even worked in Delphi in the first place.

Andy



felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: First draw of user created components blacked out.
« Reply #11 on: April 03, 2012, 05:54:01 pm »
This is an innerent problem from the "with" statement. That's why I never use it. With "with" things get messy and it is impossible for someone reading the code to know to which object the properties without qualifiers are refering too, so it makes the code unreable and brings issues when objects change and get new properties added.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 576
Re: First draw of user created components blacked out.
« Reply #12 on: April 03, 2012, 06:56:32 pm »
This is an innerent problem from the "with" statement. That's why I never use it. With "with" things get messy and it is impossible for someone reading the code to know to which object the properties without qualifiers are refering too, so it makes the code unreable and brings issues when objects change and get new properties added.
I think so Felipe

 As I said, I had some problems with them. So I use this statement only in a few situations. Normaly in a Loop,  with the same statement very "know", like this:

Code: Pascal  [Select]
  1.  While something do
  2.  Begin
  3.  With Memo1.Lines do
  4.  Begin
  5.   Add('blall'´);
  6.   Add('hfjghjf');
  7. end;
  8. end;
  9.  

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/