JUL
29
前のエントリでは
表示モードを「HTMLコード」としていた場合、HTMLDocumentに対してプラグインから操作後に表示モードを「標準」に戻すと、HTMLDocumentに対する操作内容が飛んでしまう
という問題で躓いていたが、HTMLDocumentに対して操作を行う前に表示モードを「標準」にしておくことで対処できた。
こんな感じ。HTMLDocumentを直接操作できるようになって、うはー 夢が広がりんぐ
なお、表示モードの切り替えがタブ式になったTechnology Preview版限定。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
using WindowsLive.Writer.BlogClient;
using WindowsLive.Writer.BlogClient.Clients;
using WindowsLive.Writer.PostEditor;
using WindowsLive.Writer.Extensibility.BlogClient;
using WriterMshtml = WindowsLive.Writer.Mshtml;
namespace SharpLab.WPCustomFieldsEditor {
[WriterPlugin("0aaad6ce-da8b-4f78-a73c-c73f3c6c163d", "WPCustomFieldsEditor", PublisherUrl = "http://www.sharplab.net/")]
public class PublishObserver : PublishNotificationHook {
public override bool OnPrePublish(System.Windows.Forms.IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish) {
PostEditorForm postEditorForm = getPostEditorForm(dialogOwner);
WindowsLive.Writer.Controls.MiniTabsControl MiniTabsControl = (WindowsLive.Writer.Controls.MiniTabsControl)findControlByName(postEditorForm, "MiniTabsControl", "WindowsLive.Writer.Controls");
MiniTabsControl.SelectTab(0);
WriterMshtml.MshtmlEditor mshtmlEditor = (WriterMshtml.MshtmlEditor)findControlByName(postEditorForm, "MshtmlEditor", "WindowsLive.Writer.Mshtml");
mshtml.IHTMLDocument2 doc = mshtmlEditor.HTMLDocument;
//あとは煮るなり焼くなり。
}
private PostEditorForm getPostEditorForm(IWin32Window postProgressForm) {
return (PostEditorForm)((Form)Form.FromHandle(postProgressForm.Handle)).Owner;
}
private Control findControlByName(Control control, string controlName, string controlNamespace) {
foreach (Control cChild in control.Controls) {
Type t = cChild.GetType();
if (t.Name == controlName && t.Namespace == controlNamespace) {
return cChild;
}
if (cChild.HasChildren) {
Control returned = findControlByName(cChild, controlName, controlNamespace);
if (returned != null) {
return returned;
}
}
}
return null;
}
}
}
この春はゆるふわ愛されiKnowアイテムバンクでキメちゃおう☆