Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Button text alignment

51 views
Skip to first unread message

Rob S

unread,
Apr 1, 2005, 7:43:03 AM4/1/05
to
Hi there,

Given the string below:

string s = "hello\n" + "world" + "align me";

Button1.Text = s;

No matter what i try (with either the OpenNetCF ButtonEx control or the
standard one) i cannot get each line to center on the button.

Any ideas anyone?

Try it in windows forms (c#) and it works fine. I've tried all the text
alignment options.

Tim Wilson UNDERSCORE AT PERIOD

unread,
Apr 1, 2005, 9:52:34 AM4/1/05
to
You can change the Button control to multiline with a few pinvokes.

[System.Runtime.InteropServices.DllImport("coredll")]
private static extern IntPtr GetCapture();

[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);

private const int GWL_STYLE = -16;

private const int BS_CENTER = 0x00000300;
private const int BS_VCENTER = 0x00000C00;
private const int BS_MULTILINE = 0x00002000;

private void SetButtonStyle(Button ctrl)
{
IntPtr hWnd;
int style;

ctrl.Capture = true;
hWnd = GetCapture();
ctrl.Capture = false;

style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style | BS_CENTER | BS_VCENTER |
BS_MULTILINE));

ctrl.Refresh();
}

...

SetButtonStyle(this.button1);

In fact, you shouldn't need the (BS_CENTER | BS_VCENTER) styles as they
should already be present for a normal button. But I've included them just
to be explicit.

--
Tim Wilson
.Net Compact Framework MVP

"Rob S" <Ro...@discussions.microsoft.com> wrote in message
news:5F250FD1-2DD1-4AF9...@microsoft.com...

Peter Foot [MVP]

unread,
Apr 1, 2005, 9:50:18 AM4/1/05
to
I haven't looked at the code in ButtonEx to see how it is implemented. One
observation is that a new line on Windows CE is \r\n rather than just \n -
does this help?

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

"Rob S" <Ro...@discussions.microsoft.com> wrote in message
news:5F250FD1-2DD1-4AF9...@microsoft.com...

Peter Foot [MVP]

unread,
Apr 1, 2005, 1:04:17 PM4/1/05
to
Tim,

That's a great workaround - hadn't thought of that! Unfortunately the same
technique is still required to support multi-line text in .NETCF 2.0 beta,
however you can skip the GetCapture call since the Controls in v2.0 expose
their Handle properties.

Peter

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:%233BgPps...@TK2MSFTNGP10.phx.gbl...

Tim Wilson UNDERSCORE AT PERIOD

unread,
Apr 1, 2005, 1:13:38 PM4/1/05
to
I'm actually redoing the ButtonEx control right now and I have put this on
the list of things to consider. At this point, it looks like all the
calculations will need to be done manually since ButtonEx is entirely custom
drawn. I'm not sure if this is going to make its way in but this is one area
where ButtonEx does not do as good a job as Button.

--
Tim Wilson
.Net Compact Framework MVP

"Peter Foot [MVP]" <feed...@nospam-inthehand.com> wrote in message
news:%23PFl6Uu...@TK2MSFTNGP10.phx.gbl...

Rob S

unread,
Apr 4, 2005, 4:03:02 AM4/4/05
to
Great stuff guys, many thanks for that!

"Tim Wilson" wrote:

> I'm actually redoing the ButtonEx control right now and I have put this on
> the list of things to consider. At this point, it looks like all the
> calculations will need to be done manually since ButtonEx is entirely custom
> drawn. I'm not sure if this is going to make its way in but this is one area
> where ButtonEx does not do as good a job as Button.
>
> --
> Tim Wilson

> ..Net Compact Framework MVP

0 new messages