如何读取中的属性值

herolzq 2003-12-01 03:20:51
各位大侠:

请问怎样才能把<param >中的参数读出来???
...全文
231 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
brave_heart 2003-12-24
  • 打赏
  • 举报
回复
>>WHY?

我用VC6编过IE里面的控件,就用上面文章中的方法,没有问题。

至于,.NET2003中的例子,应当是用ATL7编的吧?
brave_heart 2003-12-23
  • 打赏
  • 举报
回复
I hope the following article can help you.

////////////////////////////
//
////////////////////////////
Initializing from HTML
In the same way that you can initialize a Visual Basic ActiveX control, you can initialize an ATL control using the <PARAM> tags. When you include the <PARAM> tags as children of the <OBJECT> tags, Internet Explorer sends the data that you specify to the control when the control is first instantiated. For example, to specify an initial value for the Message property, you can insert a <PARAM> tag between the opening and closing object tags for the control. This HTML code looks like this:

<OBJECT ID="AtlCtrl"
CLASSID="CLSID:638B718E-AEF1-11D2-A9BA-444553540001">
<PARAM NAME="Message" VALUE="Hello, There!">
</OBJECT>




This data is stored in a property bag object maintained by Internet Explorer. Internet Explorer will query the control for the IPersistPropertyBag interface. If the control returns a pointer to this interface, Internet Explorer calls the Load method of IPersistPropertyBag to instruct the control to load initialization data. When calling the Load method, Internet Explorer passes a pointer to IPropertyBag interface of its property bag object. The control then calls the Read method of the passed in IPropertyBag interface to retrieve the initialization data.

To make this work correctly in the AtlCtrl control, you need to follow a few steps. As usual, ATL provides a default implementation of IPersistPropertyBag named IPersistPropertyBagImpl. Add this template class to the end of the inheritance list. After adding the template class, the declaration of CAtlCtrl should look like this:

class ATL_NO_VTABLE CAtlCtrl :


public IPersistPropertyBagImpl<CAtlCtrl>
{


};




Add the IPersistPropertyBag interface to the COM map. After adding this interface, the COM map should look like this:

BEGIN_COM_MAP(CAtlCtrl)


COM_INTERFACE_ENTRY(IPersistPropertyBag)
END_COM_MAP()




Implement the Load method, as shown in the next code fragment. You can add this method to the bottom of the class declaration. With Load, you can retrieve the initialization data that you specified on the Web page by using <PARAM> tags.

STDMETHOD(Load)(LPPROPERTYBAG pPropBag, LPERRORLOG pErrorLog)
{
CComVariant vtVal;

HRESULT hr = pPropBag->Read(L"Message", &vtVal, pErrorLog);

if (SUCCEEDED(hr) && VT_BSTR == vtVal.vt)
{
m_bstrMessage = vtVal.bstrVal;
m_fDirty = TRUE;
}

return hr;
}




In the Load method, we first have to declare a variable of CComVariant. The CComVariant variable is similar to CComBSTR in that it's a wrapper class that provides greater functionality than the data type it contains. In this case, a CComVariant wraps a VARIANT type. This variable is needed to retrieve the values of the parameters specified using <PARAM> tags. (If you need more information about CComVariant, refer to MSDN.)

Next we call the Read method of the IPropertyBag interface that was passed into the Load method. The first parameter specifies the name of the property that we want to retrieve. This parameter tacks on the 'L' specifier that converts the ANSI string to an OLE string. Here we're reading the Message property from the property bag. The second parameter is a VARIANT that will contain the value of the Message property when this method returns. The third parameter is a pointer to IErrorLog interface that the Read method uses to report errors. This parameter can be NULL if we're not interested in error information. In this case, we simply pass the pointer to IErrorLog interface that we received from the argument of the Load method.

Then we check the return value of the Read method. If the call to Read succeeded and the type of the VARIANT that was returned is a BSTR (as indicated by vtVal.vt being equal to VT_BSTR), we set the m_bstrMessage data member to the BSTR value that is contained in the vtVal variable. Now, when the OnDraw method is initially called, the value that we specified for the Message property will be displayed on the Web page. In addition to setting the m_bstrMessage data member, we also set the m_fDirty data member to TRUE. The m_fDirty data member indicates that the property has been changed. Here's how m_fDirty is defined in the CAtlCtrl class:

BOOL m_fDirty;




We should also initialize m_fDirty in the CAtlCtrl constructor. The constructor will look like this:

CAtlCtrl() : m_bstrMessage(bstrDefaultMsg),
m_fDirty(FALSE)
{
}



triout 2003-12-23
  • 打赏
  • 举报
回复
很感谢楼上的。

但我在.NET2003中自带的一个AtlAnsyc示例中,根本就没有发现什么load的实现,我搜索所有与属性名相关的单词也没有找到。

WHY?
triout 2003-12-22
  • 打赏
  • 举报
回复
up
楼主,麻烦告知如何解决你开始的问题,就是如何获得属性参数?
我定义了一个属性:FunName,在Html中设置了,但怎样修改get和put函数都无效,不能获得我在Html中设置的值。
herolzq 2003-12-02
  • 打赏
  • 举报
回复
完整的Load()函数,请帮忙看看吧!!!

STDMETHOD(Load)(IPropertyBag *pBag, IErrorLog *pLog)
{

CComVariant v;
BOOL hr ;
int port ;

v.vt = VT_I4;
v = 0L;

hr = pBag->Read(OLESTR("ServerPort"), &v, pLog);

if(SUCCEEDED(hr))
{
port = v.lVal ;
}
//::MessageBox(NULL,"aaa", "提示信息", MB_OK | MB_ICONINFORMATION);

// BSTR x = SysAllocString(L"Hello");

//CComBSTR myCComBSTR(L"Hello");

LPTSTR pstr = NULL;

v.vt = VT_BSTR;

//v.bstrVal = myCComBSTR ;

hr = pBag->Read(OLESTR("ServerIP"), &v, pLog);


//pstr = (LPTSTR)v.bstrVal ;

if(SUCCEEDED(hr))
{
//pstr = W2T(v.bstrVal);

::MessageBox(NULL,(char *)v.bstrVal, "提示信息", MB_OK | MB_ICONINFORMATION);
}
else
{
::MessageBox(NULL,"读参数失败", "提示信息", MB_OK | MB_ICONINFORMATION);
}

//SysFreeString(x);

return S_OK;

}

MessageBox中显示的结果是“1”,应该是“192.168.0.188”
wangweixing2000 2003-12-02
  • 打赏
  • 举报
回复
你看看v.bstrVal 里面有没有值,还有这句pBag->Read(OLESTR("ServerIP"), &v, pLog);你给的信息太少,我怎么来看呀!给你几个转换字符串的例子:也许你能找到答案!如果不行再找我!
//Create a BSTR and assign it to a Variant
BSTR x = SysAllocString(L"Hello");
VARIANT myVariant;
myVariant.vt = VT_BSTR;
myVariant.bstrVal = x;
SysFreeString(x);

//Create a CString and change it to a variant;
CString myCString(_T("My String"));
CString mySecondString;


//This is required to use the T2COLE macro.
USES_CONVERSION;



BSTR y = SysAllocString(T2COLE(myCString));
myVariant.bstrVal = y;
mySecondString = y;
SysFreeString(y);


//Create two BSTRs and add them.
BSTR a = SysAllocString(L"One two ");
BSTR b = SysAllocString(L"three four.");
_bstr_t my_bstr_t(a, TRUE);
my_bstr_t += b;
myVariant.bstrVal = my_bstr_t;
// or
myVariant.bstrVal = _bstr_t(a, FALSE) + b;

//Change a bstr to a CString.
CString ANewString(b);
//or if CString already exists.
myCString = b;


//Use of CComBSTR
CComBSTR myCComBSTR(L"Hello");
myCComBSTR.Append(L", how are you?");
VARIANT varFromCCom;
varFromCCom.vt = VT_BSTR;
varFromCCom.bstrVal = myCComBSTR;
gjd111686 2003-12-02
  • 打赏
  • 举报
回复
.h
private:
CString m_ServerName;
protected:
BSTR GetServerName(void);
void SetServerName(LPCTSTR newVal);
.cpp
BSTR CWebOfficeCtrl::GetServerName(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

// TODO: 在此添加调度处理程序代码
return m_ServerName.AllocSysString();
}

void CWebOfficeCtrl::SetServerName(LPCTSTR newVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

// TODO: 在此添加属性处理程序代码
if(m_ServerName.Compare(newVal)!=0)
{
m_ServerName=newVal;
}
SetModifiedFlag();
}
herolzq 2003-12-02
  • 打赏
  • 举报
回复

现在的问题:

{
LPTSTR pstr = NULL;

v.vt = VT_BSTR;

hr = pBag->Read(OLESTR("ServerIP"), &v, pLog);

pstr = (LPTSTR)v.bstrVal ;
}

但是pstr中只是整个字符串的第一个,如“192.168.0.1”,则只能显示 1
herolzq 2003-12-01
  • 打赏
  • 举报
回复
我用ATL做的ActiveX控件,请问如何从网页的<param>中读取参数,
如从:<param name="ServerName" value="LocalHost">中得到ServerName的value
gjd111686 2003-12-01
  • 打赏
  • 举报
回复
<object id=DWebOffice style="left: 0px; width: 100%; top: 0px; height: 800px" classid="clsid:05430EC0-69CA-437A-B1F9-4B78B8647BEA" viewastext>
<param name="_Version" value="65536">
<param name="_ExtentX" value="6959">
<param name="_ExtentY" value="2699">
<param name="_StockProps" value="0">
<param name="ServerName" value="LocalHost">
<param name="ServerPort" value="9003">
<param name="FileName" value="Http://LocalHost:9003/System/Project_DesignShow.jsp?Id=<%=Id%>">
<param name="Rule" value="#,2,1,2,#,2,2,2,#">
<!--<param name="ReturnData" value="">-->
<param name="InitData" value="#基础数据1#基础数据2#">
<param name="DesignFile" value="/System/Project_Design/NewResult.jsp">
</object>
<input type="button" value="打开模板" onclick="DWebOffice.OpenDoc();">
<input type="button" value="保存文档" onclick="DWebOffice.SetField('Category',GetCategory(document.BodyForm.Category));DWebOffice.SaveDoc();window.location.href='/System/Project_Design/List.jsp';">
<input type="button" value="初始数据" onclick="DWebOffice.SetData();">
<input type="button" value="获取数据" onclick="window.confirm(DWebOffice.ReturnData);">
<input type="button" value="加密数据" onclick="DWebOffice.SetProtect(1,1,'password');">
wangweixing2000 2003-12-01
  • 打赏
  • 举报
回复
说清楚点!

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧