首页 > 代码库 > delphi 01设置 字体属性

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;
 
 


来自为知笔记(Wiz)