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. Graphics.DrawString() drawing text with multiple colors

Graphics.DrawString() drawing text with multiple colors

Scheduled Pinned Locked Moved C#
graphicshelpquestionhardwaretutorial
2 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.
  • S Offline
    S Offline
    shultas
    wrote on last edited by
    #1

    Hi everyone. I am working on a project where I am scrolling text across the screen that identifies an image, along with the time. "New York, NY - 5:00AM". I have the drawing and scrolling down pat. I am using a graphics object and using drawstring in my custom OnPaint() event. The problem is that I need to have the time in a different color. ("New York, NY -" will be in black, where "5:00AM" will be in yellow). I am not sure how to go about doing this. I was not sure if one DrawString() could have multiple colors embedded in it, or if I need to have two DrawStrings with the X offset just right. If I do have to do the latter method, can you point me in the direction of figuring out where that X Start Position should be based on a given string (ie, if I start drawing the text "New York, NY" at x position 600, how do I know what X position I should begin to start drawing the 05:00AM text? This is where I am at a loss and any help would be appreciated big time. Thanks!

    L 1 Reply Last reply
    0
    • S shultas

      Hi everyone. I am working on a project where I am scrolling text across the screen that identifies an image, along with the time. "New York, NY - 5:00AM". I have the drawing and scrolling down pat. I am using a graphics object and using drawstring in my custom OnPaint() event. The problem is that I need to have the time in a different color. ("New York, NY -" will be in black, where "5:00AM" will be in yellow). I am not sure how to go about doing this. I was not sure if one DrawString() could have multiple colors embedded in it, or if I need to have two DrawStrings with the X offset just right. If I do have to do the latter method, can you point me in the direction of figuring out where that X Start Position should be based on a given string (ie, if I start drawing the text "New York, NY" at x position 600, how do I know what X position I should begin to start drawing the 05:00AM text? This is where I am at a loss and any help would be appreciated big time. Thanks!

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

      Hi, each DrawString takes one Pen, each Pen has one Color, so yes you would need to call DrawString twice. There also is MeasureString which predicts how much space a DrawString (with almost the same parameters) will require. That should solve your positioning problem. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      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