Button text alignment

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 
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,

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

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

Tim Wilson said:
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 said:
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.
 
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 said:
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

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

Tim Wilson said:
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 said:
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.
 
Great stuff guys, many thanks for that!



Tim Wilson said:
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 said:
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

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

Tim Wilson said:
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

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.
 
Back
Top