Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. MouseDown event missed in custom Button class

MouseDown event missed in custom Button class

Scheduled Pinned Locked Moved Windows Forms
graphicshelpcomdesignquestion
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TyrionTheImp
    wrote on last edited by
    #1

    Hi All, Am facing an issue with getting the MouseDown event for a custom drawn Button class in Dot Net 2.0 framework. This custom class basically listens to the MosueDown,Up,Hover,Enter and Leave events and updates the image shown on it (if we hover or click, an "active/pressed" image is shown else "inactive" image is painted).The code is as below :

    class CustomButton : Button
    {
    Bitmap displayimage;
    bool down;
    public CustomButton()
    {
    MouseUp += OnMouseUp
    MouseDown += OnMouseDown //etc.. for other mouse events
    }

    private void OnMouseDown(object sender, MouseEventArgs e)
    {
    down = true;
    displayimage = new Bitmap("pressedimage.png");
    Invalidate();
    }

    private void OnMouseUp(object sender, MouseEventArgs e)
    {
    down = false;
    displayimage = new Bitmap("inactiveimage.png");
    Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    if (down)
    {
    Matrix matrix = new Matrix();
    matrix.Translate(1, 1, MatrixOrder.Prepend);
    e.Graphics.Transform = matrix;
    }
    e.Graphics.DrawImage(bitmapImage);
    e.Graphics.DrawString(displaystring);
    base.OnPaint(e);
    }
    }

    The buttons are created in the main Form class and we also listen to MouseDown event here :

    class WinForm1 : Form
    {
    CustomImageButton button = new CustomImageButton ();

    public WinForm1
    {
    button.MouseDown += OnButtonMosueDown;
    }

    private void OnButtonMosueDown(object sender, MouseEventArgs e)
    {
    //take some action , log the user click action
    }
    }

    The problem is MouseDown events don`t even get trigerred sporadically, mostly seen in cases where the user is working with some external application and then directly comes over and clicks the custom button on the UI. Any idea as to what could be the cause/likely solution ? Is this related to the click through issue seen in dot net 2.0 Toolstrip class (refer http://blogs.msdn.com/b/rickbrew/archive/2006/01/09/511003.aspx ) ?

    P 1 Reply Last reply
    0
    • T TyrionTheImp

      Hi All, Am facing an issue with getting the MouseDown event for a custom drawn Button class in Dot Net 2.0 framework. This custom class basically listens to the MosueDown,Up,Hover,Enter and Leave events and updates the image shown on it (if we hover or click, an "active/pressed" image is shown else "inactive" image is painted).The code is as below :

      class CustomButton : Button
      {
      Bitmap displayimage;
      bool down;
      public CustomButton()
      {
      MouseUp += OnMouseUp
      MouseDown += OnMouseDown //etc.. for other mouse events
      }

      private void OnMouseDown(object sender, MouseEventArgs e)
      {
      down = true;
      displayimage = new Bitmap("pressedimage.png");
      Invalidate();
      }

      private void OnMouseUp(object sender, MouseEventArgs e)
      {
      down = false;
      displayimage = new Bitmap("inactiveimage.png");
      Invalidate();
      }

      protected override void OnPaint(PaintEventArgs e)
      {
      if (down)
      {
      Matrix matrix = new Matrix();
      matrix.Translate(1, 1, MatrixOrder.Prepend);
      e.Graphics.Transform = matrix;
      }
      e.Graphics.DrawImage(bitmapImage);
      e.Graphics.DrawString(displaystring);
      base.OnPaint(e);
      }
      }

      The buttons are created in the main Form class and we also listen to MouseDown event here :

      class WinForm1 : Form
      {
      CustomImageButton button = new CustomImageButton ();

      public WinForm1
      {
      button.MouseDown += OnButtonMosueDown;
      }

      private void OnButtonMosueDown(object sender, MouseEventArgs e)
      {
      //take some action , log the user click action
      }
      }

      The problem is MouseDown events don`t even get trigerred sporadically, mostly seen in cases where the user is working with some external application and then directly comes over and clicks the custom button on the UI. Any idea as to what could be the cause/likely solution ? Is this related to the click through issue seen in dot net 2.0 Toolstrip class (refer http://blogs.msdn.com/b/rickbrew/archive/2006/01/09/511003.aspx ) ?

      P Offline
      P Offline
      ProEnggSoft
      wrote on last edited by
      #2

      I tried your code and the mouse down event is being triggered.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups