在 TWebBrowser 选择中设置字体大小
Set Font Size in TWebBrowser Selection
我有一个电子邮件客户端,它允许用户更改字体、字体大小、粗体、斜体等
我遇到的问题是,当我尝试向上或向下更改所选内容的字体大小时,我收到 "EVariantTypeCastError" 消息 "Could not convert variant of type Null into type OleStr"。 此异常在 TextRange.queryCommandValue('FONTSIZE').
上抛出
procedure TForm1.act_FontIncreaseExecute(Sender: TObject);
var
Selection: IHTMLSelectionObject;
HtmlPage: IHTMLDocument2;
TextRange: IHTMLTxtRange;
Parent: IHTMLElement2;
s: string;
i, mode: Integer;
begin
HtmlPage := self.HtmlEditor.Document as IHTMLDocument2;
Selection := HtmlPage.Selection;
TextRange := Selection.createRange as IHTMLTxtRange;
if (TextRange <> nil) then
begin
s := TextRange.queryCommandValue('FONTSIZE');
val(s, i, mode);
if mode = 0 then
HtmlPage.execCommand('FONTSIZE', False, inttostr(i + 1))
end;
end;
这是增加选区字体大小的正确方法吗?
编辑 1:
样本HTML:
<HTML><HEAD></HEAD>
<BODY>
<P>
<SPAN style='FONT-SIZE: 7pt;'>
Test Text
</SPAN>
</P>
</BODY></HTML>
看起来问题出在 FONT-SIZE 样式上。当它被取出时,不会抛出异常。我的最终目标是能够从 Outlook 复制和粘贴,这是一个精简的例子。当我使用 color:red 等其他样式时,不会抛出异常。所以看起来它只是 FONT-SIZE 有问题。
编辑 2
异常堆栈跟踪
如您所见,在某些情况下,查询将 return NULL
然后 Val()
命令将失败。
解决方法很简单,当你得到空值时假定标准字体大小:
procedure TForm1.FontIncreaseExecute;
var
Selection: IHTMLSelectionObject;
HtmlPage: IHTMLDocument2;
TextRange: IHTMLTxtRange;
s: OleVariant;
i, mode: Integer;
begin
HtmlPage := WebBrowser1.Document as IHTMLDocument2;
Selection := HtmlPage.Selection;
TextRange := Selection.createRange as IHTMLTxtRange;
if (TextRange <> nil) then
begin
s := TextRange.queryCommandValue('FONTSIZE');
if VarisNull(s) then
s := 0; // fall back to standard font size
Val(s, i, mode);
if mode = 0 then
HtmlPage.execCommand('FONTSIZE', False, inttostr(i + 1))
end;
end;
Is this the correct approach to increase the size of the font for the
selection?
.queryCommandValue('FONTSIZE')
指围绕文本范围(字体大小 1-7)的 FONT
标记:例如
<FONT size=1>Test Text</FONT>
在您的 HTML 示例中没有 FONT
标签。需要处理周围SPAN
.
的FONT-SIZE
Style属性(CSS)
例如(没有错误检查以简化示例):
if (TextRange <> nil) then
begin
...
ShowMessage(TextRange.parentElement.style.fontSize);
end;
这将显示 7pt
。
@whosrdaddy 解释了您的具体异常原因(queryCommandValue
returns null 因为我解释的原因)
我有一个电子邮件客户端,它允许用户更改字体、字体大小、粗体、斜体等
我遇到的问题是,当我尝试向上或向下更改所选内容的字体大小时,我收到 "EVariantTypeCastError" 消息 "Could not convert variant of type Null into type OleStr"。 此异常在 TextRange.queryCommandValue('FONTSIZE').
上抛出procedure TForm1.act_FontIncreaseExecute(Sender: TObject);
var
Selection: IHTMLSelectionObject;
HtmlPage: IHTMLDocument2;
TextRange: IHTMLTxtRange;
Parent: IHTMLElement2;
s: string;
i, mode: Integer;
begin
HtmlPage := self.HtmlEditor.Document as IHTMLDocument2;
Selection := HtmlPage.Selection;
TextRange := Selection.createRange as IHTMLTxtRange;
if (TextRange <> nil) then
begin
s := TextRange.queryCommandValue('FONTSIZE');
val(s, i, mode);
if mode = 0 then
HtmlPage.execCommand('FONTSIZE', False, inttostr(i + 1))
end;
end;
这是增加选区字体大小的正确方法吗?
编辑 1:
样本HTML:
<HTML><HEAD></HEAD>
<BODY>
<P>
<SPAN style='FONT-SIZE: 7pt;'>
Test Text
</SPAN>
</P>
</BODY></HTML>
看起来问题出在 FONT-SIZE 样式上。当它被取出时,不会抛出异常。我的最终目标是能够从 Outlook 复制和粘贴,这是一个精简的例子。当我使用 color:red 等其他样式时,不会抛出异常。所以看起来它只是 FONT-SIZE 有问题。
编辑 2
异常堆栈跟踪
如您所见,在某些情况下,查询将 return NULL
然后 Val()
命令将失败。
解决方法很简单,当你得到空值时假定标准字体大小:
procedure TForm1.FontIncreaseExecute;
var
Selection: IHTMLSelectionObject;
HtmlPage: IHTMLDocument2;
TextRange: IHTMLTxtRange;
s: OleVariant;
i, mode: Integer;
begin
HtmlPage := WebBrowser1.Document as IHTMLDocument2;
Selection := HtmlPage.Selection;
TextRange := Selection.createRange as IHTMLTxtRange;
if (TextRange <> nil) then
begin
s := TextRange.queryCommandValue('FONTSIZE');
if VarisNull(s) then
s := 0; // fall back to standard font size
Val(s, i, mode);
if mode = 0 then
HtmlPage.execCommand('FONTSIZE', False, inttostr(i + 1))
end;
end;
Is this the correct approach to increase the size of the font for the selection?
.queryCommandValue('FONTSIZE')
指围绕文本范围(字体大小 1-7)的 FONT
标记:例如
<FONT size=1>Test Text</FONT>
在您的 HTML 示例中没有 FONT
标签。需要处理周围SPAN
.
FONT-SIZE
Style属性(CSS)
例如(没有错误检查以简化示例):
if (TextRange <> nil) then
begin
...
ShowMessage(TextRange.parentElement.style.fontSize);
end;
这将显示 7pt
。
@whosrdaddy queryCommandValue
returns null 因为我解释的原因)