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

MFC. How to call OnDraw() ??

Status
Not open for further replies.

alehawk

Programmer
Jun 18, 2003
332
AR
Hi!
I had creater a workspace with 2 projects, uno is MFC APP Wizzar(exe) named "draw" and other WIN32 Console Application named as "Principal". What I want it to call "draw" from "Principal". In principal i select a number between 1 to 11. I check global variables coordinates that corresponds to the number, I save them in other global variables and then I call OnDraw(pDC). Because of the Globeal Variables mi intention to call pDC->Ellipse(x1,y1,x2,y2) from OnDraw so I can draw a cicrcle with the data stored in the variables.
I can compile the 2 projects separately and I get no errors but when I want to run Principal.exe I got 0 x C0000005: Acces Violation errer. I guess the problem is in the OnDraw() function call. How can I call Ondraw() form other project?
Here I put the main code and the draw code.
Hope someone can help
Thank you!



********* CÓDIGO DE PRINCIPAL ************


#include <iostream.h>
#include "stdafx.h"
#include "draw.h"
#include "drawDoc.h"
#include "drawView.h"



//////////////////////////////
// INITIALS COORDENATES //
//////////////////////////////

int Coord_x1 [ ] = {450, 300, 200, 350, 550, 700, 800, 650, 475, 625, 550};
int Coord_y1 [ ] = { 50, 150, 350, 450, 500, 450, 275, 100, 300, 300, 400};
int Coord_x2 [ ] = {750, 600, 500, 650, 850,1000,1125, 950, 675, 825, 750};
int Coord_y2 [ ] = {350, 450, 650, 750, 800, 750, 600, 400, 500, 500, 600};


int x1,y1,x2,y2;

CDrawView *drawer;
CDC *pDC;

/////////////////////////////////////////////////
// SUBSCRIBE //
/////////////////////////////////////////////////

void main () {


int num;
cout << "Number of Antenna: " << endl;
cin >> num;


x1= Coord_x1[num-1];
y1= Coord_y1[num-1];
x2= Coord_x2[num-1];
y2= Coord_y2[num-1];

drawer ->OnDraw(pDC);

}

******** CODIGO DE drawView.cpp **********


int x1,y1,x2,y2;

void CDrawView::OnDraw(CDC* pDC) {

CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

/////////////////////////////////////////////////////
// wRITE TEXT //
/////////////////////////////////////////////////////

char text [] = "Cover zone for mobile telephony ";
pDC ->SetTextColor(RGB(0,0,0));
pDC ->TextOut(25,25,text,strlen (text));


//////////////////////////////////////////////////////
// DRAW POLYGON //
//////////////////////////////////////////////////////

CPen * draw_poly;
CPen Pen (PS_SOLID, 3, RGB (0,0,0));
POINT points [7] = {600, 50, 350, 250, 250, 550, 550, 750, 950, 650, 1100, 350, 600, 50};
draw_poly = pDC ->SelectObject(&Pen);
pDC ->Polyline (points,7);
pDC ->SelectObject (Pen);

//////////////////////////////////////////////////////
// DRAW CIRCLES //
//////////////////////////////////////////////////////

CPen * draw_circles;
CPen Pen2 (PS_SOLID, 1, RGB (0,0,0));

CBrush * antiguoRelleno;
CBrush Relleno (RGB (0,0,255));

draw_circles = pDC ->SelectObject(&Pen2);
antiguoRelleno = pDC ->SelectObject (&Relleno);

pDC->Ellipse(x1,y1,x2,y2);

pDC ->SelectObject (Pen2);

}
 
PRINCIPAL is a 10-line program that doesn't do anything right.

Code:
void main () {
   int num;
   cout << "Number of Antenna: " << endl;
   cin >> num;

   x1= Coord_x1[num-1];
   y1= Coord_y1[num-1];
   x2= Coord_x2[num-1];
   y2= Coord_y2[num-1];

   drawer ->OnDraw(pDC);
}

First of all, the four lines where you assign x1, y1, x2 and y2 do nothing, because those variables are not used anywhere else.

But the kicker is the fact that drawer and pDC have not been initialized. drawer most likely equals 0xCCCCCCCC or something like that. Trying to dereference that pointer easily causes the General Protection Fault.



In general, modern operating systems like Windows have a memory management scheme that protects each process's memory from the other processes. That being said, you can't directly modify the memory in other processes. A pointer that is valid in one process will not be valid in another.

In the same way, global variables are only global within the scope of a single process. If you assign a value to a variable called x1 in one program, it will not assign that value to any variable called x1 in other programs. (Otherwise, all MFC programs would suddenly crash when you did m_pMainWnd = new CMainFrame()!!!)



My advice to you is to consolidate these two programs into one. Instead of a console program prompting for user input, try using a dialog box. Then, using the Document/View model, you can use that to invalidate the window which will cause OnDraw to be called implicitly (the correct way).

I REALLY hope that helps.
Will
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top