none
[win32] - how change the STATIC text alignment?

    Question

  • i did these function for change the STATIC control text alignment, but isn't working :(

    void setTextAlignment(int textalignment)
        {
            long s;
            if(textalignment!=intTextAlignment)
            {
                //delete the last text alignment
                if(intTextAlignment==0)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s & ~(SS_LEFT);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if (intTextAlignment==1)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s & ~(SS_CENTER);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if(intTextAlignment==2)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s & ~(SS_RIGHT);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
     
                textalignment=intTextAlignment;
     
                //put the new text alignment
                if(textalignment==0)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s | (SS_LEFT);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if (textalignment==1)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s | (SS_CENTER);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if(textalignment==2)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE);
                    s = s | (SS_RIGHT);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                SetWindowPos(hwnd, 0, 0, 0, 0, 0,
                        SWP_NOZORDER| SWP_NOSIZE | SWP_NOMOVE|SWP_NOACTIVATE |SWP_NOCOPYBITS | SWP_DRAWFRAME);
            }
        }

    (hwnd is the STATIC handle window)

    i think my code is ok, but the text alignment isn't changed :(

    heres the styles, when i create the window:

    styles:  SS_LEFT|WS_VISIBLE|WS_CHILD| SS_LEFTNOWORDWRAP
    extended styles: WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT

    please can anyone advice me?


    Saturday, February 8, 2014 4:47 PM

Answers

  • Try this advice:

    auto new_style = SS_RIGHT; // TODO: calculate depending on 'textalignment'.
    
    if( new_style == SS_LEFT ) new_style = SS_LEFTNOWORDWRAP;
    auto s = GetWindowLongPtr( hwnd, GWL_STYLE );
    s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;
    SetWindowLongPtr( hwnd, GWL_STYLE, s );
    InvalidateRect( hwnd, NULL, TRUE );

    • Marked as answer by Cambalinho Saturday, February 8, 2014 9:43 PM
    Saturday, February 8, 2014 7:07 PM
  • On 2/8/2014 4:32 PM, Cambalinho wrote:

         s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;

    Again, the correct way to do this is:

    s = (s & ~SS_TYPEMASK) | new_style;

    let me ask anotherthing: i can do the left, center and right without a problem, but is there the justify too?

    No, not really. You can of course set the type to SS_OWNERDRAW and draw the text yourself any way you want, justified or otherwise.


    Igor Tandetnik
    • Marked as answer by Cambalinho Saturday, February 8, 2014 10:00 PM
    Saturday, February 8, 2014 9:50 PM

All replies

  • Try this advice:

    auto new_style = SS_RIGHT; // TODO: calculate depending on 'textalignment'.
    
    if( new_style == SS_LEFT ) new_style = SS_LEFTNOWORDWRAP;
    auto s = GetWindowLongPtr( hwnd, GWL_STYLE );
    s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;
    SetWindowLongPtr( hwnd, GWL_STYLE, s );
    InvalidateRect( hwnd, NULL, TRUE );

    • Marked as answer by Cambalinho Saturday, February 8, 2014 9:43 PM
    Saturday, February 8, 2014 7:07 PM
  • Try this advice:

    auto new_style = SS_RIGHT; // TODO: calculate depending on 'textalignment'.
    
    if( new_style == SS_LEFT ) new_style = SS_LEFTNOWORDWRAP;
    auto s = GetWindowLongPtr( hwnd, GWL_STYLE );
    s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;
    SetWindowLongPtr( hwnd, GWL_STYLE, s );
    InvalidateRect( hwnd, NULL, TRUE );


    works fine... so what is my code problem?
    Saturday, February 8, 2014 7:38 PM
  • On 2/8/2014 2:38 PM, Cambalinho wrote:

    works fine... so what is my code problem?

    SS_LEFT, SS_CENTER, SS_RIGHT and SS_LEFTNOWORDWRAP are not independent bits, and cannot be combined using bitwise operations. They are different values of the static control type, which you can extract from the style using SS_TYPEMASK. If you want to change the type, you do it like this:

    newStyle = (oldStyle & ~SS_TYPEMASK) | newType;

    You start with a control of type SS_LEFTNOWORDWRAP (which, again, is distinct from SS_LEFT, SS_RIGHT or SS_CENTER). The numeric values of various types just happen to be such that

    SS_LEFTNOWORDWRAP | SS_LEFT == SS_LEFTNOWORDWRAP
    SS_LEFTNOWORDWRAP | SS_CENTER == SS_OWNERDRAW
    SS_LEFTNOWORDWRAP | SS_RIGHT == SS_BITMAP

    Thus, your code is really flipping between SS_LEFTNOWORDWRAP, SS_OWNERDRAW and SS_BITMAP types, and not SS_LEFT, SS_CENTER and SS_RIGHT as you seem to expect.


    Igor Tandetnik
    Saturday, February 8, 2014 8:05 PM
  • On 2/8/2014 2:38 PM, Cambalinho wrote:

    works fine... so what is my code problem?

    SS_LEFT, SS_CENTER, SS_RIGHT and SS_LEFTNOWORDWRAP are not independent bits, and cannot be combined using bitwise operations. They are different values of the static control type, which you can extract from the style using SS_TYPEMASK. If you want to change the type, you do it like this:

    SS_LEFTNOWORDWRAP | SS_LEFT == SS_LEFTNOWORDWRAP
    SS_LEFTNOWORDWRAP | SS_CENTER == SS_OWNERDRAW
    SS_LEFTNOWORDWRAP | SS_RIGHT == SS_BITMAP
    

    Thus, your code is really flipping between SS_LEFTNOWORDWRAP, SS_OWNERDRAW and SS_BITMAP types, and not SS_LEFT, SS_CENTER and SS_RIGHT as you seem to expect.


    Igor Tandetnik

    sorry, but i continue wit problems :(

    void setTextAlignment(int textalignment)
        {
            long s=GetWindowLongPtr(hwnd,GWL_STYLE);
            if(textalignment!=intTextAlignment)
            {
    
                textalignment=intTextAlignment;
    
                //put the new text alignment
                if(textalignment==0)
                {
                    s = (s & ~SS_TYPEMASK) | SS_LEFT;
                    if(blnTextWrap==false) s=s|SS_LEFTNOWORDWRAP;
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if (textalignment==1)
                {
                    s = (s & ~SS_TYPEMASK) | SS_CENTER;
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if(textalignment==2)
                {
                    s = (s & ~(SS_TYPEMASK)) | (SS_RIGHT);
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                SetWindowPos(hwnd, 0, 0, 0, 0, 0,
                        SWP_NOZORDER| SWP_NOSIZE | SWP_NOMOVE|SWP_NOACTIVATE |SWP_NOCOPYBITS | SWP_DRAWFRAME);
            }
        }

    Saturday, February 8, 2014 8:12 PM
  • On 2/8/2014 3:12 PM, Cambalinho wrote:

    sorry, but i continue wit problems :(

    void setTextAlignment(int textalignment)
             {
                     long s=GetWindowLongPtr(hwnd,GWL_STYLE);
                     if(textalignment!=intTextAlignment)
                     {
    
                             textalignment=intTextAlignment;

    Well, you have promptly forgotten the value of the parameter you were called with, and replaced it with the value you have in a member variable. So naturally, the style never changes.


    Igor Tandetnik


    Igor Tandetnik
    Saturday, February 8, 2014 8:25 PM
  • On 2/8/2014 3:12 PM, Cambalinho wrote:

    sorry, but i continue wit problems :(

    void setTextAlignment(int textalignment)
             {
                     long s=GetWindowLongPtr(hwnd,GWL_STYLE);
                     if(textalignment!=intTextAlignment)
                     {
    
                             textalignment=intTextAlignment;

    Well, you have promptly forgotten the value of the parameter you were called with, and replaced it with the value you have in a member variable. So naturally, the style never changes.


    Igor Tandetnik


    Igor Tandetnik

    please see these code that works fine:

    void setBorder(int border)
        {
            if (intBorder!=border)
            {
                long s=0;
                if(border==0)
                {
                    if (intBorder==1)
                    {
                        s = GetWindowLongPtr(hwnd,GWL_EXSTYLE);
                        s = s & ~(WS_EX_CLIENTEDGE);
                        SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
                    }
                    else if (intBorder==2)
                    {
                        s = GetWindowLongPtr(hwnd,GWL_STYLE);
                        s = s & ~(WS_BORDER);
                        SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                    }
                    else if(intBorder==3)
                    {
                        s = GetWindowLongPtr(hwnd,GWL_EXSTYLE);
                        s = s & ~(WS_EX_STATICEDGE);
                        SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
                    }
                }
                else if(border==1)
                {
                    s = GetWindowLongPtr(hwnd,GWL_EXSTYLE) | WS_EX_CLIENTEDGE;
                    SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
                }
                else if(border==2)
                {
                    s = GetWindowLongPtr(hwnd,GWL_STYLE) | WS_BORDER;
                    SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
                }
                else if(border==3)
                {
                    s = GetWindowLongPtr(hwnd,GWL_EXSTYLE) | WS_EX_STATICEDGE;
                    SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
                }
                intBorder=border;
                //the SetWindowPos() is for update the control
                SetWindowPos(hwnd, 0, 0, 0, 0, 0,
                        SWP_NOZORDER| SWP_NOSIZE | SWP_NOMOVE|SWP_NOACTIVATE |SWP_NOCOPYBITS | SWP_DRAWFRAME);
            }
        }

    i'm trying use the same tecnic

    Saturday, February 8, 2014 8:28 PM
  • On 2/8/2014 3:28 PM, Cambalinho wrote:

    please see these code that works fine:

    void setBorder(int border)
             {
                     if (intBorder!=border)

    You don't do "border = intBorder;" here. Why do you feel the need to do it in setTextAlignment?


    Igor Tandetnik
    Saturday, February 8, 2014 8:54 PM
  • finally i put it to work:

    void setTextAlignment(int textalignment)
        {
            LONG_PTR new_style=0;
    
            if(textalignment==0)
                new_style = SS_LEFTNOWORDWRAP;
            else if( textalignment == 1 )
                new_style = SS_CENTER;
            else if(textalignment==2)
                new_style =SS_RIGHT;
    
            LONG_PTR s = GetWindowLongPtr( hwnd, GWL_STYLE );
    
            s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;
    
            SetWindowLongPtr( hwnd, GWL_STYLE, s );
            SetWindowPos(hwnd, 0, 0, 0, 0, 0,
                        SWP_NOZORDER| SWP_NOSIZE | SWP_NOMOVE|SWP_NOACTIVATE |SWP_NOCOPYBITS | SWP_DRAWFRAME);
        }

    if you see memory leak or something, please tell me ;)

    let me ask anotherthing: i can do the left, center and right without a problem, but is there the justify too?

    Saturday, February 8, 2014 9:32 PM
  • On 2/8/2014 4:32 PM, Cambalinho wrote:

         s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;

    Again, the correct way to do this is:

    s = (s & ~SS_TYPEMASK) | new_style;

    let me ask anotherthing: i can do the left, center and right without a problem, but is there the justify too?

    No, not really. You can of course set the type to SS_OWNERDRAW and draw the text yourself any way you want, justified or otherwise.


    Igor Tandetnik
    • Marked as answer by Cambalinho Saturday, February 8, 2014 10:00 PM
    Saturday, February 8, 2014 9:50 PM
  • On 2/8/2014 4:32 PM, Cambalinho wrote:

         s = ( s & ~( SS_LEFT | SS_CENTER | SS_RIGHT | SS_LEFTNOWORDWRAP ) ) | new_style;

    Again, the correct way to do this is:

    s = (s & ~SS_TYPEMASK) | new_style;
    

    let me ask anotherthing: i can do the left, center and right without a problem, but is there the justify too?

    No, not really. You can of course set the type to SS_OWNERDRAW and draw the text yourself any way you want, justified or otherwise.


    Igor Tandetnik

    sorry can you explain better the SS_TYPEMASK?
    Saturday, February 8, 2014 9:53 PM
  • On 2/8/2014 4:53 PM, Cambalinho wrote:

    sorry can you explain better the SS_TYPEMASK?

    Which part of my previous explanation do you find unclear?


    Igor Tandetnik
    Saturday, February 8, 2014 10:12 PM
  • On 2/8/2014 4:53 PM, Cambalinho wrote:

    sorry can you explain better the SS_TYPEMASK?

    Which part of my previous explanation do you find unclear?


    Igor Tandetnik

    from MSDN doc, i read( http://msdn.microsoft.com/en-us/library/windows/desktop/bb760773%28v=vs.85%29.aspx ):

    "A composite style bit that results from using the OR operator on SS_* style bits. Can be used to mask out valid SS_* bits from a given bitmask. Note that this is out of date and does not correctly include all valid styles. Thus, you should not use this style."

    i'm confused what means.


    anotherthing: the SS_RIGHTJUST style can do the justification, but isn't in MSDN doc :(
    • Edited by Cambalinho Saturday, February 8, 2014 10:17 PM
    Saturday, February 8, 2014 10:16 PM
  • On 2/8/2014 5:16 PM, Cambalinho wrote:

    "A composite style bit that results from using the OR operator on SS_* style bits. Can be used to mask out valid SS_* bits from a given bitmask. Note that this is out of date and does not correctly include all valid styles. Thus, you should not use this style."

    Well, then, you shouldn't use this style. Ignore my mentioning it. My information is out of date on this one.

    i'm confused what means.
    anotherthing: the SS_RIGHTJUST style can do the justification, but isn't in MSDN doc :(

    What do you mean, isn't? It's right there in the very same article you've just given a link to:

    *SS_RIGHTJUST*: The lower right corner of a static control with the SS_BITMAP or SS_ICON style is to remain fixed when the control is resized. Only the top and left sides are adjusted to accommodate a new bitmap or icon.

    Doesn't sound to me like it has anything whatsoever to do with justifying text.


    Igor Tandetnik
    Sunday, February 9, 2014 12:06 AM
  • On 2/8/2014 5:16 PM, Cambalinho wrote:

    "A composite style bit that results from using the OR operator on SS_* style bits. Can be used to mask out valid SS_* bits from a given bitmask. Note that this is out of date and does not correctly include all valid styles. Thus, you should not use this style."

    Well, then, you shouldn't use this style. Ignore my mentioning it. My information is out of date on this one.

    i'm confused what means.
    anotherthing: the SS_RIGHTJUST style can do the justification, but isn't in MSDN doc :(

    What do you mean, isn't? It's right there in the very same article you've just given a link to:

    *SS_RIGHTJUST*: The lower right corner of a static control with the SS_BITMAP or SS_ICON style is to remain fixed when the control is resized. Only the top and left sides are adjusted to accommodate a new bitmap or icon.

    Doesn't sound to me like it has anything whatsoever to do with justifying text.


    Igor Tandetnik

    maybe you have right....

    thanks for all... thanks

    Sunday, February 9, 2014 11:09 AM
  • This is how I formed this function:

    void setTextAlignment(HWND hwnd,int intTextAlignment)
    {
    	long s;
    	int textalignment = GetWindowLongPtr(hwnd, GWL_STYLE);
    	if (textalignment != intTextAlignment)
    	{
    		//delete the last text alignment
    		if (intTextAlignment == 0)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_LEFT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (intTextAlignment == 1)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_CENTER);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (intTextAlignment == 2)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_RIGHT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    
    		textalignment = intTextAlignment;
    
    		//put the new text alignment
    		if (textalignment == 0)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_LEFT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (textalignment == 1)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_CENTER);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (textalignment == 2)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_RIGHT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		SetWindowPos(hwnd, 0, 0, 0, 0, 0,
    			SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_DRAWFRAME);
    	}
    }
    


    Michael Haephrati

    Wednesday, June 12, 2019 12:51 PM
  • This is how I formed this function:

    void setTextAlignment(HWND hwnd,int intTextAlignment)
    {
    	long s;
    	int textalignment = GetWindowLongPtr(hwnd, GWL_STYLE);
    	if (textalignment != intTextAlignment)
    	{
    		//delete the last text alignment
    		if (intTextAlignment == 0)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_LEFT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (intTextAlignment == 1)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_CENTER);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (intTextAlignment == 2)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s & ~(SS_RIGHT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    
    		textalignment = intTextAlignment;
    
    		//put the new text alignment
    		if (textalignment == 0)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_LEFT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (textalignment == 1)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_CENTER);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		else if (textalignment == 2)
    		{
    			s = GetWindowLongPtr(hwnd, GWL_STYLE);
    			s = s | (SS_RIGHT);
    			SetWindowLongPtr(hwnd, GWL_STYLE, (LONG_PTR)s);
    		}
    		SetWindowPos(hwnd, 0, 0, 0, 0, 0,
    			SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_DRAWFRAME);
    	}
    }


    Michael Haephrati

    thank you so much for all
    Wednesday, June 12, 2019 7:44 PM