Moving objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AM Hulshoff

    #1

    Moving objects

    Can someone tell me how I can move an object, in this case a listbox, over a
    form. The code below works, but not when the form is custom sized. It works
    perfectly when the form is maximized. And what is the best way to give the
    object boundries to move within?

    Hope someone can help me.
    TIA,
    Arjan.

    /*Begin Code*/
    /*Source: MSDN*/
    void ListBox1MouseDo wn(object sender, System.Windows. Forms.MouseEven tArgs e)
    {
    int xOffset;
    int yOffset;

    if(e.Button == MouseButtons.Le ft)
    {
    xOffset = -e.X - SystemInformati on.FrameBorderS ize.Width;
    yOffset = -e.Y - SystemInformati on.CaptionHeigh t -
    SystemInformati on.FrameBorderS ize.Height;
    mouseOffset = new Point(xOffset, yOffset);
    isMouseDown = true;
    }
    }

    void ListBox1MouseMo ve(object sender, System.Windows. Forms.MouseEven tArgs e)
    {
    if (isMouseDown)
    {
    Point mousePos = Control.MousePo sition;
    mousePos.Offset (mouseOffset.X, mouseOffset.Y);
    listBox1.Locati on = mousePos;
    }
    }

    void ListBox1MouseUp (object sender, System.Windows. Forms.MouseEven tArgs e)
    {
    isMouseDown = false;
    }
    /*End Code*/
  • Stoitcho Goutsev \(100\)

    #2
    Re: Moving objects

    AM,

    If I were you I would derive a new class from list box and in this new class
    I would override WndProc and process WM_NCHITTEST message to return
    HTCAPTION. This way the windows will handle all the moving of the cotnrol.

    Here is basically how the WndProc can be written:

    private const int WM_NCHITTEST = 0x84;
    private const int HTCAPTION = 0x2;
    private const int HTCLIENT = 0x1;

    protected override void WndProc(ref Message m)
    {
    base.WndProc (ref m);
    if(m.Msg == WM_NCHITTEST && m.Result == new IntPtr(HTCLIENT ))
    {
    m.Result = new IntPtr(HTCAPTIO N);

    }


    --
    HTH
    Stoitcho Goutsev (100)


    "AM Hulshoff" <AMHulshoff@dis cussions.micros oft.com> wrote in message
    news:CDB44ED6-AE82-45A9-B0FE-EB0C31306313@mi crosoft.com...[color=blue]
    > Can someone tell me how I can move an object, in this case a listbox, over
    > a
    > form. The code below works, but not when the form is custom sized. It
    > works
    > perfectly when the form is maximized. And what is the best way to give the
    > object boundries to move within?
    >
    > Hope someone can help me.
    > TIA,
    > Arjan.
    >
    > /*Begin Code*/
    > /*Source: MSDN*/
    > void ListBox1MouseDo wn(object sender, System.Windows. Forms.MouseEven tArgs
    > e)
    > {
    > int xOffset;
    > int yOffset;
    >
    > if(e.Button == MouseButtons.Le ft)
    > {
    > xOffset = -e.X - SystemInformati on.FrameBorderS ize.Width;
    > yOffset = -e.Y - SystemInformati on.CaptionHeigh t -
    > SystemInformati on.FrameBorderS ize.Height;
    > mouseOffset = new Point(xOffset, yOffset);
    > isMouseDown = true;
    > }
    > }
    >
    > void ListBox1MouseMo ve(object sender, System.Windows. Forms.MouseEven tArgs
    > e)
    > {
    > if (isMouseDown)
    > {
    > Point mousePos = Control.MousePo sition;
    > mousePos.Offset (mouseOffset.X, mouseOffset.Y);
    > listBox1.Locati on = mousePos;
    > }
    > }
    >
    > void ListBox1MouseUp (object sender, System.Windows. Forms.MouseEven tArgs e)
    > {
    > isMouseDown = false;
    > }
    > /*End Code*/[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Moving objects

      Hi,

      You should check the current position of the mouse and make sure it's inside
      the boundary you want.

      You should probably need to change from SystemInformati on.FrameBorderS ize
      to another way to get the origin point


      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation

      "AM Hulshoff" <AMHulshoff@dis cussions.micros oft.com> wrote in message
      news:CDB44ED6-AE82-45A9-B0FE-EB0C31306313@mi crosoft.com...[color=blue]
      > Can someone tell me how I can move an object, in this case a listbox, over
      > a
      > form. The code below works, but not when the form is custom sized. It
      > works
      > perfectly when the form is maximized. And what is the best way to give the
      > object boundries to move within?
      >
      > Hope someone can help me.
      > TIA,
      > Arjan.
      >
      > /*Begin Code*/
      > /*Source: MSDN*/
      > void ListBox1MouseDo wn(object sender, System.Windows. Forms.MouseEven tArgs
      > e)
      > {
      > int xOffset;
      > int yOffset;
      >
      > if(e.Button == MouseButtons.Le ft)
      > {
      > xOffset = -e.X - SystemInformati on.FrameBorderS ize.Width;
      > yOffset = -e.Y - SystemInformati on.CaptionHeigh t -
      > SystemInformati on.FrameBorderS ize.Height;
      > mouseOffset = new Point(xOffset, yOffset);
      > isMouseDown = true;
      > }
      > }
      >
      > void ListBox1MouseMo ve(object sender, System.Windows. Forms.MouseEven tArgs
      > e)
      > {
      > if (isMouseDown)
      > {
      > Point mousePos = Control.MousePo sition;
      > mousePos.Offset (mouseOffset.X, mouseOffset.Y);
      > listBox1.Locati on = mousePos;
      > }
      > }
      >
      > void ListBox1MouseUp (object sender, System.Windows. Forms.MouseEven tArgs e)
      > {
      > isMouseDown = false;
      > }
      > /*End Code*/[/color]


      Comment

      • AM Hulshoff

        #4
        Re: Moving objects

        That's what I tried, but everytime I move the object I get the same result.
        The boundries is something I won't have much problems with. But that other
        part is causing me a headache.

        "Ignacio Machin ( .NET/ C# MVP )" wrote:
        [color=blue]
        > Hi,
        >
        > You should check the current position of the mouse and make sure it's inside
        > the boundary you want.
        >
        > You should probably need to change from SystemInformati on.FrameBorderS ize
        > to another way to get the origin point
        >
        >
        > --
        > Ignacio Machin,
        > ignacio.machin AT dot.state.fl.us
        > Florida Department Of Transportation[/color]

        Comment

        Working...