creating a draggable custom control [VC++]

I've done some search on MSDN but didn't find what I need in the right language.
I'm using VC++ 2005 to create a custom control modeling a sort of graphical equalizer. A part of this control would be another custom control, a sort of panel which can be dragged horizontally and resized to set center frequency and bandwidth.
My idea is to inherit from UserControl and add a Panel. How can I make this control draggable


Answer this question

creating a draggable custom control [VC++]

  • SomeRandomName

    You are setting the left property. Just make sure that newX isn't smaller than zero or larger than "something" - panel1->Width. Where "something" is the container's width.


  • GrahamY

    Check this thread. It uses VB.NET but it is all plain event handling and Windows Forms methods...


  • TarPista

    Use the ClientSize.Width property.


  • 87jerome

    Thanks again. Meanwhile I found the solution on my own, but there's still a problem. I make this check (newX + panel1->Width) <= (panel1->Parent->Width) and, if moved to the right, Panel1 goes beyond it's container of 2 pixels. I think they are the pixels corresponding to the sum of Panel1's and its container's borders.
    Know I only have to discover if all borderstyles have the same width and I'm done.
    Thanks again.

  • Walter Beach

    Thank you for the reply. The code works in VC++ as well.
    Do you also know how to avoid that the control goes out of it's container
    I tried the following code (c++)

    private: System::Void panel1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
                     if(dragging)
                     {
                              Point pos = this->panel1->PointToScreen(e->Location);
                             int newX = panelPos.X+pos.X-mousePos.X;
                             if (((newX + panel1->Width) < panel1->Parent->Right)
                                     && (newX > panel1->Parent->Left))
                             {
                                 this->panel1->Left=newX;
                             }
                     }
                 }


    to replace the MouseMove event you suggested, but the Control doesn't stop where I want.
    Oddly, if I substitute 0 (which is the actual value) to panel1->Parent->Left, the code works.

  • creating a draggable custom control [VC++]