将Delphi VarSupports翻译成C ++ Builder

时间:2013-10-21 23:02:43

标签: delphi c++builder ihtmldocument2

我正在尝试将此代码从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;

一切顺利,直到这一行:

    if (VarSupports(OVStyleSheet, IID_IHTMLStyleSheet, StyleSheet))

它报告:E2285无法找到'VarSupports(OleVariant,_GUID,_di_IHTMLStyleSheet)'的匹配项

其他一切都很好。任何人都可以帮我上线吗?

到目前为止我的翻译:

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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

编译错误的原因是VarSupports被定义为采用Variant,并且您传递的是OleVariant

我认为代码正在尝试将OVStyleSheet分配给IHTMLStyleSheet接口StyleSheet。在C ++ Builder中,您应该能够分配它,就像在

中一样
_di_IInterface inter = _di_IInterface(OVStyleSheet);
StyleSheet = inter;

如果成功且StyleSheet不为NULL,则应该能够使用StyleSheet。请注意,无效的Variant分配可能会引发异常,因此您可能希望处理该异常(并假设该异常也意味着OVStyleSheet不支持IHTMLStyleSheet接口)

此外,C ++ Builder有一个Interface.Supports函数,它看起来像VarSupports那样做,除了VarSupports采用一个变体,所以Interface.Supports还要求你自己从OleVariant获取接口。可能类似于:

di_IInterface inter = _di_IInterface(OVStyleSheet);
if (inter->Supports(StyleSheet))
{
    ShowMessage("StyleSheet has been assigned");
}

编译,但我没有测试过。