delphi 01设置 字体属性

设置/获取 字体属性
  1. 名称
  2. 大小
  3. 粗体
  4. 斜体
  5. 下划线
  6. 删除线
  7. 颜色1
  8. 颜色2
 
uses MSHTML;
 
//设置
//------------------------------------------------------------------------------
procedure WB_SetFontName();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('FontName', False, Form1.cbb_FontNameList.Text);
end;
//------------------------------------------------------------------------------
procedure WB_SetFontSize();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('FontSize', false, StrToInt(Form1.cbb_FontSize.Text));
end;
//------------------------------------------------------------------------------
procedure WB_SetBold();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('Bold', False, 1);
end;
//------------------------------------------------------------------------------
procedure WB_Setstrike();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('StrikeThrough', False, 1);
end;
//------------------------------------------------------------------------------
procedure WB_SetItalic();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('Italic', False, 1);
end;
//------------------------------------------------------------------------------
procedure WB_SetUnderline();
begin
  (Form1.webbrowser1.Document as IHTMLDocument2).execCommand('Underline', False, 1);
end;
//------------------------------------------------------------------------------
//颜色要为 HTML 的颜色格式
procedure WB_SetForeColor();
begin
  (Form1.WebBrowser1.Document as IHTMLDocument2).execCommand('ForeColor', False, '#CD0000');
end;
//------------------------------------------------------------------------------
procedure WB_SetBackColor();
begin
  (Form1.WebBrowser1.Document as IHTMLDocument2).execCommand('BackColor',false, '#8EE5EE');
end;
 
 
 
//获得
//------------------------------------------------------------------------------
//First chance exception at $759E9617. Exception class EOleException with message '不支持该命令。'. Process Webbrowser.exe (6472)
function GetFontName():string;
begin
      Result:=(Form1.webbrowser1.Document as IHTMLDocument2).queryCommandValue('FontName');
end;
//------------------------------------------------------------------------------
function GetFontSize():string;
begin
    Result:=(Form1.webbrowser1.Document as IHTMLDocument2).queryCommandValue('FontSize');
end;
//------------------------------------------------------------------------------
function GetFontColor():TColor;
begin
      Result:=(Form1.webbrowser1.Document as IHTMLDocument2).queryCommandValue('ForeColor');
end;
//------------------------------------------------------------------------------
function GetFontBgColor():TColor;
begin
      Result:=(Form1.webbrowser1.Document as IHTMLDocument2).queryCommandValue('BackColor');
end;
//------------------------------------------------------------------------------
function IsBold():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('Bold');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsItalic():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('Italic');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsUnderline():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('Underline');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsStrikeThrough():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('StrikeThrough');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsSubScript():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('SubScript');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsSuperScript():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('SuperScript');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsJustifyLeft():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('JustifyLeft');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsJustifyCenter():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('JustifyCenter');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsJustifyRight():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('JustifyRight');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsJustifyFull():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('JustifyFull');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsInsertOrderedList():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('InsertOrderedList');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
function IsInsertUnorderedList():Boolean;
Var  bRtn:Boolean;
begin
    bRtn:= (Form1.webbrowser1.Document as IHTMLDocument2).queryCommandState('InsertUnorderedList');
    if bRtn then
        Result:=True
    else
        Result:=False;
end;
//------------------------------------------------------------------------------
//工具栏感应
 
procedure TForm1.WebBrowser1CommandStateChange(ASender: TObject;
  Command: Integer; Enable: WordBool);
begin

//------------------------------------------------------------------------------
  if WebBrowser1.ReadyState =READYSTATE_COMPLETE  then
  if GetSelText<>'' then
  begin
      mni_Copy.Enabled:=True;
      btn_Copy.Enabled:=True;
      btn_Cut.Enabled:=True;
      mni_Cut.Enabled:=True;
  end
  else
  begin
       mni_Copy.Enabled:=false;
       btn_Copy.Enabled:=False;
       btn_Cut.Enabled:=False;
       mni_Cut.Enabled:=false;
  end;
  //如果剪切板上没有内容则
  if IsClipboardFormatAvailable(CF_TEXT) then
  begin
     btn_Paste.Enabled:=True;
     mni_Paste.Enabled:=True;
  end
  else
  begin
     btn_Paste.Enabled:=False;
     mni_Paste.Enabled:=False;
  end;

//------------------------------------------------------------------------------

try
  //cbb_FontNameList.Text:=GetFontName();
  cbb_FontNameList.ItemIndex :=cbb_FontNameList.Items.IndexOf( GetFontName());
  //cbb_FontSize.Text:=GetFontSize();
   cbb_FontSize.ItemIndex :=cbb_FontSize.Items.IndexOf( GetFontSize());
  btn_Bold.Down:=IsBold();
  btn_Italic.Down:=IsItalic();
  btn_Underline.Down:=IsUnderline();
  btn_strikethrough.Down:=IsStrikeThrough();

  btn_SubScript.Down:=IsSubScript();
  btn_SuperScript.Down:=IsSuperScript();

  ToolButton_AlignTwo.Down:=IsJustifyFull();
  ToolButton_AlignLeft.Down:=IsJustifyLeft();
  ToolButton_AlignCenter.Down:=IsJustifyCenter();
  ToolButton_AlignRight.Down:=IsJustifyRight();

  ToolButton_UnoredredList.Down:=IsInsertUnorderedList();
  ToolButton_OrderedList.Down:=IsInsertOrderedList();
  //格式化
except
  Exit;
end;

  case Command of
    CSC_NAVIGATEBACK: ToolButton_Back.Enabled := Enable;    //“后退”按钮
    CSC_NAVIGATEFORWARD: ToolButton_Forward.Enabled := Enable;    //“前进”按钮
    CSC_UPDATECOMMANDS: ToolButton_Stop.Enabled := TWebBrowser(ASender).Busy;  //“停止”按钮
  end;
end;
 
 




转载于:https://www.cnblogs.com/xe2011/p/3876346.html

Delphi中,字体属性可以通过TFont类来设置。在自定义图形控件中,可以通过在控件的published区增加对应的字体属性来实现在不同区域显示不同字体的内容。例如,在一个继承自TGraphicControl类的自定义图形控件中,可以添加Text1Font和Text2Font属性来分别设置不同的字体。\[2\] 在设置字体属性时,可以使用TFont类的Assign方法来复制字体的各个字段值,将其存放在实例的字段中。这样可以确保在运行时对字体赋值时,值的来源在上下文环境中是确定且存在的。而在设计时,通过设计器直接对字体的各个子项赋值,实际上是在逐一对其字段赋值。需要注意的是,在设计时通过字体对话框进行赋值时,实际上是产生了一条Windows消息,消息传递完成之后内容就会销毁,所以使用:=赋值就会产生地址读写的异常。\[3\] 总结来说,Delphi中的字体属性可以通过TFont类来设置,可以使用Assign方法来复制字体的字段值,确保在运行时对字体赋值时的来源存在。在自定义图形控件中,可以通过增加对应的字体属性来实现不同区域显示不同字体的内容。 #### 引用[.reference_title] - *1* *2* *3* [Delphi自定义图形控件的自定义字体属性在设计期报错的解决办法](https://blog.csdn.net/weixin_33716941/article/details/91990710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值