Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Canvas

Posted on 2000-04-05
19
484 Views
Last Modified: 2010-04-04
Hi!
Sometimes I get a stupid mistake: "Canvas doesn't allow drawing". Usually it happens if another window covers a part of my window (but not always). What I do: I have some controls, on the canvas of which I perform some drawing. Does anybody know what to do?

Thanks.
0
Comment
Question by:shtern
  • 8
  • 3
  • 2
  • +6
19 Comments
 
LVL 2

Expert Comment

by:kubeerja
ID: 2687287
can u show part of your code
0
 

Author Comment

by:shtern
ID: 2687315
It's really long. There are a couple of self-made components, which perform a lot of drawing. It's a simple drawing on canvas: Canvas.Draw, BitBlt and so on.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2687562
There are a few possible reasons for this.

1. The canvas on which you are trying to draw has been locked.

2. The handle to the canvas which you are drawing on (or attempting to anyway) is not valid.

 if you are moving things around on the form and using screen coordinates for the drawing, you might be actually trying to draw unto something which does not allow it. (such as a form's caption bar).
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:bryan7
ID: 2688494
listenning
0
 
LVL 1

Expert Comment

by:nrico
ID: 2689152
You can eliminate (2) if you use Form.Canvas.Handle.
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2689343
Try using a TImage instead of painting directly on the form canvas. Might work better.

--johan
0
 

Author Comment

by:shtern
ID: 2689377
I draw direct on the control's canvas, and use the coordinates inside of it, so it can't be the form's caption. Using TImage could be OK, but where is the difference? I have created my own component, something like TTreeView, but with other possibilities, and the drawing I perform is the drawing of nodes on it. Should I use TImage as background?

Except, it happens only if another window (e.g. Explorer) has covered a part of my window for a while. As my window becomes active again, this error occurs, but not always.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2689432
Is it possible your try drawing in control was created, but not inserted in Z-order? I mean:

C:=TMyControl.Create(Self);
// you can't draw before next operator is executed.
InsertControl(C);
// now drawing available.

-----
Igor.
0
 

Author Comment

by:shtern
ID: 2689470
It's not when the program starts. I can work any time with it, and then it happens. This control is on the main form, and it is always open.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2689497
When you move some form over your component, it may force drawing of your component. Do you have the message at that moment?
----
Igor.
0
 

Expert Comment

by:MaximW
ID: 2711947
It happens if you use a lot of graphics and your system GDI resources are low(=0).Optimizing of open DCs & bitmaps should help.
0
 

Author Comment

by:shtern
ID: 2712787
MaximW, could you please tell me more about it. Yes, I use graphic, but not so much, and it happens on many computers with different resources. What do you mean with optimizing?
0
 

Expert Comment

by:MaximW
ID: 2720638
If the problem is running out of GDI resources... try to look up
b : TBitmap;
b.Dormant;
b.ReleaseImage;

P.S. Sorry for so unclear advises, but i have the same problem to solve:).
0
 

Author Comment

by:shtern
ID: 2720727
I don't understand why is it running out of resources. I have just some bitmaps (two or three) which I draw with Canvas.Draw as a Background and some TImage's, no more. But maybe it's a case.

But I haven't understood your last advise: where should I use it?

Sorry for such a question, but I've read in delphi help about it, and understood practically nothing.
0
 

Expert Comment

by:MaximW
ID: 2722251
Hi
 regarding
 .Dormant
 .FreeImage

 as far as I know,  for  every TBitmap or TImage object you open, GDI locks an entry for HBITMAP(bmp handle) in the GDI table. The more bitmap objects you have open the less free entries remain.
So ,if you use a bitmap for drawing purpose only, something like this

 mb:= TBitmap.Create;
 mb.LoadFromResources(...
 .Canvas.Draw (x,y,mb);

and you need TBitmap object(not the image) itself for the future use, makes sense the following:
mb:= TBitmap.Create;
 mb.LoadFromResources(...
 .Canvas.Draw (x,y,mb);
 mb.Dormant;
 mb.FreeImage;
//it will release the GDI entry

even if you place it in the loop of ...200, you will have 200 TBitmap objects and the same level of GDI res. as it  has been before the loop.
As I have seen in Delphi help and some Delphi books, only .Dormant must do that keeping your image in memory stream instead of open image, and free GDI res , but it doesn't:-). Maybe i don't understand it good enough.
0
 

Author Comment

by:shtern
ID: 2723466
Can you also explain me what does it change (except freeing resources)? Maybe, the quality will be not so good or what?
0
 

Author Comment

by:shtern
ID: 2730774
Adjusted points from 100 to 200
0
 
LVL 1

Accepted Solution

by:
saulite earned 200 total points
ID: 2787813
Before draw on window, bring it to front. Do this every time, before drawing
0
 

Author Comment

by:shtern
ID: 2789121
Could you, please, be more exact? I have my program running. Then I run, e.g., Explorer. As I then go to my program again (e.g. Alt-Tab), this error happens. But (!) not always. Maybe it's really something with resources?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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