aligned multiline custom button

G

Guest

i have been able to create a custom control that overrides the onpaint method to create a multiline button. However, i can't think of a way to align this text to center. Thoughts

Current code that I have

private bool pressed = false

public WrappingButton(



protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e

pressed = true
this.Invalidate()
base.OnMouseDown (e)


protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e

pressed = false
this.Invalidate()
base.OnMouseUp (e)


protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe

Graphics graphics = pe.Graphics
Pen pen = new System.Drawing.Pen(Color.Black)

SolidBrush brush = new SolidBrush(Color.Black)

Rectangle rect = new Rectangle(5, 5, this.Width-10, this.Height-10)

SolidBrush brushFill = new SolidBrush(Color.Silver)

if (pressed

brushFill.Color = Color.Black
brush.Color = Color.White


graphics.FillRectangle(brushFill, 2, 2, this.Width-4, this.Height-4)

Font font = new Font(this.Font.Name, this.Font.Size-1, FontStyle.Bold)

graphics.DrawString(this.Text, font, brush, rect)

graphics.DrawRectangle(pen, 2, 2, this.Width-4, this.Height-4)

base.OnPaint(pe)
}
 
A

Alex Yakhnin [MVP]

A

Alex Yakhnin [MVP]

that is only single line though yes?
Umm.. I think yes.

But it should be easy to change to make it multiline:
You'd need to change the DrwString call to use a different override:

Instead of:

graphicsBuffer.DrawString(this.Text, this.Font, brushText, point.X,
point.Y);

you can use this:

Rectangle textRect = new Rectangle(point.X, point.Y, this.Width- 4,
this.Height - 4);

graphicsBuffer.DrawString(this.Text, this.Font, textRect);
 
N

Nick Randolph

One of the issues with using this alternative override is that it is very
slow. We had a complex custom control where we were writing around 20 odd
string in rectangle defined areas. We couldn't work out what was causing
the really bad refreshing problems. It turned out that this override must
do a lot of background processing to determine how to layout the string. We
removed the writing and it improved almost 100 fold!!

Nick
 
C

Chris Tacke, eMVP

That doesn't sound right to me - that's not that much processing - I've done
far more on custom controls with no flickering. You double-buffered and
also overrode OnPaintBackground with an empty method?

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net
 
N

Nick Randolph

Yeh, I agree - and was a little surprised at what was happening. We are
definitely double-buffering and overriding the OnPaintBackground. I will
have to investigate a little more (didn't have time when we had the problem
as we had to get the product out the door, but will hopefully be able to
re-visit the issue for the next release).

Nick
 
N

Nick Randolph

Chris

Ok, after a bit more experimenting, you are right there is really no
noticable performance difference between the two overloads. In fact it is
just poor performance by the drawstring method in general.

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top