【问题标题】:Translating Delphi VarSupports to C++ Builder将 Delphi VarSupports 转换为 C++ Builder
【发布时间】:2013-10-30 15:25:42
【问题描述】:

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

【问题讨论】:

    标签: delphi c++builder ihtmldocument2


    【解决方案1】:

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

    在我看来,代码似乎试图将OVStyleSheet 分配给IHTMLStyleSheet 接口样式表。在 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");
    }
    

    这可以编译,但我没有测试过。

    【讨论】:

    • 谢谢,但如果我这样分配它就无法编译 - E2285 找不到匹配的 'operator _di_IHTMLStyleSheet::= (OleVariant)'
    • 您必须先从 OleVariant 获取接口——我已经更正了上面的代码。如前所述,它可以编译,但我尚未对其进行功能测试。
    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多