Step by step procedures for creating the VC/Ex2 example
- Make sure you have register the ActiveCDX.OCX, see install.htm for more information on this.
- Create a new project using MFC AppWiz. In step 1 select Dialog based for application type and then click finish.
- Insert the ActiveCDX Control.
Open the dialog editor, right click at the form, select CDXVisual Control and set the ID to IDC_CDXVISUALCTRL. Right click again at the form, select CDXDraw Control and set the ID to
IDC_CDXDRAWCTRL.
- Open the MFC ClassWizard, select member variables tab, double click IDC_CDXVISUALCTRL, the Developer studio will ask you if you want to make a wrapper class for the control, click OK. Change the class name into CDXVisual, click OK. Enter m_Visual as the member variable name, click OK. Do the same for the IDC_CDXDRAWCTRL but set the class name into CDXDraw and the member variable name into
m_Draw.
- Put 4 buttons on the form, set their ID to IDC_STOP, IDC_LINE, IDC_RECT, and IDC_CIRCLE. Set each button caption to
"Stop", "Line", "Rect" and "Circle" according to their respective ID.Rename the OK button
caption to "Exit". Remove the other buttons and control from the dialog form. We wouldn't need them.
- Add a protected member m_Demo to the CEx2Dlg (the dialog class). This variable will indicate which shape to draw. It's an integer variable. The possible values are 0 (none), 1 (line), 2 (rect) and 3
(fillcircle).
- Double click all the 4 buttons you have created before in step 5 (STOP, LINE, RECT, and CIRCLE) one by one to create the handler functions, and insert the code:
void CEx2Dlg::OnStop()
{
// TODO: Add your control notification handler code here
m_Demo = 0;
m_Visual.Fill(m_Visual.GetBack(), 0);
}
void CEx2Dlg::OnLine()
{
// TODO: Add your control notification handler code here
m_Demo = 1;
m_Visual.Fill(m_Visual.GetBack(), 0);
}
void CEx2Dlg::OnRect()
{
// TODO: Add your control notification handler code here
m_Demo = 2;
m_Visual.Fill(m_Visual.GetBack(), 0);
}
void CEx2Dlg::OnCircle()
{
// TODO: Add your control notification handler code here
m_Demo = 3;
m_Visual.Fill(m_Visual.GetBack(), 0);
}
The above code will set the m_Demo to a value according to the button that the user select. The Fill command will erase the backbuffer by filling it with black pixels.
- Use the ClassWizard to create a handler for WM_TIMER message. Activate the timer in InitDialog by writing this code below the TODO:
SetTimer(1, 10, 0);
- Write this code for the OnTimer:
void CEx2Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static BOOL firstTime = TRUE;
if (firstTime)
{
firstTime = FALSE;
srand( (unsigned)time( NULL ) );
if (!m_Visual.CreateWindowed((long)GetSafeHwnd(), 640, 480))
{
MessageBox("Failed creating windowed CDX");
PostQuitMessage(0);
return;
}
}
// Don't bother if no shape to draw
if (m_Demo != 0)
{
// Get and lock the backbuffer
long backBuffer = m_Visual.GetBack();
m_Visual.Lock(backBuffer);
// Generate random position and color
long x1 = (rand()*640)/RAND_MAX;
long y1 = (rand()*480)/RAND_MAX;
long x2 = (rand()*640)/RAND_MAX;
long y2 = (rand()*480)/RAND_MAX;
long radius = (rand()*50)/RAND_MAX;
long color = rand()*rand();
// Draw the shape
switch (m_Demo)
{
case 1:
m_Draw.Line(backBuffer, x1, y1, x2, y2, color);
break;
case 2:
m_Draw.Rect(backBuffer, x1, y1, x2, y2, color);
break;
case 3:
m_Draw.FillCircle(backBuffer, x1, y1, radius, color);
break;
}
// Unlock and flip
m_Visual.Unlock(backBuffer);
m_Visual.Flip();
Invalidate(FALSE);
}
CDialog::OnTimer(nIDEvent);
}
The code will first get the back buffer, lock it, draw something, unlock it and flip.