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. IDeviceContext OR How to MeasureText out of the Paint Event

IDeviceContext OR How to MeasureText out of the Paint Event

Scheduled Pinned Locked Moved C#
graphicshelptutorialquestion
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
    C Scharbe
    wrote on last edited by
    #1

    Hello, I'd like to Resize a control that contains any text. If I use TextRenderer.MeasureText there is always a Padding, also if I use the Flag NoPadding: Size size = TextRenderer.MeasureText("dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); I found out that i need to set a IDeviceContext like that: private void MyControl_Paint(object sender, PaintEventArgs e) { Size size = TextRenderer.MeasureText(e.Graphics, "dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); } My Problem is: I dont want to Resize the Control in the Paint-Event, so I guess I need to get somehow the current IDeviceContext, but how? Or do I use the TextRenderer.MeasureText method in a wrong way? Thank You for Your Answers!

    C 1 Reply Last reply
    0
    • C C Scharbe

      Hello, I'd like to Resize a control that contains any text. If I use TextRenderer.MeasureText there is always a Padding, also if I use the Flag NoPadding: Size size = TextRenderer.MeasureText("dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); I found out that i need to set a IDeviceContext like that: private void MyControl_Paint(object sender, PaintEventArgs e) { Size size = TextRenderer.MeasureText(e.Graphics, "dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); } My Problem is: I dont want to Resize the Control in the Paint-Event, so I guess I need to get somehow the current IDeviceContext, but how? Or do I use the TextRenderer.MeasureText method in a wrong way? Thank You for Your Answers!

      C Offline
      C Offline
      C Scharbe
      wrote on last edited by
      #2

      Solution: IDeviceContext deviceContext = this.CreateGraphics(); ARTICLE AT: Friday, April 28, 2006 Calculating Size of Multi-Line WinForm Label With MeasureText http://geekswithblogs.net/paulmehner/Default.aspx

      M 1 Reply Last reply
      0
      • C C Scharbe

        Solution: IDeviceContext deviceContext = this.CreateGraphics(); ARTICLE AT: Friday, April 28, 2006 Calculating Size of Multi-Line WinForm Label With MeasureText http://geekswithblogs.net/paulmehner/Default.aspx

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Don't forget to dispose of the Graphics object you just created. It's a very common mistake that leads to resource leaks.

        Regards, mav -- Black holes are the places where God divided by 0...

        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