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+ how to not painting the background image [modified]

GDI+ how to not painting the background image [modified]

Scheduled Pinned Locked Moved C#
graphicscsharpvisual-studiowinformslinq
3 Posts 2 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

    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 code 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 { 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() {

    L 1 Reply Last reply
    0
    • C cppwxwidgetsss

      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 code 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 { 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() {

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        C Offline
        C Offline
        cppwxwidgetsss
        wrote on last edited by
        #3

        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

        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