How to set the text alignment of a static control ?

Windows specific questions.
Post Reply
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to set the text alignment of a static control ?

Post by kcvinu »

Hi all,
I am creating a static control (Label) in my program with win32 api. So far so good. I want to implement a text alignment property to my Label type. By default, Windows is creating a label with top-left text alignment. And we can change it to center with adding these two flags.

Code: Select all

label.style = current_style or SS_LEFT or SS_CENTERIMAGE ' // for  left-center align
label.style = current_style or SS_CENTER or SS_CENTERIMAGE ' // for  center align
label.style = current_style or SS_RIGHT or SS_CENTERIMAGE ' // for right- center align
But how do we align it to bottom-left and and bottom-center and bottom-right ? I can't find any flags related to bottom alignment. Please help.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to set the text alignment of a static control ?

Post by jj2007 »

See the doc. I'm afraid that is a case for an ownerdraw exercise...
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to set the text alignment of a static control ?

Post by kcvinu »

@jj2007,
Thanks. Then let me try to experiment with DrawText function without using owner draw style. I need to figure out where to draw the text. I mean these are my assumptions.
1. Draw text right after creating the control
2. Draw text in the paint event.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to set the text alignment of a static control ?

Post by jj2007 »

It might even be easier to draw on the main window's DC. No control needed.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: How to set the text alignment of a static control ?

Post by PaulSquires »

In my program I implemented it as SS_OWNERDRAW in order to get the proper alignments. Here are the styles I used for each text position.

Code: Select all

Function wfxLabel.GetTextAlignStyleValue( ByVal nValue As LabelAlignment ) As Long
   dim as long wsStyle 
   Select Case nValue
      Case LabelAlignment.BottomCenter: wsStyle = DT_CENTER Or DT_BOTTOM  or DT_SINGLELINE
      Case LabelAlignment.BottomLeft:   wsStyle = DT_LEFT   Or DT_BOTTOM  or DT_SINGLELINE
      Case LabelAlignment.BottomRight:  wsStyle = DT_RIGHT  Or DT_BOTTOM  or DT_SINGLELINE
      Case LabelAlignment.MiddleCenter: wsStyle = DT_CENTER Or DT_VCENTER or DT_SINGLELINE
      Case LabelAlignment.MiddleLeft:   wsStyle = DT_LEFT   Or DT_VCENTER or DT_SINGLELINE
      Case LabelAlignment.MiddleRight:  wsStyle = DT_RIGHT  Or DT_VCENTER or DT_SINGLELINE
      Case LabelAlignment.TopCenter:    wsStyle = DT_CENTER Or DT_TOP     or DT_WORDBREAK 
      Case LabelAlignment.TopLeft:      wsStyle = DT_LEFT   Or DT_TOP     or DT_WORDBREAK 
      Case LabelAlignment.TopRight:     wsStyle = DT_RIGHT  Or DT_TOP     or DT_WORDBREAK 
   End Select
   wsStyle = wsStyle or DT_EXPANDTABS
   Return wsStyle 
End Function
You then need to work with the WM_MEASUREITEM and WM_DRAWITEM messages at the Label's parent level (usually the Form window).
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to set the text alignment of a static control ?

Post by kcvinu »

PaulSquires wrote:In my program I implemented it as SS_OWNERDRAW in order to get the proper alignments. Here are the styles I used for each text position.
-------------------------
You then need to work with the WM_MEASUREITEM and WM_DRAWITEM messages at the Label's parent level (usually the Form window).
Thanks for the reply. So, it seems that using SS_OWNERDRAW is the best option. It gives us full control. But we need to draw the entire control.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to set the text alignment of a static control ?

Post by jj2007 »

kcvinu wrote:using SS_OWNERDRAW is the best option. It gives us full control. But we need to draw the entire control.
As mentioned earlier, you can also draw directly in the client area of the main window. Pseudocode:

Code: Select all

MakeBrush hBrush, LiteGrey
MakeFont hFont, Height:-16, "Arial Black"
Event Paint
  mov rc_.left, 200
  mov rc_.top, 200
  mov rc_.right, 360
  mov rc_.bottom, 250
  invoke SetBkMode, PtDC, TRANSPARENT
  invoke SelectObject, PtDC, hFont
  invoke SelectObject, PtDC, hBrush
  invoke RoundRect, PtDC, rc_.left, rc_.top, rc_.right, rc_.bottom, 19, 19
  sub rc_.right, 3
  sub rc_.bottom, 3
  invoke DrawText, PtDC, Chr$("Hello World"), 11, addr rc_, DT_BOTTOM or DT_RIGHT or DT_SINGLELINE
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: How to set the text alignment of a static control ?

Post by aurelVZAB »

...or you can add just one small child window and put your static control anywhere on that window
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to set the text alignment of a static control ?

Post by dodicat »

I would just use chr(10,10, . . .) to pull the text down.

Code: Select all


#include "windows.bi"
Function Set_Font (Font As String,Size As long,Bold As long,Italic As long,Underline As long,StrikeThru As long) As HFONT
    Dim As HDC hDC=GetDC(HWND_DESKTOP)
    Dim As long CyPixels=GetDeviceCaps(hDC,LOGPIXELSY)
    ReleaseDC(HWND_DESKTOP,hDC)
    Return CreateFont(0-(Size*CyPixels)/72,0,0,0,Bold,Italic,Underline,StrikeThru,ANSI_CHARSET _
    ,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,Font)
End Function

Dim As HFONT  ThisFont=Set_Font("Times new roman",34,0,0,0,0)
Dim As HFONT  smallFont=Set_Font("Times new roman",8,0,0,0,0)

var mainwindow=CreateWindowEx(0,"#32770","Main.",WS_OVERLAPPEDWINDOW or WS_VISIBLE,200,200,800,600,0,0,0,0)
var stat=createwindowex(0,"static",chr(10,10)+"Sunk a bit on bottom."  , WS_CHILD or WS_VISIBLE or SS_SUNKEN or SS_RIGHT ,10,0,270,50,MainWindow,0,0,0)
var stat2=createwindowex(0,"static",chr(10,10)+"With a border on bottom"  , WS_CHILD or WS_VISIBLE or WS_BORDER or SS_LEFT ,10,80,370,50,MainWindow,0,0,0)
var stat3=createwindowex(0,"static",chr(10,10)+"With a border in centre"  , WS_CHILD or WS_VISIBLE or WS_BORDER or SS_CENTER ,10,160,370,50,MainWindow,0,0,0)
var stat4=createwindowex(0,"static",chr(10,10)+"With a border"  , WS_CHILD or WS_VISIBLE or SS_CENTER or WS_BORDER,10,240,760,150,MainWindow,0,0,0)
var stat5=createwindowex(0,"static","https://docs.microsoft.com/en-us/windows/win32/controls/static-control-styles"  , WS_CHILD or WS_VISIBLE or SS_CENTER ,10,400,760,150,MainWindow,0,0,0)

SendMessage(stat4,WM_SETFONT,Cast(WPARAM,ThisFont),0)
SendMessage(stat5,WM_SETFONT,Cast(WPARAM,SmallFont),0)
dim as  MSG emsg
While GetMessage( @emsg, 0, 0, 0 )
    
    'TranslateMessage( @emsg )
    DispatchMessage( @emsg )
    
    Select Case emsg.hwnd
    Case mainwindow
        Select Case emsg.message
        Case 273 : PostQuitMessage(0)
        End Select
    End Select
    wend
 
I don't understand your op.
label.style = current_style or SS_LEFT or SS_CENTERIMAGE ' // for left-center align
...
...
Is it a library thing or a custom udt.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: How to set the text alignment of a static control ?

Post by PaulSquires »

dodicat wrote: I don't understand your op.
label.style = current_style or SS_LEFT or SS_CENTERIMAGE ' // for left-center align
...
...
Is it a library thing or a custom udt.
Yeah, it is just code from the underlying library of routines I wrote that work with the code generated by my WinFBE visual designer. I posted it here so that the op would know what SS_* style combinations to use in order to achieve the different positions within the static control. The other stuff in that code snippet would not be usable as it is written.
Post Reply