From: Scott McPhillips [MVP] on
ccwork wrote:
> I am sorry.
> But the application does not have document/view class. Will this be the cause?
>

No, ActiveX controls can also be dynamically created using a CDialog or
any other CWnd-derived class as the parent window. Make sure the parent
CWnd is already created and has an m_hWnd value before attempting to
create the ax control.

If you have the MFC source code installed you can single-step into your
CreateControl call to get a hint what is going wrong.

--
Scott McPhillips [VC++ MVP]

From: ccwork on
Ok. Here is the whole program:

#include <afxwin.h>
#include <afxext.h>
#include <afxdisp.h>
#include <afxdtctl.h>
#include <afxcmn.h>
#include "try.h"

#define LEFT_ALIGNMENT 30
#define RIGHT_ALIGNMENT 180

#define ZOOM_BTN_ID 0x500
#define WINDOW_LEVEL_BTN_ID 0x501
#define MAGNIFY_BTN_ID 0x502
#define PAN_BTN_ID 0x503



class MyFrame:public CFrameWnd
{
public:
MyFrame();
CWnd m_ControlWrapper;

DECLARE_DYNAMIC(MyFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
void zoomBtnClick();
void wndLevelBtnClick();
void magnifyBtnClick();
void panBtnClick();
int test();

DECLARE_MESSAGE_MAP()
};

IMPLEMENT_DYNAMIC(MyFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()

MyFrame::MyFrame()
{
}

BOOL MyFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(!CFrameWnd::PreCreateWindow(cs))
return FALSE;

cs.style=WS_OVERLAPPED|WS_SYSMENU|WS_BORDER|WS_MAXIMIZEBOX;
cs.dwExStyle|=WS_EX_TOPMOST;
cs.lpszClass=AfxRegisterWndClass(0);
return TRUE;
}

int MyFrame::test()
{
RECT rc;
GetClientRect(&rc);
BOOL bStat=m_ControlWrapper.CreateControl ("mscal.calendar.7",
"", WS_VISIBLE|WS_CHILD, rc, this, 5000, NULL, FALSE, NULL);
if (bStat==FALSE)
{
::MessageBox(m_hWnd,"Error!!", "Could not place control", MB_OK);
return 0;
}
return 0;
}

int MyFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct)==-1)
return -1;
return 0;
}

void MyFrame::zoomBtnClick()
{
AfxMessageBox("Zoom!");
}

void MyFrame::wndLevelBtnClick()
{
AfxMessageBox("Window/Level!");
}

void MyFrame::magnifyBtnClick()
{
AfxMessageBox("Magnify!");
}

void MyFrame::panBtnClick()
{
AfxMessageBox("Pan!");
}

class MyApp:public CWinApp
{
public:
MyApp();
virtual BOOL InitInstance();
virtual int ExitInstance();
};

MyApp::MyApp()
{
}

BOOL MyApp::InitInstance()
{
/* Use activeX control */
AfxEnableControlContainer();

MyFrame *wnd=new MyFrame;
m_pMainWnd=wnd;
wnd->Create(NULL, "Hello");
if (wnd->m_hWnd)
AfxMessageBox("Pan!");
wnd->test();
wnd->ShowWindow(SW_SHOWMAXIMIZED);
wnd->UpdateWindow();

return TRUE;
}

int MyApp::ExitInstance()
{
m_pMainWnd=NULL;
return CWinApp::ExitInstance();
}

MyApp *theApp=NULL;

__declspec(dllexport) int InitViewer()
{
if (theApp)
{
return 1;
}
theApp=new MyApp;
theApp->InitInstance();
return 0;
}

__declspec(dllexport) int DestroyViewer()
{
if (theApp)
{
theApp->ExitInstance();
delete theApp;
}
theApp=NULL;
return 0;
}

So in InitInstance() the m_hWnd is there (message appears) but the control
""Could not place control" still appears.

wnd->Create(NULL, "Hello");
if (wnd->m_hWnd)
AfxMessageBox("m_hWnd is there");
wnd->test();

Any idea?


"Scott McPhillips [MVP]" wrote:

> ccwork wrote:
> > I am sorry.
> > But the application does not have document/view class. Will this be the cause?
> >
>
> No, ActiveX controls can also be dynamically created using a CDialog or
> any other CWnd-derived class as the parent window. Make sure the parent
> CWnd is already created and has an m_hWnd value before attempting to
> create the ax control.
>
> If you have the MFC source code installed you can single-step into your
> CreateControl call to get a hint what is going wrong.
>
> --
> Scott McPhillips [VC++ MVP]
>
>
From: ccwork on
"Scott McPhillips [MVP]" wrote:

> ccwork wrote:
> > I am sorry.
> > But the application does not have document/view class. Will this be the cause?
> >
>
> No, ActiveX controls can also be dynamically created using a CDialog or
> any other CWnd-derived class as the parent window. Make sure the parent
> CWnd is already created and has an m_hWnd value before attempting to
> create the ax control.

What's the meaning of "CWnd is already created"? I try to create the
activeX control in the constructor of class derived from CWnd. And how do I
know the m_hWnd has a value?

> If you have the MFC source code installed you can single-step into your
> CreateControl call to get a hint what is going wrong.
>
> --
> Scott McPhillips [VC++ MVP]

From: Scott McPhillips [MVP] on
ccwork wrote:
> What's the meaning of "CWnd is already created"? I try to create the
> activeX control in the constructor of class derived from CWnd. And how do I
> know the m_hWnd has a value?

A CWnd is merely a wrapper around a Win32 window. You can't do much
with a CWnd until it is connected to an actual Win32 window. The CWnd
is not a window until its Create function has been called. So its
m_hWnd value is NULL until after the Create function has been called.

You cannot create a child window until after its parent window has been
created. You can create a child window in the parent's OnCreate message
handler, but you cannot create a child window in the constructor.

--
Scott McPhillips [VC++ MVP]

From: Victor on
Take a look at MSDN article "PRB: Dynamic Creation of Redistributable
Control Fails"
(Article ID: Q151804)
Is it your case?

Regards,

Victor

"ccwork" <ccwork(a)discussions.microsoft.com> wrote in message
news:7F11C3BC-5063-4246-8C82-DFB5788C07B7(a)microsoft.com...
> Hi,
> I try to insert ActiveX to my program as following:
>
> class MyWnd:public CFrameWnd
> {
> public:
> MyWnd()
> {
> CString strControlProgid;
> strControlProgid="mscal.calendar.7";
> m_Control.DestroyWindow();
> RECT rc;
> ::GetClientRect(m_hWnd,&rc);
> BOOL bStat=FALSE;
> bStat=m_Control.CreateControl(strControlProgid, "",
WS_VISIBLE,
> rc,
> this, 5000, NULL, FALSE, NULL);
> if (bStat == FALSE)
> {
> ::MessageBox (m_hWnd,"Error!!","Could not place
> control",MB_OK);
> }
> }
> private:
> CWnd m_Control;
> }
> class MyApp:public CWinApp
> {
> MyWnd *wnd;
> public:
> BOOL InitInstance()
> {
> AfxEnableControlContainer();
> wnd=new MyWnd();
> }
> }
>
> The error prompt "Error!! Could not place control" always appear. Anyone
> know what the problem?