解決 Google Earth 嵌入控制項隨表單縮放而自動調整大小的問題,核心在於傳送 WM_COMMAND、WM_QT_PAINT、WM_SIZE 訊息並重新計算視窗矩形。
private void ResizeGoogleControl() {
NativeMethods.SendMessage(GEHWnd, (uint)WM_COMMAND, WM_PAINT, 0);
NativeMethods.PostMessage(GEHWnd, WM_QT_PAINT, 0, 0);
RECT mainRect, clientRect;
NativeMethods.GetWindowRect(GEHWnd, out mainRect);
NativeMethods.GetClientRect(GEHrender, out clientRect);
int offsetW = mainRect.Width - clientRect.Width;
int offsetH = mainRect.Height - clientRect.Height;
int newWidth = this.Control.Width + offsetW;
int newHeight = this.Control.Height + offsetH;
NativeMethods.SetWindowPos(GEHWnd, HWND_TOP, 0, 0, newWidth, newHeight, SWP_FRAMECHANGED);
NativeMethods.SendMessage(GEHWnd, (uint)WM_COMMAND, WM_SIZE, 0);
}
此方法需在表單 Resize 事件中呼叫,確保 GE 畫面隨容器即時縮放。