Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Frame caption alignment

Posted on 2003-10-22
6
Medium Priority
?
514 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 55

Accepted Solution

by:
Ryan Chong earned 2000 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
Independent Software Vendors: 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!

 

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 55

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

571 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