?
Solved

Setting the text of a button-control to left justified...

Posted on 2003-03-27
18
Medium Priority
?
1,285 Views
Last Modified: 2012-05-04
Hi, friends! A small, stupid question...

I have a button control (CButton) in a MFC-program I'm writing. At runtime, I sometimes need to change the button's text (caption) from centered to left justified (and sometimes back again). It's easy to set in the Dialog Editor, but how do I change it at runtime? I can't find any CButton or CWnd member function to use. Where does Windows/MFC store that property of the button/window?
0
Comment
Question by:AndersO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +6
18 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8216843
I don't believe you find it in the CButton. It is a generic property shared among all controls.

I don't believe you find it in CWnd either, that is a generic class for any window, not just controls.

I would believe you find it in a class in between those two. Check for a property called 'Alignment' or 'TextAlignment' or some such. I am sorry but I don't have the exact name of the property here as I don't have the MFC documentation available and I am sitting on a Linux machine. However, I am sure that if you search all member functions of all classes between CWnd and CButton you should find it somewhere.

Alf
0
 
LVL 10

Expert Comment

by:substand
ID: 8216964
look for a propery called "fmt" in the CButton class.

i don't know if it exists, but it does exist for list view controls, so somthing similar ought to exist for buttons.

if you have

CButton myButt

you could set it as  

myButt.fmt=...


 
0
 

Author Comment

by:AndersO
ID: 8217029
> I don't believe you find it in the CButton. It
> is a generic property shared among all controls.
>
> I don't believe you find it in CWnd either, that
> is a generic class for any window, not just controls.
>
> I would believe you find it in a class in between
> those two.

But there is no class in between CButton and CWnd! All controls (CButton, CEdit and so on) are based directly on CWnd. But I can't find a property/member function in CButton for this (there are only a few), and none of the CWnd-functions/styles seems to apply (but I'm no MFC expert, so maybe I missed something there).

> look for a propery called "fmt" in the CButton class.
>
> i don't know if it exists

No, it doesn't. Besides from "set/get if checked" and some bitmap functions, the only member functions there are are to set/get "button style". But there is no button styles (BS_-values) for justification...

More ideas, anyone...?   :-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Salte
ID: 8217114
Hmm.... ok, so MFC has defined CButton as a baseclass of CWnd? What about CControl? I thought that class was somewhere in between those at least.

In any case, this require details of the MFC and I don't have that in front of me so I am afraid I can't help more. I do know that MFC is crap and to be avoided but I guess that doesn't help you much ;-)

One thing I do know though is that whatever you can do with MFC you can also do it manually by sending a message directly to the window handle of the CButton object. You could try to search through windows messages and see what you find there.

Alf
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 8217272
"I do know that MFC is crap and to be avoided but I guess that doesn't help you much ;-)"

Just out of interest Alf why do you think this, and this being the case what is your preferred development language for Windows GUI applications ?

DJ
0
 
LVL 12

Expert Comment

by:Salte
ID: 8217378
For windows GUI applications I would use .Net or Borland C++ builder. MFC is out of the question, it was an option a couple of years ago but that is a long time ago.

The MFC is crap because it adds, on the whole, very little, is structured (designed) badly and forces you to think along lines that works very well for some applications but for all others it becomes artificial and unnatural. It simply has the wrong tools for the job and using it forces you to do things in an odd way.

Alf
0
 
LVL 12

Expert Comment

by:Salte
ID: 8217401
Btw, the above statement does not mean to imply that Borland C++ Builder or .net is perfect, but it means that at the moment they are the best thing available to the general public. There are numerous things in .Net I would have liked to see different, some minor and some might say unimportant things and others are bigger. My main gripe with VCL (Borland C++ builder) is that it is implemented in Pascal (delphi). I think it would have been a better design if they had started with C++ first and then built their support for Delphi on top of that - but then Delphi is a continuation of Turbo Pascal and is Borland's own language so you can't really blame them for it but it does imply that some things doesn't work the way you would like them to from a C++ point of view.

Alf
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 8217418
Thank you for that Alf, as always your feedback is highly valued and appreciated.
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 8218323


Well, here's my incredibly cheesy solution:


void CTestButtonDlg::OnTest()
{
     CButton* pButton = (CButton*)GetDlgItem(IDC_TEST);
     static int i = 0;

     if ( i )
     {
          pButton->SetWindowText( "Test          " );
          i = 0;

     }
     else
     {
          pButton->SetWindowText( "          Test" );
          i = 1;

     }
}
0
 
LVL 6

Expert Comment

by:GaryFx
ID: 8218340
Have you tried using CButton::SetButtonStyle(BS_LEFT) or CButton::SetButtonStyle(BS_CENTER), as appropriate?

Gary
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 8218364
SetButtonStyle will change it from a push button to a radio button, but won't change alignment...I've tried that
0
 
LVL 12

Expert Comment

by:Salte
ID: 8219174
buttons styles are for radio buttons (and possibly also checkboxes) and determine if the caption should come to the right or the left of the radio button. I doubt 'CENTER' would be an appropriate setting.

Alf
0
 

Author Comment

by:AndersO
ID: 8219260
>   pButton->SetWindowText( "Test          " );
> ...
>   pButton->SetWindowText( "          Test" );

Hmmm... That's a - shall we say - "innovative" solution... :-) But it won't do, I'm afraid. I'm displaying variable data in a lot of chose-which-one-to-process-buttons, and I'm not interested in having to calculate text widths and such. And it's a lot of buttons - otherwise it would be easy to have two buttons and show one/hide one.

> SetButtonStyle will change it from a push button to
> a radio button, but won't change alignment...I've
> tried that

I didn't even know there was BS_LEFT / BS_CENTER button styles; they are not listed in the CButton-documentation. I fount them now in the documentation for button styles for Windows (outside MFC). I've tried changing the button style with these values, but nothing happends. (Setting BS_LEFTTEXT on a check box after it's been created doesn't work either.)

But if I CREATE a new button with BS_LEFT, then it works fine! (So does BS_CENTER and BS_RIGHT.) Those values seems to be valid only at creation time...

I want to change my buttons after they are created, though. Other suggestions...?
0
 
LVL 2

Expert Comment

by:MirkoLMV
ID: 8232198
Hard-code solution is
 SendMessage (hwndButton, BM_SETSTYLE, BS_LEFT, TRUE) ;
Styles can be BS_LEFT, BS_CENTER, BS_RIGHT or more others, and even a combination of styles can be specified as wParam value.
In lParam TRUE value stands for a redrawing intention, and we can have FALSE for preenting the redrawing.
The Message is described in the Microsoft references : msdn.microsoft.com/libraries
I have no significant experience with MFC, but I would expect that a message specific to a button to be processed by the class which encapsulates a button.
The question can not be considered conventional, since it is an issue not even mentioned in tutorials for Windows API programming.

Good luck !
0
 

Author Comment

by:AndersO
ID: 8282768
> Hard-code solution is
> SendMessage (hwndButton, BM_SETSTYLE, BS_LEFT, TRUE) ;
> Styles can be BS_LEFT, BS_CENTER, BS_RIGHT or more
> others, and even a combination of styles can be
> specified as wParam value.

I'm afraid that doesn't work either, MirkoLMV. The SetButtonStyle member function for CButton (which I first thought would do the job) does exactly that... (And I've tried doing SendMessage "by hand".)

Both SetButtonStyle and BM_SETSTYLE WILL change a button from e.g. a push button to a radio button (as corduroy9 says), but (strangely enough) won't change alignment... That seems to be a creation-time-only setting.

I've given up on this--and implemented it in a way that works: making two buttons in the dialog editor for every button concerned, one centered and one left justified. And then used SW_SHOW/SW_HIDE to show one of them and hide the other one...

But thanx for all your help!
0
 
LVL 12

Expert Comment

by:Salte
ID: 8282937
Just to check - you did try to set the text alignment for the window. A button is a window and so the handle to the button control is also a handle to a window and I wonder if you could set the text alignment of that window in any way.

One problem is that the SetTextAlign require a DC but you don't have a DC you have a window handle.

It's possible it is a simple window flag - do you know where the alignment show up in the window creation code? which parameter is used to receive the text alignment? I assume it is a parameter to the CreateWindowEx() function or some such. If so it might be able to modify that flag using some other function.

Alf
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9551582
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9594695
PAQed, with points refunded (30)

Computer101
E-E Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question