Solved

Frame caption alignment

Posted on 2003-10-22
6
382 Views
Last Modified: 2013-12-26
Is there anyway to have the caption of a Frame control centered automatically?
The align property is not available for this control

For my application the frame.caption changes constantly and is always aligned to the left.
Using spaces()  is not acurate because of the font.

0
Comment
Question by:kcmagicdata
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9604675
Hi!

The only way I could think of is to set the frame caption to "" (blank) and use a label to act as Frame caption. Note that Label has Allign and Autosize property which
makes it easy to 'manipulate'.

Here is the sample code.
On a empty Form put following objects:      three option buttons, one frame, one label (inside a frame), one textbox and one command button and then paste this code

--------------------------------------------------------------

Private Sub Command1_Click()
    Label1.Caption = Text1.Text                   'Update the caption
    If Option1.Value = True Then                  'Align left
        Label1.Left = 80
        Label1.Alignment = 0
    End If
    If Option2.Value = True Then                                        'Align right
        Label1.Left = Frame1.Width - Label1.Width - 80
        Label1.Alignment = 1
    End If
    If Option3.Value = True Then                                         'Center align
        Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
        Label1.Alignment = 2
    End If
End Sub

Private Sub Form_Load()
    Frame1.Top = 240                                        'Some positioning
    Frame1.Left = 240
    Frame1.Width = 2600
    Frame1.Height = 2600
    Frame1.Caption = ""

    Label1.Top = 0

    Option1.value = True
    Option1.Caption = "Left"
    Option2.Caption = "Right"
    Option3.Caption = "Center"

    If Option1.Value = True Then                                         'Align left
        Label1.Left = 80
        Label1.Alignment = 0
    End If
    If Option2.Value = True Then                                            'Align right
        Label1.Left = Frame1.Width - Label1.Width - 80
        Label1.Alignment = 1
    End If
    If Option3.Value = True Then                                                'Center align
        Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
        Label1.Alignment = 2
End If


End Sub

Private Sub Option1_Click()                                 'Align left
    Label1.Left = 80
    Label1.Alignment = 0
End Sub

Private Sub Option2_Click()                                              'Align right
    Label1.Left = Frame1.Width - Label1.Width - 80
    Label1.Alignment = 1
End Sub

Private Sub Option3_Click()                                                   'Center align
    Label1.Left = Frame1.Width / 2 - (Label1.Width / 2)
    Label1.Alignment = 2
End Sub
-------------------------------------------------------
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9604682
Don't forget to put Label1.Autosize property to True.   I forgot to include it to the code.
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 9604738
Try this example:

Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const BS_LEFT As Long = &H100
Private Const BS_RIGHT As Long = &H200
Private Const BS_CENTER As Long = &H300
Private Const BS_TOP As Long = &H400
Private Const BS_BOTTOM As Long = &H800
Private Const BS_VCENTER As Long = &HC00
Private Const BS_ALLSTYLES = BS_LEFT Or BS_RIGHT Or BS_CENTER Or BS_TOP Or BS_BOTTOM Or BS_VCENTER
Private Const GWL_STYLE& = (-16)
Public Enum bsHorizontalAlignments
    bsLeft = BS_LEFT
    bsRight = BS_RIGHT
    bsCenter = BS_CENTER
End Enum
Public Enum bsVerticalAlignments
    bsTop = BS_TOP
    bsBottom = BS_BOTTOM
    bsVCenter = BS_VCENTER
End Enum

Public Sub AlignTextCenter(fr As Frame, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)
    Dim oldStyle As Long
    ' get current style
    oldStyle = GetWindowLong(fr.hWnd, GWL_STYLE)
    ' clear existing alignment setting(s)
    oldStyle = oldStyle And (Not BS_ALLSTYLES)
    ' set new style and refresh
    Call SetWindowLong(fr.hWnd, GWL_STYLE, _
    oldStyle Or HStyle Or VStyle)
    fr.Refresh
End Sub

Private Sub Form_Load()
    AlignTextCenter Frame1, bsCenter, bsTop
End Sub

'Customized from the code from Richie_Simonetti at
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20177568.html

Hope this helps
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kcmagicdata
ID: 9604746
Sorry, im trying to avoid using another control to display the caption.
I need the frame controls caption to display the text
thanks for the suggestion
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 9604748
Even make it more fun by change this:

Public Sub AlignTextCenter(fr As Frame, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)

to:
   
Public Sub AlignTextCenter(fr As Object, Optional ByVal HStyle As bsHorizontalAlignments = bsCenter, Optional ByVal VStyle As bsVerticalAlignments = bsVCenter)

as you can try like:

AlignTextCenter Frame1, bsCenter, bsTop
    AlignTextCenter Command1, bsRight, bsBottom
    AlignTextCenter Check1, bsCenter, bsTop

cheers
0
 

Author Comment

by:kcmagicdata
ID: 9604763
thanks
you guys are a big help
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now