Thread: Custom button drawing - handling text and image positions/bounds

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Custom button drawing - handling text and image positions/bounds

    hello,

    this is a simple question, i quess. i'm capable of custom drawing a button background, but the text and the image is a problem.

    see, the image can be on top, in front, floating anywhere. the text can have a line break, when it's too long it fits in two lines.

    so how do i draw the image and the text manually respectful to the image / text positioning and layouting?

    i know there are ppl who know this
    thanks!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You could overwrite the onPaint method or add to the Paint event handler.

    Look for Graphics.DrawImage() and Graphics.DrawString()

    You could also set a label on top of the button and stuff like that.
    Using WPF was helpful for me since it gives you more clear options on how to do this stuff.

    Give us a bit more information and maybe we can help more

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    And as for splitting text over multiple lines, see Graphics.MeasureString.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    first, let me say: i know how to override the paint method and how to use drawimage and drawstring. it's not my first time
    it's easy to draw a button with an image on the left and next to it some text. but there comes the ImageTextRelation and the text could be wrapped.
    so drawstring supports drawing the text within a rectangle, but how do i calculate this rectangle? there must be some function to get the rectangle and the image position, or at least a function which only draws text&image, but no background?
    i mean - other than to use thousands of switch..cases, which will result in bugs. naturally.

    any ideas?
    Last edited by Devils Child; 07-14-2010 at 07:47 AM.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Graphics.MeasureString will return this rectangle.

    Graphics.DrawString draws a string on a specific point, which is relative to the control.

    Give us an example so we can understand better what you want. Or some code or pseudo-code

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    ok. i splitted the drawing in 4 cases:
    - no image, text does not break
    - no image, text breaks
    - image, text does not break
    - image, text breaks

    and i'm always drawing the image left to the text and try to draw centered. i guess this is a bad solution, but so far the best i can think of.

    thank you.
    if anyone has a better idea, ur welcome!

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I am not an expert but let me try.

    Ok. So DrawString() lets you draw a string where you want it the size you want it. Correct?
    You can find the actual rectangle with MeasureString().

    If MeasureString() is greater than the Size of the button, it won't fit. Thus you would need to either lessen the font size or break the string. If for example you have a button that is 100x100 a string that is 150x30 then you would break try and break the string into two. You would do that by getting making two substrings. Then measure their size if MeasureString(). If both their width would fit and their combined height as well, you can draw them in the button. Otherwise, well fix it.

    Now, I believe that the BackgroundImage of the button shouldn't be affected. That means that DrawString() should draw on top of the BackgroundImage. Isn't that show? If it is then you are simply done!

    For more image options you could use DrawImage() to draw the image where you want it. Draw first the image of course and then the string.

    I am uncertain what is NOT working for you....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. C program for text to image
    By karthiks551985 in forum C Programming
    Replies: 7
    Last Post: 10-10-2008, 09:36 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Converting text file to an image file?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-30-2002, 08:33 PM