1. 程式人生 > >呼叫OCX控制元件時的宣告和實現

呼叫OCX控制元件時的宣告和實現

FirstLessonOcx.h
#if !defined(AFX_FIRSTLESSONOCX_H__0CA538A0_1E9A_49D9_ADBE_0D21B83849DF__INCLUDED_)
#define AFX_FIRSTLESSONOCX_H__0CA538A0_1E9A_49D9_ADBE_0D21B83849DF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Machine generated IDispatch wrapper class(es) created by Microsoft Visual C++

// NOTE: Do not modify the contents of this file.  If this class is regenerated by
//  Microsoft Visual C++, your modifications will be overwritten.

/////////////////////////////////////////////////////////////////////////////
// FIRSTLESSONOCX wrapper class

class CFirstLessonOcx : public CWnd
{
protected:
	DECLARE_DYNCREATE(CFirstLessonOcx)
public:
	CLSID const& GetClsid()
	{
		static CLSID const clsid
			= { 0x58134c6c, 0x7b0e, 0x4a14, { 0xb0, 0x5b, 0x15, 0xc4, 0x9c, 0x6c, 0x6c, 0xdd } };
		return clsid;
	}
	virtual BOOL Create(LPCTSTR lpszClassName,
		LPCTSTR lpszWindowName, DWORD dwStyle,
		const RECT& rect,
		CWnd* pParentWnd, UINT nID,
		CCreateContext* pContext = NULL)
	{ return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID); }

	BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle,
		const RECT& rect, CWnd* pParentWnd, UINT nID,
		CFile* pPersist = NULL, BOOL bStorage = FALSE,
		BSTR bstrLicKey = NULL)
	{ return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID,
	pPersist, bStorage, bstrLicKey); }

	// Attributes
public:
	long StartMonitor(LPCTSTR szDevIDNO, long nChannel);
	void CloseAllSound();
	long GetLayout();
	void SetLayout(long propVal);
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FIRSTLESSONOCX_H__0CA538A0_1E9A_49D9_ADBE_0D21B83849DF__INCLUDED_)
FirstLessonOcx.cpp
// Machine generated IDispatch wrapper class(es) created by Microsoft Visual C++

// NOTE: Do not modify the contents of this file.  If this class is regenerated by
//  Microsoft Visual C++, your modifications will be overwritten.


#include "stdafx.h"
#include "FirstLessonOcx.h"

/////////////////////////////////////////////////////////////////////////////
// CCMSV6OCX

IMPLEMENT_DYNCREATE(CFirstLessonOcx, CWnd)

/////////////////////////////////////////////////////////////////////////////
// CFirstLessonOcx properties

long CFirstLessonOcx::StartMonitor(LPCTSTR szDevIDNO, long nChannel)
{
	long result;
	static BYTE parms[] = VTS_BSTR VTS_I4;
	InvokeHelper(0x1, DISPATCH_METHOD, VT_I4, (void*)&result, parms, szDevIDNO, nChannel);//適合有輸入引數,有返回值的排程介面
	return result;
}

void CFirstLessonOcx::CloseAllSound()
{
	InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);//無輸入引數無返回值,也可呼叫此介面
}

long CFirstLessonOcx::GetLayout()
{
	long result;
	GetProperty(0x3, VT_I4, (void*)&result);//適合無輸入引數,只獲取返回值的排程介面
	return result;
}

void CFirstLessonOcx::SetLayout(long propVal)
{
	SetProperty(0x4, VT_I4, propVal);//適合無返回的排程介面
}

ActiveXTestDlg.h
// ActiveXTestDlg.h : 標頭檔案
//

#pragma once
#include "FirstLessonOcx.h"

// CActiveXTestDlg 對話方塊
class CActiveXTestDlg : public CDialog
{
// 構造
public:
	CActiveXTestDlg(CWnd* pParent = NULL);	// 標準建構函式

// 對話方塊資料
	enum { IDD = IDD_ACTIVEXTEST_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支援

private:
	CFirstLessonOcx m_ocx;

// 實現
protected:
	virtual BOOL OnInitDialog();
};

ActiveXTestDlg.cpp
void CActiveXTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_ACTIVEXFIRSTLESSCTRL1, m_ocx);
}


BOOL CActiveXTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	//如果不繫結控制元件,可自己建立,必須用WS_CHILD,並指定父視窗  
	//m_ocx.Create(_T("My ocx"), WS_CHILD | WS_VISIBLE, CRect(10,10,100,30), this, 100);

	m_ocx.GetLayout();
}