簡體   English   中英

將Delphi VarSupports轉換為C ++ Builder

[英]Translating Delphi VarSupports 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;

一切順利,直到這一行:

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

發生編譯錯誤的原因是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");
}

可以編譯,但我尚未對其進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM