我在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