GDI+ how to not painting the background image [modified]
-
Hello everybody I have a Form and I use the GDI+ to have a User Interface I have a Button(just an image) that should be repainted when it is clicked and I use a flag at the mouse event handler to know when the button is clicked and in the OnPaint method I check it FIRST, and if the flag was True I don't paint the BackGroundImage, because it is painted before (at the startup of the Form) but I don't know why that BackGroundImage is gone when the event occures and only my button and the BackColor of the Form is shown =>>note: I don't want to use BackGroundImage because I think it will paint it all the times that the Form is repainted, and it is process intensive Thank you P.S: If you want to read this code better you can copy/paste it in VS.NET and select all the code and use CTRL + (K) then CTRL + (F) to format the code in a good format to be more readable here is the OnPaint method:
protected override void OnPaint(PaintEventArgs e) { if (LanguageTraining) { //dc.FillRectangle(Brushes.White, new Rectangle(744, 617, 248, 42)); dc.DrawImage(ButtonSelectedBlue, LanguageButtonBounds); LanguageTraining = false; base.OnPaint(e); return; } base.OnPaint(e); Graphics dc = e.Graphics; dc.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y); dc.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y); dc.DrawImage(piccy, piccyBounds); dc.DrawString("Language Training", ButtonFont, TextBrush, new Point(755, 625)); }
Here is all the codeusing 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 { public partial class Form1 : Form { private bool LanguageTraining = false; 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; private readonly Point[] piccyBounds; private readonly Point[] LanguageButtonBounds; public Form1() {
-
Hello everybody I have a Form and I use the GDI+ to have a User Interface I have a Button(just an image) that should be repainted when it is clicked and I use a flag at the mouse event handler to know when the button is clicked and in the OnPaint method I check it FIRST, and if the flag was True I don't paint the BackGroundImage, because it is painted before (at the startup of the Form) but I don't know why that BackGroundImage is gone when the event occures and only my button and the BackColor of the Form is shown =>>note: I don't want to use BackGroundImage because I think it will paint it all the times that the Form is repainted, and it is process intensive Thank you P.S: If you want to read this code better you can copy/paste it in VS.NET and select all the code and use CTRL + (K) then CTRL + (F) to format the code in a good format to be more readable here is the OnPaint method:
protected override void OnPaint(PaintEventArgs e) { if (LanguageTraining) { //dc.FillRectangle(Brushes.White, new Rectangle(744, 617, 248, 42)); dc.DrawImage(ButtonSelectedBlue, LanguageButtonBounds); LanguageTraining = false; base.OnPaint(e); return; } base.OnPaint(e); Graphics dc = e.Graphics; dc.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y); dc.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y); dc.DrawImage(piccy, piccyBounds); dc.DrawString("Language Training", ButtonFont, TextBrush, new Point(755, 625)); }
Here is all the codeusing 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 { public partial class Form1 : Form { private bool LanguageTraining = false; 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; private readonly Point[] piccyBounds; private readonly Point[] LanguageButtonBounds; public Form1() {
Hi, 1. you can show formatted code by using PRE tags instead of CODE tags 2. There must be several ways to prevent a background repaint. for one I would try without the base.OnPaint() statement; alternatively you can have a look at the ControlStyles Enumeration, and the Control.SetStyle method. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi, 1. you can show formatted code by using PRE tags instead of CODE tags 2. There must be several ways to prevent a background repaint. for one I would try without the base.OnPaint() statement; alternatively you can have a look at the ControlStyles Enumeration, and the Control.SetStyle method. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Hi, and thank you for your answer but unfortunately I eliminated the "e.base();" from the code but the problem wasn't solved I am trying the other option you mentioned
modified on Saturday, June 20, 2009 8:29 AM