[RESOLVED] [win32] - how change the STATIC text alignment?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] [win32] - how change the STATIC text alignment?

  1. #1
    Join Date
    Apr 2009
    Posts
    626

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

    i did these code like the Border and the Border works fine... but the text alignment isn't changed
    Code:
    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);
            }
        }
    i don't understand what i'm doing wrong

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: [win32] - how change the STATIC text alignment?

    Try to call InvalidateRect for this static control.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    626

    Re: [win32] - how change the STATIC text alignment?

    Quote Originally Posted by VictorN View Post
    Try to call InvalidateRect for this static control.
    the problem isn't that(i have tested)... please see my 's' variable

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: [win32] - how change the STATIC text alignment?

    Well, the main your problem in this Forum is your code snippets are almost always not complete, they contain a lot of undefined or undeclared variables. Yor was notified many times that code snippets you have posted should be very easy tested by other people but you ignore it.
    You also ignore the debugging!

    So what are you expecting we will do with your questions? Test/debug these snippets for you? Well, it is in the most cases impossible!
    So the only thing we can do is just to guess!

    Now about your 's' variable: why do you declare it as long but then cast it to LONG_PTR?
    Why not just to declare it as LONG_PTR?
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: [win32] - how change the STATIC text alignment?

    You haven't got one check for function call failure anywhere! As myself and others have pointed out several times, you need to check the return value from all function calls for failure. I would be coding this routine something like this

    Code:
    void setTextAlignment(int textalignment)
    {
    LONG_PTR s;
    
    	if (textalignment == intTextAlignment)
    		return;
    
    	//delete the last text alignment
    	SetLastError(0);
            if (!(s = GetWindowLongPtr(hwnd, GWL_STYLE)) && GetLastError())
    		MessageBox(NULL, "Bad GetWindowLongPtr", "error", MB_OK);
    
    	switch (intTextAlignment) {
    		case 0:
                            s &= ~(SS_LEFT);
    			break;
    
    		case 1:
    			s &= ~(SS_CENTER);
    			break;
    
    		case 2:
    			s &= ~(SS_RIGHT);
    			break;
    
    		default:
    			MessageBox(NULL, "Bad intTextAlignment", "error", MB_OK);
    	}
    
    	//put the new text alignment
    	switch (textalignment) {
    		case 0:
    			s |= (SS_LEFT);
    			break;
    
    		case 1:
    			s |= (SS_CENTER);
    			break;
    
    		case 2:
    			s |= (SS_RIGHT);
    			break;
    
    		default:
    			MessageBox(NULL, "Bad textalignment", "error", MB_OK);
    	}
    
    	intTextAlignment = textalignment;
    
    	SetLastError(0);
    	if (!SetWindowLongPtr(hwnd, GWL_STYLE, s) && GetLastError())
    		MessageBox(NULL, "Bad SetWindowLongPtr", "error", MB_OK);
    
    	if (!SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_DRAWFRAME))
    		MessageBox(NULL, "Bad SetWindowPos", "error", MB_OK);
    }
    In relation to your code,
    Code:
    textalignment=intTextAlignment;
    
                //put the new text alignment
                if(textalignment==0)
    you are setting textalignment to intTextAlignment and then testing textalignment! Put the assignment the other way round. You are setting the new style back to the old style that you have just cleared
    Last edited by 2kaud; February 8th, 2014 at 03:40 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2009
    Posts
    626

    Re: [win32] - how change the STATIC text alignment?

    Quote Originally Posted by 2kaud View Post
    I would be coding this routine something like this
    Code:
    void setTextAlignment(int textalignment)
    {
    LONG_PTR s;
    
    	if (textalignment == intTextAlignment)
    		return;
    
    	//delete the last text alignment
    	SetLastError(0);
            if (!(s = GetWindowLongPtr(hwnd, GWL_STYLE)) && GetLastError())
    		MessageBox(NULL, "Bad GetWindowLongPtr", "error", MB_OK);
    
    	switch (intTextAlignment) {
    		case 0:
                            s = s & ~(SS_LEFT);
    			break;
    
    		case 1:
    			s = s & ~(SS_CENTER);
    			break;
    
    		case 2:
    			s = s & ~(SS_RIGHT);
    			break;
    
    		default:
    			MessageBox(NULL, "Bad intTextAlignment", "error", MB_OK);
    	}
    
    	//put the new text alignment
    	switch (textalignment) {
    		case 0:
    			s = s | (SS_LEFT);
    			break;
    
    		case 1:
    			s = s | (SS_CENTER);
    			break;
    
    		case 2:
    			s = s | (SS_RIGHT);
    			break;
    
    		default:
    			MessageBox(NULL, "Bad textalignment", "error", MB_OK);
    	}
    
    	intTextAlignment = textalignment;
    
    	SetLastError(0);
    	if (!SetWindowLongPtr(hwnd, GWL_STYLE, s) && GetLastError())
    		MessageBox(NULL, "Bad SetWindowLongPtr", "error", MB_OK);
    
    	if (!SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_DRAWFRAME))
    		MessageBox(NULL, "Bad SetWindowPos", "error", MB_OK);
    }
    In relation to your code,
    Code:
    textalignment=intTextAlignment;
    
                //put the new text alignment
                if(textalignment==0)
    you are setting textalignment to intTextAlignment and then testing textalignment! Put the assignment the other way round, You are setting the new style back to the old style that you have just cleared
    sorry, your code maybe my control hide it.
    heres the code working fine:
    Code:
    void setTextAlignment(int textalignment)
        {
            intTextAlignment=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);
        }
    let me ask 1 thing: here we can change the center, left and right alignment without a problem. but is there a way for justify?

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: [win32] - how change the STATIC text alignment?

    Do you mean using SS_CENTERIMAGE, SS_RIGHTJUST styles?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2009
    Posts
    626

    Re: [win32] - how change the STATIC text alignment?

    Quote Originally Posted by 2kaud View Post
    Do you mean using SS_CENTERIMAGE, SS_RIGHTJUST styles?
    i have tested the both:
    SS_CENTERIMAGE: put the text in vertical center way;
    SS_RIGHTJUST: seems not justify the text, but put the text on left

  9. #9
    Join Date
    Apr 2009
    Posts
    626

    Re: [win32] - how change the STATIC text alignment?

    but maybe you have right, because it can move the last word to the next line.
    thanks for all

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,962

    Re: [win32] - how change the STATIC text alignment?

    Quote Originally Posted by Cambalinho View Post
    let me ask 1 thing: here we can change the center, left and right alignment without a problem. but is there a way for justify?
    Yeah, let me ask one other thing: why don't you find this out right from MS documentation?

    Static Control Styles
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center