コンピュータや音楽の事書いてます

PowerPointにはScreenUpdatingがない

Excelで重い描画処理をする場合、ScreenUpdatingプロパティを使えば高速化出来る。しかし、PowerPointにはこのプロパティが無い為困る。
この様にしたら速くなった。

準備
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;

ScreenUpdatingの代わり

    private PPT.Application PPTapp = new PPT.Application();
    [DllImport("user32.dll")]
    //wpを1にすると描画禁止 0にすると許可
    private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wp, IntPtr lp);
    private const uint WM_SETREDRAW = 0x000B;
    [DllImport("user32.dll")]
    private static extern bool LockWindowUpdate(IntPtr Handle);
    private void W32LockWindowUpdate(dynamic w, bool lock_)
    {
        IntPtr hwnd, res1;
        if (w is PPT.SlideShowWindow)
        {
            hwnd = (IntPtr)((PPT.SlideShowWindow)w).HWND;
        }
        else if (w is PPT.DocumentWindow)
        {
            hwnd = (IntPtr)((PPT.DocumentWindow)w).HWND;
        }
        else return;
        if (lock_)
        {
            res1 = SendMessage(hwnd, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
        }
        else
        {
            res1 = SendMessage(hwnd, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
            hwnd = IntPtr.Zero;
        }
        bool res2 = LockWindowUpdate(hwnd);
        if (!lock_)
        {
            try {
            w.View.Zoom -= 1;
            w.View.Zoom += 1;
            } catch { }
        }        
        return;
    }
使用法
    //描画を止める指示。
    W32LockWindowUpdate(getNormalWindow(), true); //編集などをするWindow
    W32LockWindowUpdate(PPTapp.ActivePresentation.SlideShowWindow, true); //スライドショーのWindow
    /*
    〜〜〜重い描画処理〜〜〜
    */
    //止めたWindowを元に戻す。
    W32LockWindowUpdate(getNormalWindow(), false);
    W32LockWindowUpdate(PPTapp.ActivePresentation.SlideShowWindow, false);
本題とは関係ないけど、あると便利。
    //NormalなWindowを探す
    PPT.DocumentWindow getNormalWindow()
    {
        foreach (PPT.DocumentWindow w in PPTapp.ActivePresentation.Windows)
        { 
            try
            {
                if (w.ViewType == PPT.PpViewType.ppViewNormal)
                {
                    return w;
                }
                else MessageBox.Show("NormalでないWindow"); //これは多分発生しない
            }
            catch
            {
                //発表者ツールだとViewTypeが読めない為、ここにくる
            }
        }
        return null;
    }