1

I'd like to have a TForm with BorderStyle = bsNone (no borders, no caption), which is nevertheless resizable and movable. I already figured out how to do the resizable part, but I'm stuck with getting it movable.

/**
*   Overrides standard CreateParams method to create a TForm with BorderStyle
*    bsNone but is nevertheless movable and resizable
**/
void __fastcall CreateParams(TCreateParams &Params)
{
    BorderStyle = bsNone;
    TForm::CreateParams(Params);
    //set flag WS_EX_STATICEDGE
    //for more details on this flag, see http://msdn.microsoft.com/en-us/library/ms632680(v=vs.85).aspx
    Params.ExStyle = Params.ExStyle ^ 0x00020000L;
    //set flag WS_SIZEBOX
    //for more details on this flag, see http://msdn.microsoft.com/en-us/library/ff700543(v=VS.85).aspx
    Params.Style = Params.Style ^ 0x00040000L;
}

It's probably only a matter of finding the correct flags. Any ideas?

2 Answers 2

7

The best way to allow a form to move is to mimic how a form moves when you click and drag the title bar. Since your window doesn't have a title bar, when Windows needs to know what part of your form the mouse cursor is over you lie and tell Windows that it is indeed over the title bar. After that, moving works normally since the default behaviour kicks in.

To do this, you respond to the WM_NCHITTEST message, which is easily done by overriding the form's WndProc method. This message is sent in several situations (not just mouse clicks or movement) so don't assume anything about what the user's doing when you get this message. Handle it by setting the message result to HTCAPTION, a value indicating the position is over the title bar.

Important things to note are:

  • This method will be called for every message the form gets; don't do anything slow or complex here.
  • Always let the default inherited implementation of WndProc handle the message. This is essential for most messages, since you only want to change the behaviour for this one and if you don't call the inherited implantation nothing will happen for the messages at all, but it's important for this one too since you don't know what code needs to be sent this message. I.e., you want to add to how your program responds to this, not replace it. This is a general good guideline for all message processing you intercept / add. The WndProc documentation mentions this too.
  • You can set a region of the form to be the draggable bit by checking the mouse coordinates. In the below code, only the top 100 pixels of the form will be draggable.

Sample code:

void __fastcall TForm1::WndProc(Messages::TMessage& Message) {
    TForm::WndProc(Message); // Inherited implementation
    if (Message.Msg == WM_NCHITTEST) {
        TWMNCHitTest& oMsg = reinterpret_cast<TWMNCHitTest&>(Message);
        TPoint oPoint(oMsg.XPos, oMsg.YPos); // Screen coordinates
        oPoint = ScreenToClient(oPoint); // Now in form-local coordinates
        // It's in the title bar (caption) if it's in a rectangle at the top of the form
        if ((oPoint.x > 0 && oPoint.x < ClientWidth) &&
            (oPoint.y > 0 && oPoint.y < 100))
        {
            oMsg.Result = HTCAPTION;
        }
    }
}
1
  • 1
    Thanks a lot! That's an answer I would really like to reward with more than an upvote and accept!
    – mort
    Aug 25, 2011 at 8:34
5

Try to place this code to Form OnMouseDown event handler:

ReleaseCapture();
this->Perform(WM_SYSCOMMAND, 0xF012, 0);
5
  • 3
    Better provide an WM_NCHITTEST handler returning HTCAPTION where the form should be moveable, instead of an undocumented trick. Aug 24, 2011 at 16:09
  • @Sertac Akyuz: Good you please give some more information about your idea? I don't really get your idea.
    – mort
    Aug 24, 2011 at 16:50
  • @mort - See an example of WM_NCHITTEST handler on this page. After the Dispatch you can do, for instance, if(Pt.y<21)Msg.Result=HTCAPTION; where Pt is a TPoint converted to client coordinates with ScreenToClient on a Point assigned from Msg.LParam (low word is x, high word is y). Also see this page where MapWindowPoints is used instead of ScreenToClient. Aug 24, 2011 at 19:17
  • 1
    @Sertac: I was going to post exactly that as an answer :) I don't want to nab your points, so please add your comment as an answer (maybe with a code example or something.)
    – David
    Aug 24, 2011 at 23:26
  • 1
    @David - Please post an answer, AAMOF I'm not a c++ guy, don't even have a compiler, that's why I commented in the first place. Aug 24, 2011 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.