Powerpoint VSTO スライド上のポイント座標と画面全体のピクセル座標の関係
Powerpointスライド上の図形やテキストなどの座標はポイントで表される。黒い余白部分は含まない。
マウスを使ったPowerpointアプリをつくる場合、マウスのカーソル位置は黒い余白部分を含む画面全体のピクセル座標で検知される為、スライド上のオブジェクトにマウスカーソルが当たっているかどうかの判断は非常に厄介なものになる。
VSTOのライブラリの中に、この座標変換や、余白部分の幅が分かる様な機能は見当たらない。
そこで、黒い余白部分の大きさを計算し、画像の位置とサイズをポイントではなく、ピクセル座標で求める方法を開発した。
//こういうオブジェクトがあったとして、 Presentation p; Shape buttonNext; //画面上のボタンなどの図形/画像 //画面全体サイズのピクセル座標 黒い余白も含んでいる int screenW = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; int screenH = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; //スライド部分のポイント座標 余白は含んでいない float slideW = p.PageSetup.SlideWidth; float slideH = p.PageSetup.SlideHeight; //ピクセルとポイントとの比率 float ratioX = screenW / slideW; float ratioY = screenH / slideH; //黒い空白が左右なのか、上下なのか判定し、余白部分を求める float ratioScreen = (float)screenW / screenH; float ratioSlide = slideW / slideH; float ratio = ratioScreen > ratioSlide ? ratioY : ratioX; int marginX = (int)(screenW - slideW * ratio) / 2; //どちらかは0になる int marginY = (int)(screenH - slideH * ratio) / 2; // //画面全体ピクセル座標で見た場合のボタンの位置とサイズ int bx = (int)(buttonNext.Left * ratio + marginX); int by = (int)(buttonNext.Top * ratio + marginY); int bw = (int)(buttonNext.Width * ratio); int bh = (int)(buttonNext.Height * ratio);