CDC

Started by
4 comments, last by Nat 23 years, 1 month ago
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!
~~~º¥º -
Advertisement
Cult of the Dead Cow? ohhhh a device context class

Did you make an MFC project? If not then you want to use a HDC - CDC is an MFC class.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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");
}


~~~º¥º -
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.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
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
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 ..??
~~~º¥º -

This topic is closed to new replies.

Advertisement