在C ++ Builder中自动锁定Form2定位

时间:2015-07-25 15:51:18

标签: c++builder

我的应用程序使用两种形式。当我单击主窗体中的面板时,Form2应该显示出来。主表单和表单2之间的距离为几个像素。

现在我需要的是当我将主窗体移动到任何地方时,然后Form2移动到主窗体所在的位置。我的意思是我需要Form2才能锁定主表格。

1 个答案:

答案 0 :(得分:1)

当收到WndProc()WM_WINDOWPOSCHANGING消息时,让MainForm覆盖虚拟WM_WINDOWPOSCHANGED方法,根据需要相对于MainForm的当前位置重新定位Form2。

class TMainForm : public TForm
{
...
protected:
   void __fastcall WndProc(TMessage &Message);
...
};

...
#include "Form2.h"

...

void __fastcall TMainForm::WndProc(TMessage &Message)
{
    TForm::WndProc(Message);
    switch (Message.Msg)
    {
        case WM_WINDOWPOSCHANGING:
        case WM_WINDOWPOSCHANGED:
        {
            if ((Form2) && (Form2->Visible))
            {
                Form2->Left = ...; // such as this->Left
                Form2->Top = ...; // such as this->Top + this->Height
            }
            break;
        }
    }
}