我在dll中有一個實用程序功能,可以在主機應用程序屏幕上居中顯示我的表單。我正在使用RAD Studio XE2。我必須手動執行它,因爲主機應用程序是非VCL並且TForm的表單放置參數無法正常工作。下面的代碼工作。這兩個函數都聲明爲靜態的,並且我以前將應用程序句柄屬性設置爲主機應用程序。爲什麼使用指針調用GetWindowRect會導致異常,但地址不是
void MyClass::GetAppCenter(POINT * pos) {
RECT Rect;
GetWindowRect(Application->Handle, &Rect);
pos->x = (Rect.left + Rect.right)/2;
pos->y = (Rect.top + Rect.bottom)/2;
}
void MyClass::PlaceForm(TForm * f) {
POINT pos;
GetAppCenter(&pos);
for (int i = 0; i < Screen->MonitorCount; i++) {
TRect r = Screen->Monitors[i]->WorkareaRect;
if (r.Contains(pos)) {
f->Left = (r.Left + r.Right)/2 - f->Width/2;
f->Top = (r.Top + r.Bottom)/2 - f->Height/2;
return;
}
}
}
我最初GetAppCenter代碼使用矩形*代替,並返回正確的值,但扔了訪問衝突異常當我設置F->左。誰能解釋爲什麼?
// original version
void OasisUtils::GetOasisCenter(POINT * pos) {
RECT *Rect;
GetWindowRect(Application->Handle, Rect);
pos->x = (Rect->left + Rect->right)/2;
pos->y = (Rect->top + Rect->bottom)/2;
delete Rect; // tried with and without this
}
謝謝@Philip,當然。初學者錯誤。 – marcp 2013-02-28 19:07:49