0

I have a windows form where I programmatically create controls.. later I allow them to be moved around per drag&drop that I implemented myself:

private void valueToolStripMenuItem_Click(object sender, EventArgs e)
        {
        Label label = new Label();
        label.Text = "Label";
        label.AutoSize = true;
        label.Location = PointToClient(MousePosition);
        label.MouseDown += new MouseEventHandler(this.dyncontrol_MouseDown);
        label.MouseMove += new MouseEventHandler(this.dyncontrol_MouseMove);
        label.MouseUp += new MouseEventHandler(this.dyncontrol_MouseUp);
        label.ContextMenuStrip = valueContextMenu;
        this.Controls.Add(label);
        }

combined with these functions:

private void dyncontrol_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Control control = (Control)sender;
            selected = true;
            offset.X = PointToClient(MousePosition).X - control.Location.X;
            offset.Y = PointToClient(MousePosition).Y - control.Location.Y;
        }
    }
private void dyncontrol_MouseMove(object sender, MouseEventArgs e)
    {
        if (selected)
        {
            Control control = (Control)sender;
            Point newLocation = new Point(PointToClient(MousePosition).X - offset.X, PointToClient(MousePosition).Y - offset.Y);
            control.Location = newLocation;
        }
    }
private void dyncontrol_MouseUp(object sender, MouseEventArgs e)
    {
        selected = false;
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Control control = (Control)sender;
            Point newLocation = new Point(PointToClient(MousePosition).X - offset.X, PointToClient(MousePosition).Y - offset.Y);
            control.Location = newLocation;
        }
    }

this works perfectly fine for pictureboxes, labels, buttons etc.. now I wanted to add a axWindowsMediaplayer and do the same:

private void videoStreamToolStripMenuItem_Click(object sender, EventArgs e)
    {
        AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
        wmp.Size = new Size(300, 300);
        wmp.ContextMenuStrip = valueContextMenu;
        wmp.MouseDown += new MouseEventHandler(this.dyncontrol_MouseDown);
        wmp.MouseMove += new MouseEventHandler(this.dyncontrol_MouseMove);
        wmp.MouseUp += new MouseEventHandler(this.ddyncontrol_MouseUp);
        this.Controls.Add(wmp);
        wmp.uiMode = "none";
        wmp.Ctlenabled = false;
        wmp.URL = "C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv";
    }

but I can't drag the Player(MouseDown and MouseUp are not firing) around and also the contextmenu isn't the right one, but a standard thing that appears as if I hadn't changed anything..

Any ideas why this is so?

I already tried to delete the reference in the project and add it again..

Salocin
  • 393
  • 6
  • 17
  • Wrong events. WMP exposes its own, they are named MouseDownEvent, MouseMoveEvent and MouseUpEvent. Easy to discover when you use the designer btw. – Hans Passant Nov 18 '14 at 09:12
  • this explains the not firing of MouseDown.. I tried to do this now: wmp.MouseDownEvent+=new AxWMPLib._WMPOCXEvents_MouseDownEventHandler(this.dynControl_MouseDown); but it the compiler doesn't accept it.. – Salocin Nov 18 '14 at 10:01
  • You have to change the event handler method signature as well, second argument is a AxWMPLib._WMPOCXEvents_MouseMoveEvent. As I said, do this with the designer first so these things are obvious. – Hans Passant Nov 18 '14 at 10:04
  • ok, thank you! Figured it out with the Designer.. but when I assign a Contextmenustrip in the designer it doesn't change it either? – Salocin Nov 18 '14 at 10:34
  • WMP has its own context menu, you disabled it by setting Ctlenabled = false. Use the MouseDownEvent instead, test for e.nButton == 2 and call ContextMenuStrip.Show() – Hans Passant Nov 18 '14 at 10:39

0 Answers0