Thread: click and drag form

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    click and drag form

    I was wondering on how to click on the form anywhere and be able to drag it.

    I found a sample on how to do this, but it's in C++. Here's what it looks like:

    void CNCHitDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CDialog::OnLButtonDown(nFlags, point);
    // fake windows into thinking your clicking on the caption does not
    // maximize on double click
    PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));
    }

    If anyone knows how to translate this, that would be a huge help

    thank you.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Ok, nevermind, i found out how to do it. If anyone else is curious on how to do it. I found a way to sort of "cheat" i guess.

    What you do is place a picturebox on the form and dock it so it takes up the whole form. Once you do this, simply select the events button and double click on MouseMove and MouseDown events.

    for the MouseDown event this is the code:

    private void pictureBox1_MouseDown(
    object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ptOffset = new Point(-e.X - pictureBox1.Left,
    -e.Y - pictureBox1.Top);
    }

    --- I should note ptOffest. You have to declair it under where your controls are.. private Point ptOffset;

    ...Now for MouseMove event:

    private void pictureBox1_MouseMove(
    object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if( e.Button==MouseButtons.Left)
    {
    Point mousePos = Control.MousePosition;
    mousePos.Offset(ptOffset.X, ptOffset.Y);
    this.Location = mousePos;
    }
    }

    There ya go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] drawing: drag GDI shapes
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 10-05-2008, 02:06 PM
  2. Window Drag / Blur
    By ventolin in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2004, 07:27 AM
  3. I can't figure out what project to use!
    By yodoblec in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2003, 09:27 AM
  4. Advice for Beginner??
    By hawghauler in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2003, 05:33 AM
  5. SDI Menu App - MSVC 6
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 10-16-2001, 09:59 PM