MFC Custom Control in a MFC DLL
Hello everyone!!
I'm going to make a custom control which can be loaded on Toolbox to
add in a MFC Application as a component. Before I tried creating a MFC
ActiveX Control but it's an OCX and plus it uses COM.
So I'm trying to create a regular DLL (which could have more than one
control...) and I started with creating a new MFC DLL choosing Use MFC
in a Shared DLL.
Then I added a MFC Class where at the start I want to show a rectangle
that redraws every time I resize the control on a resource window in a
MFC application.
I'd like to know how I can initialize the MFC DLL in the main .CPP
file of the DLL in the way I can load the DLL in the Toolbox and have
a custom control to apply in a MFC application? Or maybe I shall
change the CWnd base in my class with, for example, CDialog?
Thanks to everyone in advance for the suggests..... :-)
Ciao
Luigi
Here's the code of custom control's class:
//
// CaChart.cpp : implementation file
//
#include "stdafx.h"
#include "CACharts.h"
#include "CaChart.h"
// CCaChart
IMPLEMENT_DYNAMIC(CCaChart, CWnd)
CCaChart::CCaChart()
: iGraphType(GRAPH_BARS)
{
RegisterWindowClass();
}
CCaChart::~CCaChart()
{
}
// Register the window class if it has not already been registered.
BOOL CCaChart::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, CACHART_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW |
CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor
(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = CACHART_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
BEGIN_MESSAGE_MAP(CCaChart, CWnd)
//{{AFX_MSG_MAP(CCaChart)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CCaChart message handlers
void CCaChart::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Create memory DC
CDC MemDC;
if (!MemDC.CreateCompatibleDC(&dc))
return;
CRect rect;
GetClientRect(rect);
MemDC.FillRect(rect, CBrush::FromHandle((HBRUSH)GetStockObject
(BLACK_BRUSH)));
MemDC.Ellipse(rect);
}
BOOL CCaChart::OnEraseBkgnd(CDC* pDC)
{
return CWnd::OnEraseBkgnd(pDC);
}
void CCaChart::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base
class
// In our case this is not needed - yet - so just drop through
to
// the base class
// Get Size of Display area
CWnd::PreSubclassWindow();
}
BOOL CCaChart::Create(CWnd* pParentWnd, const RECT& rect, UINT nID,
DWORD dwStyle /*=WS_VISIBLE*/)
{
return CWnd::Create(CACHART_CLASSNAME, _T(""), dwStyle, rect,
pParentWnd, nID);
}
void CCaChart::set_GraphType(GRAPH_TYPE graphtype)
{
iGraphType = graphtype;
}
GRAPH_TYPE CCaChart::get_GraphType()
{
return (GRAPH_TYPE)iGraphType;
}