« Pluginの作成(第一回) | メイン | BlogWriteII正式版公開 »
2005年06月18日
Pluginの作成(第二回)
第一回に続いて、もうちょっと詳しいプラグインのサンプルです。実際的なケースを想定しています。雛形としてお使いください。このサンプルは、NowListeningプラグインとして作ったものを元にしています。 基本的に、BlogWriteのエディタ画面でボタンをクリックすると、編集中の文章にプラグイン側から文字列が追加されます。
プロジェクトファイルは前回と同じく、一行だけ変更します。ユニットは下記に全部載せています。
#PluginNowListening.pas
unit PluginNowListening;
interface
uses
Windows, Messages, SysUtils, Classes, Dialogs, Forms, Controls,JvPlugin,
SynEdit,(*BlogWriteでSynEditコンポーネントを利用しているため、キャスト時に使います*)
SHDocVw_TLB,MSHTML_TLB,ActiveX,ComObj,(*IEのHTMLコンポーネント用*)
StdCtrls,(*TCheckbox*)
ComCtrls(*TPageControl*);
type
TPluginNowListening = class(TJvPlugin)
procedure JvPlugInInitialize(Sender: TObject; var AllowLoad: Boolean);
procedure JvPlugInConfigure(Sender: TObject);
procedure JvPlugInPluginMessage(Sender: TObject; APluginMessage: Integer; AMessageText: String);
procedure PluginNowListeningCommands0Execute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function RegisterPlugin: TJvPlugin; stdcall;
implementation
{$R *.dfm}
// IMPORTANT NOTE: If you change the name of the Plugin container,
// you must set the type below to the same type. (Delphi changes
// the declaration, but not the procedure itself. Both the return
// type and the type created must be the same as the declared type above.
function RegisterPlugin: TJvPlugin;
begin
Result := TPluginNowListening.Create(nil);
end;
procedure TPluginNowListening.JvPlugInInitialize(Sender: TObject;var AllowLoad: Boolean);
begin
//初期化時の処理。BlogWriteでは新規記事作成で、エディタ画面がCreateされる時に呼ばれる。
end;
procedure TPluginNowListening.JvPlugInConfigure(Sender: TObject);
begin
//プラグイン設定画面を表示できる。BlogWrite側でまだプラグイン管理画面を作っていないため、現在は表示されない。
end;
procedure TPluginNowListening.JvPlugInPluginMessage(Sender: TObject;APluginMessage: Integer; AMessageText: String);
begin
//ShowMessage(Format('Plugin Message number %d received. MessageText: %s', [APluginMessage, AMessageText]));
//BlogWrite側から特にメッセージを送る事は今のところしていませんが、今後投稿時や、特定のタイミングでMessageを飛ばすことがあるかもしれません。
end;
procedure TPluginNowListening.PluginNowListeningCommands0Execute(Sender: TObject);
var
Comp0 : TComponent;
Comp1 : TComponent;
Comp2 : TComponent;
s: string;
begin
s:='プラグインから追加';//追加する文字列
Comp0:= Manager.Owner.FindComponent('PageControlEx1'); //Pageコントロール取得
if not Assigned(Comp0) then exit;
if TPageControl(Comp0).ActivePage.Name = 'TabSheetMain' then begin //本文編集タブ表示中である
Comp1 :=Manager.Owner.FindComponent('SynEditMain'); //HTMLエディタを取得
if Assigned(Comp1) then begin
if TSynEdit(Comp1).Visible then begin //HTMLエディタを表示中である
Comp2:=Manager.Owner.FindComponent('CheckBoxLineBreaks'); //オプションの「自動改行」チェックボックス
if Assigned(Comp2) then begin
if TCheckBox(Comp2).checked then begin //「自動改行」がオンである
TSynEdit(Comp1).lines.Add(s); //HTMLエディタに文字列を追加
end else begin
TSynEdit(Comp1).lines.Add('<p>'+s+'</p>'); //HTMLエディタにHTML文字列を追加
end;
//Dont't forget!
TSynEdit(Comp1).Modified :=true; //文字列を変更したら、変更フラグを立てる
end else begin
TSynEdit(Comp1).lines.Add('<p>'+s+'</p>'); //念のため
TSynEdit(Comp1).Modified :=true;
end;
end else begin //デザインモードで編集中である
Comp1 :=Manager.Owner.FindComponent('MyWebBrowser1');
if TWebBrowser(Comp1).Visible then begin
if Assigned(TWebBrowser(Comp1).Document) then begin
if IHTMLDocument2(TWebBrowser(Comp1).Document).readyState = 'complete' then begin
IHTMLDocument2(TWebBrowser(Comp1).Document).body.innerHTML := IHTMLDocument2(TWebBrowser(Comp1).Document).body.innerHTML+#13#10+'<p>'+s+'</p>';
end;
end;
end;
end;
end;
end else if TPageControl(Comp0).ActivePage.Name = 'TabSheetExtended' then begin
Comp1 :=Manager.Owner.FindComponent('SynEditExtended');
if Assigned(Comp1) then begin
//以下本文編集とほぼ同じ
//.....
if TSynEdit(Comp1).Visible then begin //HTMLエディタを表示中である
//.....
end else begin //デザインモードで編集中である
Comp1 :=Manager.Owner.FindComponent('MyWebBrowser2');
//.....
end;
end;
end else if TPageControl(Comp0).ActivePage.Name = 'TabSheetOption' then begin
end else if TPageControl(Comp0).ActivePage.Name = 'TabSheetPreview' then begin
end else begin
end;
end;
end.
ご覧のように、ある程度コンポーネント名とか分かっていないと作れない所があると思います。どんな機能のものを作るかによってどこまで知る必要があるかが異なると思いますので、個別に問い合わせていただけるとお手伝いできるかと思います。
以下、NowListeningプラグインの実行結果サンプル
今聴いている曲:Lynyrd Skynyrd - 「Sweet Home Alabama」 (Skynyrd's Innyrds - Lynyrd Sky)
今聴いている曲:Outkast - 「Im Sorry Ms. Jackson...」 (Stankonia)
投稿者 BlogWrite担当 : 2005年06月18日 08:03
トラックバック
このエントリーのトラックバックURL:
http://www.witha.jp/b/mt-tb-hate-spam.cgi/272

