[英]Translating Delphi VarSupports to C++ Builder
I am attempting to translate this code from Delphi to C++ Builder: 我正在尝试将此代码从Delphi转换为C ++ Builder:
procedure HandleStyleSheets(const Document: IDispatch);
var
Doc: IHTMLDocument2; // document object
StyleSheets: IHTMLStyleSheetsCollection; // document's style sheets
SheetIdx: Integer; // loops thru style sheets
OVSheetIdx: OleVariant; // index of a style sheet
StyleSheet: IHTMLStyleSheet; // reference to a style sheet
OVStyleSheet: OleVariant; // variant ref to style sheet
RuleIdx: Integer; // loops thru style sheet rules
Style: IHTMLRuleStyle; // ref to rule's style
begin
// Get IHTMLDocument2 interface of document
if not Supports(Document, IHTMLDocument2, Doc) then
Exit;
// Loop through all style sheets
StyleSheets := Doc.styleSheets;
for SheetIdx := 0 to Pred(StyleSheets.length) do
begin
OVSheetIdx := SheetIdx; // sheet index as variant required for next call
// Get reference to style sheet (comes as variant which we convert to
// interface reference)
OVStyleSheet := StyleSheets.item(OVSheetIdx);
if VarSupports(OVStyleSheet, IHTMLStyleSheet, StyleSheet) then
begin
// Loop through all rules within style a sheet
for RuleIdx := 0 to Pred(StyleSheet.rules.length) do
begin
// Get style from a rule and reset required attributes.
// Note: style is IHTMLRuleStyle, not IHTMLStyle, although many
// attributes are shared between these interfaces
Style := StyleSheet.rules.item(RuleIdx).style;
Style.backgroundImage := ''; // removes any background image
Style.backgroundColor := ''; // resets background colour to default
end;
end;
end;
end;
Everything went fine until this line: 一切顺利,直到这一行:
if (VarSupports(OVStyleSheet, IID_IHTMLStyleSheet, StyleSheet))
It reports: E2285 Could not find a match for 'VarSupports(OleVariant,_GUID,_di_IHTMLStyleSheet)' 它报告:E2285找不到与“ VarSupports(OleVariant,_GUID,_di_IHTMLStyleSheet)”的匹配项
Everything else translated just fine. 其他所有翻译都很好。 Can anyone help me with above line?
有人可以帮我解决上述问题吗?
My translation so far: 到目前为止,我的翻译是:
DelphiInterface<IHTMLDocument2> Doc; // document object
DelphiInterface<IHTMLStyleSheetsCollection> StyleSheets; // document's style sheets
int SheetIdx; // loops thru style sheets
OleVariant OVSheetIdx; // index of a style sheet
DelphiInterface<IHTMLStyleSheet> StyleSheet; // reference to a style sheet
OleVariant OVStyleSheet; // variant ref to style sheet
int RuleIdx; // loops thru style sheet rules
DelphiInterface<IHTMLRuleStyle> Style; // ref to rule's style
DelphiInterface<IHTMLStyleSheetRule> StyleSheetRule;
// Get IHTMLDocument2 interface of document
if (!Supports(EmbeddedWB1->Document, IID_IHTMLDocument2, Doc)) throw Exception("Not supported");
// Loop through all style sheets
StyleSheets = Doc->styleSheets;
for (SheetIdx = 0; SheetIdx < StyleSheets->length; SheetIdx++)
{
OVSheetIdx = SheetIdx; // sheet index as variant required for next call
// Get reference to style sheet (comes as variant which we convert to interface reference)
StyleSheets->item(OVSheetIdx, OVStyleSheet);
if (VarSupports(OVStyleSheet, IID_IHTMLStyleSheet, StyleSheet))
{
// Loop through all rules within style a sheet
for (RuleIdx = 0; RuleIdx < StyleSheet->rules->length; RuleIdx)
{
// Get style from a rule and reset required attributes.
// Note: style is IHTMLRuleStyle, not IHTMLStyle, although many
// attributes are shared between these interfaces
StyleSheet->rules->item(RuleIdx, StyleSheetRule);
Style = StyleSheetRule->style;
Style->backgroundImage = L""; // removes any background image
Style->backgroundColor = L""; // resets background colour to default
}
}
}
}
The reason for the compile error is that VarSupports is defined as taking a Variant
, and you are passing an OleVariant
. 发生编译错误的原因是VarSupports被定义为采用
Variant
,并且您正在传递OleVariant
。
It looks to me as if the code is trying to assign the OVStyleSheet
to the IHTMLStyleSheet
interface StyleSheet. 在我看来,似乎代码正在尝试将
OVStyleSheet
分配给IHTMLStyleSheet
接口StyleSheet。 In C++ Builder, you should be able to just assign it, as in 在C ++ Builder中,您应该能够对其进行分配,如下所示:
_di_IInterface inter = _di_IInterface(OVStyleSheet);
StyleSheet = inter;
If that succeeds and StyleSheet
is not NULL, you should be able to use StyleSheet
. 如果成功,并且
StyleSheet
不为NULL,则应该可以使用StyleSheet
。 Note that invalid Variant assignments can throw an exception, so you might want to handle that (and assume that the exception also means that the OVStyleSheet
does not support the IHTMLStyleSheet
interface) 请注意,无效的Variant分配会引发异常,因此您可能要处理该异常(并假定该异常还意味着
OVStyleSheet
不支持IHTMLStyleSheet
接口)
Also, C++ Builder has an Interface.Supports function that appears to do what VarSupports does, except that VarSupports takes a variant, so Interface.Supports also requires you to obtain the interface from the OleVariant yourself. 此外,C ++ Builder具有一个Interface.Supports函数,该函数似乎可以完成VarSupports的工作,但VarSupports具有一个变体,因此Interface.Supports还要求您自己从OleVariant获取接口。 Probably something like:
大概是这样的:
di_IInterface inter = _di_IInterface(OVStyleSheet);
if (inter->Supports(StyleSheet))
{
ShowMessage("StyleSheet has been assigned");
}
This compiles, but I have not tested it. 可以编译,但我尚未对其进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.