Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
void CMFC_ActiveXCtrl::OnDraw(
            CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
    // TODO: Replace the following code with your own drawing code.
//  m_MfcDlg.MoveWindow(rcBounds,TRUE);
//  InvalidateControl(rcBounds,TRUE);
    m_MfcDlg.m_Static.MoveWindow(CRect(rcBounds.left,rcBounds.top,rcBounds.Width()-18,rcBounds.Height()-18),TRUE);
    m_MfcDlg.SetWindowPos(NULL,rcBounds.left,rcBounds.top,rcBounds.Width(),rcBounds.Height(),SWP_NOMOVE);
}


void CMFC_Dialog::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	GetDlgItem(IDC_DISPLAY_IMAGE)->GetClientRect(&m_rectClientRect);

//	CDC *lPdc = GetDlgItem(IDC_DISPLAY_IMAGE)->GetDC();

//	lPdc->Rectangle(m_rectClientRect.left+20,m_rectClientRect.top+20,m_rectClientRect.right-20,m_rectClientRect.bottom-20);

	Graphics lGraphics(GetDlgItem(IDC_DISPLAY_IMAGE)->GetDC()->m_hDC);

	Pen pen(Color(255,255,0));

	lGraphics.DrawRectangle(&pen,Rect(10,10,200,200));


	// Do not call CDialog::OnPaint() for painting messages
}

When I use this in ActiveX Container,My Application Collapsed.Who can tell me how to use GDI+ in Dialog-Based ActiveX.THANKS!
Posted

1 solution

Are you sure you initialized GDI+ correctly in the ActiveX solution?
 
Share this answer
 
Comments
apriltommy 30-Aug-10 7:04am    
I use the class below to initialize GDI+
class CGdiPlusCtrl
{
public:
void DeInitializeGdiPlus();
void InitializeGdiPlus();
CGdiPlusCtrl();
virtual ~CGdiPlusCtrl();
private:
HANDLE m_hMap;

bool m_bInited, m_bInitCtorDtor;

ULONG_PTR m_gdiplusToken;

GdiplusStartupInput m_gdiplusStartupInput;

long m_initcount;

};
CGdiPlusCtrl::CGdiPlusCtrl():m_bInitCtorDtor(false),
m_bInited(false),
m_hMap(NULL),
m_gdiplusStartupInput(NULL),
m_gdiplusToken(NULL),
m_initcount(0)
{
if(m_bInitCtorDtor)
InitializeGdiPlus();
}

CGdiPlusCtrl::~CGdiPlusCtrl()
{
if (m_bInitCtorDtor)
DeInitializeGdiPlus();
}

void CGdiPlusCtrl::InitializeGdiPlus()
{
if (!m_bInited)
{
char buffer[1024];
sprintf(buffer,"GDIPlusInitID = %x",GetCurrentProcessId());
m_hMap = CreateFileMapping((HANDLE)INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE|SEC_COMMIT,0,sizeof(long),buffer);
if (m_hMap == NULL)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(m_hMap);
}
else
{
m_bInited = true;
GdiplusStartup(&m_gdiplusToken,&m_gdiplusStartupInput,NULL);
TRACE("GdiPlus Inited!
");
}
}
}
m_initcount++;
}

void CGdiPlusCtrl::DeInitializeGdiPlus()
{
m_initcount--;
if (m_bInited == true || m_initcount !=0)
{
TRACE("GdiPlus ShutDown!
");
GdiplusShutdown(m_gdiplusToken);
m_bInited = false;
CloseHandle(m_hMap);

}
}
apriltommy 30-Aug-10 7:12am    
THANK YOU!
I put the initialization in the WRONG palce.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900