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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probs with drawing a line 1

Status
Not open for further replies.

horst123

Programmer
Mar 21, 2005
18
DE
Hi,

I've got a problem. I've got a class called haupt and another class called neben. In the neben class there is a pictureBox item and I want to draw a line into it.

Therefor I write in haupt: neben* p_neben = new neben();
neben.start();

in the start method i write this -> Show(); and after that I initialize graphics (p_graphics) and pen (p_pen). But if I try to draw a line by writing p_graphics -> DrawLine(p_pen, 24, 0, 24, 210); noting will happen. And if I write this into the pictureBox1_Click(...) method the line is drawn. Can anybody explain this to me?


thx
 
in p_Graphics, have you acquired a context to the picturebox?

If you do this in the onclick of the picturebox, then (and I am asusming here, which is dangerous) the this pointer refers to the picturebox.

p_graphics may not be in the picturebox but somewhere else.

Did you know that you can make your code appear in its own box on this forum by wrapping it in [*code*] and [*/code*] tags (remove the * to make it work)
 
I initialized the p_graphics pointer like that: code p_graphics = this -> pictureBox1 -> CreateGraphics(); /code

Did you want to know that? It is initialized in the start() method... I post the impotant things:

code System::Void start() {
this -> Show();
p_graphics = this -> pictureBox1 -> CreateGraphics();
p_pen = new Pen(Color::Black);
draw_Graph();
}

//void drawLine();
private: System::Void pictureBox1_Click(System::Object * sender, System::EventArgs * e)
{
//draw_pGraph();
p_graphics -> DrawLine(p_pen, 1, 1, 300, 400);
p_graphics -> DrawLine(p_pen, 1, 1, 400, 50);
}

private: System::Void draw_Graph() {
p_graphics -> DrawLine(p_pen, 24, 0, 24, 210);
p_graphics -> DrawLine(p_pen, 20, 200, 504, 210);

Invalidate();
}

};
/code
 
2 things to try.

1) Put a break point in each function, and check the value of the pointer when the function is called (and if they are enabled at the same time, check that they are equal)

2) Why are the coordinates for drawing on the context different in the OnClick and drawGraph functions?

K
 
Hi there,

the pointers are equal and the coordinates are okay, too. If I write draw_Graph() into the pictureBox1_click(...) method he draws the line, only if I write it into the start() method he doesn't.
 
Where / when is the start method called?

(Sorry Im being dense and missing the obvious, I've not done much GUI work with C++)
 
I call the start method when I push a button of the main frame ("haupt").

thx for help, I do not know what else to try...
 
What happens if the button is in the same container as the picturebox, rather than in the frame class?

 
Eeeh,

I don't know, but I have some new information:

if i write "while(true);" after the lines in the draw_Graph() method i see the lines, but the pictureBox is transparent and seems not to be completely initialized... perhaps that helps
 
Try stepping through the function to see where it goes after you have called draw function.

I would guess that if anything, it is being drawn then overwritten by something further on in the code, as the window should be initialised only once, and that would happen before you pressed the button in haupt

The problem lies somewhere inbetween the interfacing of the Main Frame and the window in which your drawingbox is located. Is this a dialog based application?
 
Hi,

using a Panel solves the problem... wonder why, but thanks anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top