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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Changing the color of a pen or brush on the fly?

Changing the color of a pen or brush on the fly?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
2 Posts 2 Posters 1 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
    CoffeeAddict19
    wrote on last edited by
    #1

    I know how to initialize a brush or pen to a specific color, but I couldn't find any methods for changing the contents of a brush or pen once it has been initialized. In my application the user has to be able to change it, so simply creating a new variable isn't practical. I've been deleting the brush and then re-initializing it with the new color as you can see:

    void CGameWin::ChangeBackgroundColor(COLORREF Color)
    {
    	CurrentBackgroundColor = Color;
    	CurrentBackgroundColorBrush.DeleteObject();
    	CurrentBackgroundColorBrush.CreateSolidBrush(Color);
    	background_DC.SelectObject(CurrentBackgroundColorBrush);
    }
    

    Is there a better way to do this?

    C 1 Reply Last reply
    0
    • C CoffeeAddict19

      I know how to initialize a brush or pen to a specific color, but I couldn't find any methods for changing the contents of a brush or pen once it has been initialized. In my application the user has to be able to change it, so simply creating a new variable isn't practical. I've been deleting the brush and then re-initializing it with the new color as you can see:

      void CGameWin::ChangeBackgroundColor(COLORREF Color)
      {
      	CurrentBackgroundColor = Color;
      	CurrentBackgroundColorBrush.DeleteObject();
      	CurrentBackgroundColorBrush.CreateSolidBrush(Color);
      	background_DC.SelectObject(CurrentBackgroundColorBrush);
      }
      

      Is there a better way to do this?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can't change a brush, you need to create a new one. If this happens often enough, you could build a cache, I guess.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      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