|
-
March 14th, 2009, 01:05 PM
#1
Drawing from Object
I'm trying to create a separate object from my program ('trackerObj.h') which I can use in my main window.
I want to be able to draw a rectangle for each new trackerObj.
For example,
Code:
void CVisualAuidoView::OnLButtonDown(UINT nFlags, CPoint point)
{
trackObj *mytrack = new trackObj();
}
my trackObj class is:
Code:
#pragma once
#include "stdafx.h"
class trackObj : public CObject
{
public:
//Construction
trackObj();
public:
//Attributes
CRectTracker m_tracker;
protected:
CWnd* m_pWnd;
public:
//Operations
void drawMyTracker(int t);
public:
//Implementation
virtual ~trackObj();
};
trackObj::trackObj()
{
int t = 60;
drawMyTracker(t);
}
trackObj::~trackObj()
{
}
void trackObj::drawMyTracker(int t)
{
CClientDC dc(m_pWnd);
CClientDC* pDC=&dc;
pDC->MoveTo(t, 20);
pDC->LineTo(60, 122);
pDC->LineTo(264, 122);
}
The program starts to run, but then gives me a dialog which says:
Code:
Unhandled exception at 0x009a1c39 in VisualAuido.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
Could someone give me a hand with drawing from other classes - I can't make an OnDraw method so no idea how to go about this!
Cheers
Gareth
-
March 14th, 2009, 01:07 PM
#2
Re: Drawing from Object
What does the debugger tell/show you during the crash ?
-
March 14th, 2009, 01:17 PM
#3
Re: Drawing from Object
 Originally Posted by Skizmo
What does the debugger tell/show you during the crash ?
Well it looks as if its going to run, then I get a dialog with the message.
wingdi.cpp opens (which I've never seen before)
and I have a little yellow aarow on the left pointing at the line starting "ASSERT" in the code below:
Code:
CClientDC::CClientDC(CWnd* pWnd)
{
ASSERT(pWnd == NULL || ::IsWindow(pWnd->m_hWnd));
if (!Attach(::GetDC(m_hWnd = pWnd->GetSafeHwnd())))
AfxThrowResourceException();
}
The "Output" window (which is set to "Show output from: Debug") shows:
Code:
'VisualAuido.exe': Loaded 'C:\Users\Gareth\Documents\Visual Studio 2008\Projects\VisualAuido\Debug\VisualAuido.exe', Symbols loaded.
'VisualAuido.exe': Loaded 'C:\Windows\System32\ntdll.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\kernel32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\KernelBase.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\user32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\gdi32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\lpk.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\usp10.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\msvcrt.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\comdlg32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\shlwapi.dll'
'VisualAuido.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7000.0_none_ee2c028959bec753\comctl32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\shell32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\winspool.drv'
'VisualAuido.exe': Loaded 'C:\Windows\System32\advapi32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\rpcrt4.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\oledlg.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\ole32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\oleaut32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\imm32.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\msctf.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\avgrsstx.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\dui70.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\uxtheme.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\cryptbase.dll'
'VisualAuido.exe': Loaded 'C:\Windows\System32\dwmapi.dll'
First-chance exception at 0x009a1c39 in VisualAuido.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
Unhandled exception at 0x009a1c39 in VisualAuido.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
If you need any more info please ask - I'll be watching this thread all night! 
Cheers
Gareth
-
March 14th, 2009, 03:26 PM
#4
Re: Drawing from Object
 Originally Posted by gareth.h.rees
If you need any more info please ask - I'll be watching this thread all night!
Cheers
Gareth
Nobody needs any more information, including you. The answer is directly in your post:
Code:
ASSERT(pWnd == NULL || ::IsWindow(pWnd->m_hWnd));
One of the two conditions is NOT true.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 03:31 PM
#5
Re: Drawing from Object
 Originally Posted by TheCPUWizard
Nobody needs any more information, including you. The answer is directly in your post:
Code:
ASSERT(pWnd == NULL || ::IsWindow(pWnd->m_hWnd));
One of the two conditions is NOT true.
Sorry,
I'm pretty new to C++, and VERY new to MFC/Visual Studio...
I've tried using all sorts in the CClient dc( ____ ); code...
In most examples I've seen, 'this' has been in the brackets.
For example, when I do:
Code:
void trackObj::drawMyTracker(int t)
{
CClientDC dc(this);
//CClientDC* pDC=&dc;
dc.MoveTo(t, 20);
dc.LineTo(60, 122);
dc.LineTo(264, 122);
}
I get the error:
trackobj.h(38) : error C2664: 'CClientDC::CClientDC(CWnd *)' : cannot convert parameter 1 from 'trackObj *const ' to 'CWnd *'
I have no idea how to get around this 
Cheers
Gareth
-
March 14th, 2009, 03:34 PM
#6
Re: Drawing from Object
 Originally Posted by gareth.h.rees
trackobj.h(38) : error C2664: 'CClientDC::CClientDC(CWnd *)' : cannot convert parameter 1 from 'trackObj *const ' to 'CWnd *'
Why do you think you should be able to convert a CONSTANT tackObj into a modifiable CWnd???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 03:36 PM
#7
Re: Drawing from Object
 Originally Posted by TheCPUWizard
Why do you think you should be able to convert a CONSTANT tackObj into a modifiable CWnd???
To be fair, I don't really have that much of an idea what that means :S
I understand that C++ has drawing areas, but unlike Java, it appears that its harder to draw from any method?
I'm just trying to figure out how to create a new object that includes drawing code, but alas.. not quite sure how to go about it??
Cheers
Gareth
-
March 14th, 2009, 03:41 PM
#8
Re: Drawing from Object
You are missing some VERY VERY basic C++ knowledge. You are not nearly to the point of writing this type of application if you do not understand casting and const-correctness.
Simply put:
If you tell the compiler that you are not allowed to modify something, then attempt to say that you can, the compiler stops you.
So, DONT try to lie to the compiler, DONT try to outsmart the compiler.
DO, get a good C++ text book, and start at page #1, reading every word, and typing in EVERY line of code, and stepping through EVERY line of code with your debugger.
When you finish the book in a few weeks (assuming several hours per day dedicated to the task), you can look at this project, and immediately see the problem.
I will teach any man to fish, but nobody is going to eat the fish I caught.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 04:16 PM
#9
Re: Drawing from Object
 Originally Posted by TheCPUWizard
You are missing some VERY VERY basic C++ knowledge. You are not nearly to the point of writing this type of application if you do not understand casting and const-correctness.
Simply put:
If you tell the compiler that you are not allowed to modify something, then attempt to say that you can, the compiler stops you.
So, DONT try to lie to the compiler, DONT try to outsmart the compiler.
DO, get a good C++ text book, and start at page #1, reading every word, and typing in EVERY line of code, and stepping through EVERY line of code with your debugger.
When you finish the book in a few weeks (assuming several hours per day dedicated to the task), you can look at this project, and immediately see the problem.
I appreciate that I don't have a good knowledge of C++. It's part of my final year university project, and since it was a 'set' project I assumed the project supervisor would be able to help me out when I got stuck. When I started coding I found he didn't know much about Visual C++ either, so I was pretty much thrown right in the deep end!!
I've done most of the Sams Teach Yourself Visual C++ in 21 days but a lot of the examples were difficult to get to work - possibly because I'm using VS2008, rather than 2005?
Either way, I've had to try to push on as best I can with the project rather than spending forever reading books - I *had* to try to make progress.
I know if I had plenty of time that going through books would be the best way to learn, but I'm a tight deadline - I have 7 weeks to do the project AND a project report.
I don't want to sound impatient - I'm just convinced this is an easy thing to do so don't want to spend hours and hours on it since I've got much more to do after this.
Thanks
Gareth
Last edited by gareth.h.rees; March 16th, 2009 at 03:05 PM.
-
March 14th, 2009, 04:28 PM
#10
Re: Drawing from Object
 Originally Posted by gareth.h.rees
I've done most of the Sams Teach Yourself Visual C++ in 21 days but a lot of the examples were difficult to get to work - possibly because I'm using VS2008, rather than 2005?
There should not be any issues VS-2008/2005 related. However, if you just "skimmed" the book, I am not suprised if you had difficulty.
That book requires following the advice I gave earlier (read everything, type in everything, debug everything). It is very easy in this (or any other book) to mis a single paragraph or even sentance that is critical to understanding the following information.
For the people I know who have followed the book successfully, they usually spend 6-8 hours per chapter.
ps: Public Disclaimer: I was the technical editor of that book for some editions, and the Author is also here on CG...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 04:38 PM
#11
Re: Drawing from Object
 Originally Posted by TheCPUWizard
There should not be any issues VS-2008/2005 related. However, if you just "skimmed" the book, I am not suprised if you had difficulty.
That book requires following the advice I gave earlier (read everything, type in everything, debug everything). It is very easy in this (or any other book) to mis a single paragraph or even sentance that is critical to understanding the following information.
For the people I know who have followed the book successfully, they usually spend 6-8 hours per chapter.
ps: Public Disclaimer: I was the technical editor of that book for some editions, and the Author is also here on CG...
I think its more the layout of the IDE that makes it confusing - VS2008 seems to have a lot of differences.
I got through all of chapter one, and most of chapter two - the later pages didn't seem to be much about graphics.
Is there an easy fix to my problem? As mentioned, I really need to get things done. I'd like to try to understand what I'm doing a lot more, and I'm sure it would help in the future. However, I have other modules all with coursework/tests, as well as two part time jobs, so I just physically don't have the time to really read through books when they may or may not help.
Cheers
Gareth
-
March 14th, 2009, 05:00 PM
#12
Re: Drawing from Object
Your problem has NOTHING to do with Graphics, or even with with Windows programming. It is simple basic flaws in your logic.
For example, given the posted code (btw: you should ALWAYS post a minimal yet complete - compilable, runnable - that duplicates the error...)
Your class has an m_pWnd which you are using, but I do not see any place where it is initialized.
Then later you try to treat your object as if it WAS a CWnd, but it clearly is not.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 05:09 PM
#13
Re: Drawing from Object
 Originally Posted by TheCPUWizard
Your problem has NOTHING to do with Graphics, or even with with Windows programming. It is simple basic flaws in your logic.
Sorry, as mentioned - I know I don't know an awful lot about C++. I'm not trying to be wrong, theres just a lot to learn
 Originally Posted by TheCPUWizard
For example, given the posted code (btw: you should ALWAYS post a minimal yet complete - compilable, runnable - that duplicates the error...)
My code is several files long - I can post it all if required, but figured the bits I've posted are the main parts? If you need any more please do ask - I'm guessing that experienced programmers will know more what they're looking for than me..
 Originally Posted by TheCPUWizard
Your class has an m_pWnd which you are using, but I do not see any place where it is initialized.
I put it near the top of the trackObj.h as per an example I found on the net
Code:
public:
//Attributes
CRectTracker m_tracker;
protected:
CWnd* m_pWnd;
 Originally Posted by TheCPUWizard
Then later you try to treat your object as if it WAS a CWnd, but it clearly is not.
Not intentional - just not that sure on how to use these drawing/window methods?
How do I pass the CWnd to the new object class in order to draw?
Cheers
Gareth
-
March 14th, 2009, 05:12 PM
#14
Re: Drawing from Object
1) If you work with minimal code, it will never be long. Your entire problem can be reproduced in under 20 lines.
2) Go back and re-read what I said about assigbning a value to m_pWnd!!!!!
3) To pass a CWnd - You create a new CWnd or gain access to an existing one.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 05:17 PM
#15
Re: Drawing from Object
I posted my entire trackObj class - however, the visual studio project wizard created various things (ie, the Doc, View and MainFrame)...
Okay - I can see I haven't actually assigned anything to m_pWnd. I thought it was just a built in variable.
I noticed I can do CVisualAuidoView::GetDC and ..::GetActiveWindow.
Is that what I'm looking for?
I'd imagine the GetDC would work?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|