Archived

This topic is now archived and is closed to further replies.

Nat

CDC

Recommended Posts

Nat    122
Hi .. this might seem pretty duumb ..How do I init the CDC. I am not generating an MFC template using VC++. I am using the headers and libs only .. any tutors on CDC would be great thanks!

Share this post


Link to post
Share on other sites
Nat    122
Thanks! it worked ...
Ok.. I did not make an MFC application using MFC App Wizard. I made it like this below. Now I want to add animation to this, where and how can I add a message loop ?.
/////////////////////////////////////////////////////////////
#include

// Declare the application class
class CTestApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

// Create an instance of the application class
CTestApp TestApp;

// Declare the main window class
class CTestWindow : public CFrameWnd
{

public:
CTestWindow();


};

void CDCDrawing()
{
HDC hdc = GetDC(GetActiveWindow());
CDC* pDC = CDC::FromHandle(hdc) ;
CBrush bsh; // Construct it, then initialize
if( bsh.CreateSolidBrush( RGB(255,0,0) ))
{
// Select it into the device context
// Save the old pen at the same time
pDC->SelectObject( &bsh );
}
pDC->SetTextColor(RGB(100,0,0));
pDC->Rectangle(10,20,50,100);
pDC->Draw3dRect(100,200,50,50,RGB(200,100,0),RGB(20,255,100));

pDC->DeleteDC();
}
// The InitInstance function is called
// once when the application first executes
BOOL CTestApp::InitInstance()
{

m_pMainWnd = new CTestWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);

CDCDrawing();

m_pMainWnd->UpdateWindow();
return TRUE;
}


// The constructor for the window class
CTestWindow::CTestWindow()
{
CRect r;
// Create the window itself
Create(NULL,
"CStatic Tests",
WS_OVERLAPPEDWINDOW,
CRect(0,0,640,480));

}

class MyCView : public CView
{
public:
void OnDraw(CDC *pDC);
};

void MyCView::OnDraw(CDC *pDC)
{
// well this does not work ... I wonder why ????
pDC->TextOut(100,100,"Ok ..Lets see");
}


Share this post


Link to post
Share on other sites
NuffSaid    122
You initialise CDC by supplying a this pointer.

So to allow you to get a DC to the current window, all you need to do is

CDC dc(this);
dc.TextOut(blah..);

The DC is automatically released when the CDC object is destroyed, which should be at the end of the block as this is allocated off the stack.

As for the problem where the text isn''t drawing, try to see if CDC is NULL.

Share this post


Link to post
Share on other sites
kvh    122
what Magmai meant was that you use a HDC if you are writing straight API, that means that you include windows.h

if you include afxwin.h you are using MFC so you best use a CDC

Share this post


Link to post
Share on other sites
Nat    122
Ok! the above code does the drawing of objects .. I want to have a LOop where I can add animations .. where and how do I create it .. can I have it between ShowWindow and UpdateWindow ..??

Share this post


Link to post
Share on other sites