2013-10-21 19 views
1

我試圖翻譯德爾福此代碼C++ Builder中:翻譯德爾福VarSupports到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

的原因編譯錯誤是VarSupports被定義爲採取Variant,而你傳遞一個OleVariant

它看起來好像代碼試圖將OVStyleSheet分配到IHTMLStyleSheet接口StyleSheet。在C++ Builder中,你應該能夠公正分配給它,如

_di_IInterface inter = _di_IInterface(OVStyleSheet); 
StyleSheet = inter; 

如果成功和StyleSheet不爲NULL,你應該能夠使用StyleSheet。需要注意的是無效的變分配可以拋出異常,所以你可能要來處理(且假定異常也意味着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"); 
} 

這個編譯,但我沒有測試過它。

+0

謝謝,但如果我指定它就像它不能編譯 - E2285找不到匹配的操作符_di_IHTMLStyleSheet :: = (OleVariant)' – Coder12345

+0

你必須首先從OleVariant獲取接口 - 我已經糾正上面的代碼。如前所述,它編譯,但我沒有功能測試它。 – nachbar