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. C#
  4. GDI+, problem with Invalidate() method

GDI+, problem with Invalidate() method

Scheduled Pinned Locked Moved C#
graphicshelpcsharpwinformslinq
3 Posts 3 Posters 0 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.
  • C Offline
    C Offline
    cppwxwidgetsss
    wrote on last edited by
    #1

    Hi, I have a form that is designed by GDI+, it has some buttons(images) I want to change the button when the mouse is over the button so I handled the MouseMove event with the OnMouseMove and I calculated if the mouse is in the button region , after that if it was, I call the method Invalidate() so that the button should be repainted but the problem is that the Program runs very slowly, and you can see the mouse movement in slow motion, when I comment the Invaliate() method in the code, everything is good, I also checked the code with an empty method of OnPaint() so that the problem is not because of OnPaint() but Invalidate() by the way i am using the Double Buffred Form I am confused somebody help me thank you P.S: here is the whole code: From1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace paintImage
    {
    enum ButtonState
    {
    NotSelected,
    Blue,
    Green

    }
    public partial class Form1 : Form
    {
        ButtonState Language = ButtonState.NotSelected;
        private bool MouseOutLanguageTraining = false;
        private bool LanguageTraining = false;
        private bool MouseOnLangugeTraining = false;
        Rectangle LanguageButtonPosition = new Rectangle(774, 617, 218, 42);
        //private Point MouseClickPosition;
        //private readonly Brush TextBrush = Brushes.White;
        //private readonly Font ButtonFont = new Font("alefba", 18,FontStyle.Bold);
        
        readonly Image piccy;
        readonly Image ButtonSelectedBlue;
        readonly Image ButtonSelectedGreen;
        readonly Image ButtonNotSelected;
    
        private readonly Point\[\] piccyBounds;
        private readonly Point\[\] LanguageButtonBounds;
        public Form1()
        {
            InitializeComponent();
            piccy = Image.FromFile(@"C:\\pics\\page12.png");
            ButtonNotSelected = Image.FromFile(@"C:\\Pics\\Button Not Selected.png");
            ButtonSelectedBlue = Image.FromFile(@"C:\\pics\\Button Selected Blue.png");
            ButtonSelectedGreen = Image.FromFile(@"C:\\pics\\Button Selected Green.png");
            AutoScrollMinSize = piccy.Size;
            piccyBounds = new Point\[3\];
            LanguageButtonBounds = new Point\[3\];
            
            const int resolutionX = 1024; //the x dimension of the display resolution
            const int resolutionY =
    
    C OriginalGriffO 2 Replies Last reply
    0
    • C cppwxwidgetsss

      Hi, I have a form that is designed by GDI+, it has some buttons(images) I want to change the button when the mouse is over the button so I handled the MouseMove event with the OnMouseMove and I calculated if the mouse is in the button region , after that if it was, I call the method Invalidate() so that the button should be repainted but the problem is that the Program runs very slowly, and you can see the mouse movement in slow motion, when I comment the Invaliate() method in the code, everything is good, I also checked the code with an empty method of OnPaint() so that the problem is not because of OnPaint() but Invalidate() by the way i am using the Double Buffred Form I am confused somebody help me thank you P.S: here is the whole code: From1.cs

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;

      namespace paintImage
      {
      enum ButtonState
      {
      NotSelected,
      Blue,
      Green

      }
      public partial class Form1 : Form
      {
          ButtonState Language = ButtonState.NotSelected;
          private bool MouseOutLanguageTraining = false;
          private bool LanguageTraining = false;
          private bool MouseOnLangugeTraining = false;
          Rectangle LanguageButtonPosition = new Rectangle(774, 617, 218, 42);
          //private Point MouseClickPosition;
          //private readonly Brush TextBrush = Brushes.White;
          //private readonly Font ButtonFont = new Font("alefba", 18,FontStyle.Bold);
          
          readonly Image piccy;
          readonly Image ButtonSelectedBlue;
          readonly Image ButtonSelectedGreen;
          readonly Image ButtonNotSelected;
      
          private readonly Point\[\] piccyBounds;
          private readonly Point\[\] LanguageButtonBounds;
          public Form1()
          {
              InitializeComponent();
              piccy = Image.FromFile(@"C:\\pics\\page12.png");
              ButtonNotSelected = Image.FromFile(@"C:\\Pics\\Button Not Selected.png");
              ButtonSelectedBlue = Image.FromFile(@"C:\\pics\\Button Selected Blue.png");
              ButtonSelectedGreen = Image.FromFile(@"C:\\pics\\Button Selected Green.png");
              AutoScrollMinSize = piccy.Size;
              piccyBounds = new Point\[3\];
              LanguageButtonBounds = new Point\[3\];
              
              const int resolutionX = 1024; //the x dimension of the display resolution
              const int resolutionY =
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This looks right to me. Of course, the mouse move is going to keep firing paint messages, it probably makes more sense to only fire the invalidate when the state changes.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      1 Reply Last reply
      0
      • C cppwxwidgetsss

        Hi, I have a form that is designed by GDI+, it has some buttons(images) I want to change the button when the mouse is over the button so I handled the MouseMove event with the OnMouseMove and I calculated if the mouse is in the button region , after that if it was, I call the method Invalidate() so that the button should be repainted but the problem is that the Program runs very slowly, and you can see the mouse movement in slow motion, when I comment the Invaliate() method in the code, everything is good, I also checked the code with an empty method of OnPaint() so that the problem is not because of OnPaint() but Invalidate() by the way i am using the Double Buffred Form I am confused somebody help me thank you P.S: here is the whole code: From1.cs

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;

        namespace paintImage
        {
        enum ButtonState
        {
        NotSelected,
        Blue,
        Green

        }
        public partial class Form1 : Form
        {
            ButtonState Language = ButtonState.NotSelected;
            private bool MouseOutLanguageTraining = false;
            private bool LanguageTraining = false;
            private bool MouseOnLangugeTraining = false;
            Rectangle LanguageButtonPosition = new Rectangle(774, 617, 218, 42);
            //private Point MouseClickPosition;
            //private readonly Brush TextBrush = Brushes.White;
            //private readonly Font ButtonFont = new Font("alefba", 18,FontStyle.Bold);
            
            readonly Image piccy;
            readonly Image ButtonSelectedBlue;
            readonly Image ButtonSelectedGreen;
            readonly Image ButtonNotSelected;
        
            private readonly Point\[\] piccyBounds;
            private readonly Point\[\] LanguageButtonBounds;
            public Form1()
            {
                InitializeComponent();
                piccy = Image.FromFile(@"C:\\pics\\page12.png");
                ButtonNotSelected = Image.FromFile(@"C:\\Pics\\Button Not Selected.png");
                ButtonSelectedBlue = Image.FromFile(@"C:\\pics\\Button Selected Blue.png");
                ButtonSelectedGreen = Image.FromFile(@"C:\\pics\\Button Selected Green.png");
                AutoScrollMinSize = piccy.Size;
                piccyBounds = new Point\[3\];
                LanguageButtonBounds = new Point\[3\];
                
                const int resolutionX = 1024; //the x dimension of the display resolution
                const int resolutionY =
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        To add to what Chris said, it is probably that you are continually firing Paint messages - why not use MouseEnter and MouseLeave to handle the re-paint? That way, you don't recalculate on every mouse move, and you only fire the Paint when the image should be changing. BTW: it is not good practice to use absolute numbers for mouse position checking - why not use the position of the button instead?

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        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