Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disappearing drawing

Status
Not open for further replies.

cppdev

Programmer
May 12, 2003
23
US
I have the following code called when the event "OnMouseMove" Occurs. It lets me draw a rectangle on the screen ...and then the error occurs immediately after drawing the rectangle...soon as i move the mouse the image disappears. Any ideas what could be wrong?

all this code should do is track the coordinates of the mouse as it moves.


Thanks in advance,
cppdev


void __fastcall TForm1::panel1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
int XLocation, YLocation;
int WidthInPixels, HeightInPixels;

WidthInPixels = GUIData.GetPixelWidth();
HeightInPixels = GUIData.GetPixelHeight();

// convert pixels to rows and columns
XLocation = X/WidthInPixels;
YLocation = Y/HeightInPixels;

Label8->Caption = XLocation;
Label9->Caption = YLocation;

}
 
Also, when i comment out these lines of code :

Label8->Caption = XLocation;
Label9->Caption = YLocation;


It does not erase the shapes drawn on the canvas.
SO i am assuming the problem is something to do with these lines of code but i do not know why. These are not even displayed on the panel and canvas where the drawing is occuring. Label8, and Label9 are on the form below the panel and canvas (which is on top of the panel ).


Thanks in advance
cppdev
 
It sounds like when you update Label8 and Label9, whatever you are drawing on is updated also(and when it's updated, it doesn't know that you want a rectangle on it anymore). What exactly are you drawing your rectangle to?

To test this theory out, simply move the window offscreen, let go of it, then click it and drag it back on screen. Is the rectangle gone? Yes means that the object you are drawing on has it's own paint routine. You will have to override that paint routine by creating a new custom component based on that object, or use a different object where the paint routine won't get in your way.

If this is your problem, use a TImage to draw the rectangle on instead of whatever you're using. You can use a TImage and make it transparent, then place it over the form, components, etc. Make sure you set up the transparency correctly, then you can just draw your rectangle to it's Canvas and it won't have to be redrawn until the mouse moves.

Also note that you won't get away with just drawing the rectangle on the Canvas, you will have to erase it first...but you've probably already figured that out if you are currently getting it to work without the Label interaction.

Chris
 
Yes, when i move the panel off the screen, release and drag back onto the screen the rectangle disappears.

This is the class i have been using. Could a modification of this work?

class TPanelCanvasHack : public TPanel
{
public:
__property Canvas;

// specifies brush parameters and canvas area (rect () )
void PaintCanvas();
// specifies font, font size, color
void SetPrintParameters(); // returns 1 if parameters
//set, 0 if not
// specifies grid point.
void DrawPoint(int X, int Y);
// selects area and draw rectangle to specify field
void SelectField(int X1, int Y1, int X2, int Y2 );
};


void TPanelCanvasHack::paintCanvas()
{
// PAINT - begin
TRect MyRect(GUIData.GetX1(), GUIData.GetY1(), GUIData.GetX2() , GUIData.GetY2() );

Canvas->Brush->Color = clWhite;
Canvas->Brush->Style = bsSolid;

Canvas->Rectangle(MyRect);

// specifies font size, text color and font
SetPrintParameters();

} // end PaintCanvas()
 
This is fine, but the ancestor Paint method still needs to be overriden.

class TPanelCanvasHack : public TPanel
{
public:
__property Canvas;

// specifies brush parameters and canvas area (rect () )
virtual void __fastcall Paint(void);
void PaintCanvas();
// specifies font, font size, color
void SetPrintParameters(); // returns 1 if parameters
//set, 0 if not
// specifies grid point.
void DrawPoint(int X, int Y);
// selects area and draw rectangle to specify field
void SelectField(int X1, int Y1, int X2, int Y2 );
};

Then in the .c file:

void __fastcall TPanelCanvasHack::paint(void)
{
PaintCanvas();
}

Good luck,
Chris
 
I just noticed that you are creating your new class based on TPanel instead of TCustomPanel. This might present a problem with the Paint method. Try basing it on TCustomPanel instead.
 
I have made the changes suggested. when i call

((TPanelCanvasHack*)Panel1)->Paint(); It does not paint the canvas white, it stays gray.

This is what i was calling:

((TPanelCanvasHack*)Panel1)->PaintCanvas();



Thanks in advance,
cppdev
 
Hmm...I went ahead and tested it. I did the following and it worked with just some subtle differences from what I said before:

class definition:
class PACKAGE TPanelWithCanvas : public TPanel
{
private:
protected:
public:
__property Canvas;
virtual void __fastcall Paint(void);
__fastcall TPanelWithCanvas(TComponent* Owner);
__published:
};

source code:
static inline void ValidCtrCheck(TPanelWithCanvas *)
{
new TPanelWithCanvas(NULL);
}
//---------------------------------------------------------------------------
__fastcall TPanelWithCanvas::TPanelWithCanvas(TComponent* Owner)
: TPanel(Owner)
{
}
void __fastcall TPanelWithCanvas::paint(void)
{
TCustomPanel::paint();
Canvas->Rectangle(0,0,10,10);
}
//---------------------------------------------------------------------------
namespace Panelwithcanvas
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TPanelWithCanvas)};
RegisterComponents("Samples", classes, 0);
}
}

Good luck,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top