GDI+, problem with Invalidate() method
-
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 =
-
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 =
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
-
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 =
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