Resize controls (labels or textboxes) during run-time 
Author Message
 Resize controls (labels or textboxes) during run-time

My program is like this:

There's 1 drawing panel (a picture box) and there's 1 label which once
i drag to the drawing panel, a window will appear for the user to type
in the texts they want and change font, font size, bold/italic, the
width and height of the labels containing those texts. Once the user
finishes and click ok, a label containing those texts with the
settings will be loaded onto the drawing panel. The user can move the
label on the drawing panel. If the user wants to change the settings/
texts of the label, they double click the label and the window
mentioned above appears.

My client requests us to improve it in which the user will be able to
double click on the labels and go on to type the texts onto the
labels. Once they click anywhere in the drawing panel which is not the
labels, the label's caption will change to the typed texts. The other
request is the user can resize the labels on run-time which once the
mouse is on top of the labels, the "indication to resize" will appear
on the corners of the labels. Then, the user can resize the label by
dragging from the "indication".

To do this, i have decided to change to use Textbox instead of label.
Using textbox, i'm able to the "type the texts" part but i'm
struggling with the "resize" part.



Sun, 06 Feb 2011 17:45:20 GMT  
 Resize controls (labels or textboxes) during run-time

Quote:
> The other request is the user can resize the labels on run-time
> which once the mouse is on top of the labels, the "indication to
> resize" will appear on the corners of the labels. Then, the user
> can resize the label by dragging from the "indication". To do this,
> i have decided to change to use Textbox instead of label. Using
> textbox, i'm able to the "type the texts" part but i'm struggling
> with the "resize" part.

You can display and deal with the sizing and moving handles in a number of
different ways, some with a remarkably small amount of code, but every
example I've so far seen has various problems under certain circumstances,
usually where on many occasions the "shape" of the resizing handle indicates
that you can perform a specific move or resize but you cannot actually do
so. This usually happens only at the extreme edges or corners, but it is
really annoying when it does so. The only way so far I've been able to
overcome these problems is by using subclassing, which complicates the code
somewhat. However, it does seem to work okay, both for Controls with borders
and for those without. I actually spent a bit of time on this one some time
ago, and I got it to a "just about working the way I want" when I got
sidetracked onto something else, and I haven't looked at it since ;-) You're
welcome to what I've done so far. It's so long ago now that I can't remember
how "finished" it was, but it seems to work okay and should be suitable for
your needs. In this specific example I used an array of PictureBoxes and an
array of RichTextBoxes (ten of each, all initially placed on top of each
other), and the code allows the user to click on any PictureBox or
RichTextBox and move it about the Form and resize it.

To try it out start a new VB project and place one RichTextBox and one
PictureBox on the Form. Set the Index value of both the PictureBox and the
RichTextBox to zero in the properties window. In my own test program I have
set the BorderStyle property of both of these Controls to none, so that they
have no borders, so perhaps you may wish to do the same, at least initially.
The code should work fine though with Controls that have borders, because I
seems to remember taking all that stuff into account. Then place two Image
Controls on the Form and set the Picture property of each of them to
whatever you wish. Also place a Timer Control on the Form. Then add a
standard code Module to the project. Then paste in the following two blocks
of code, one for the Module and one for the Form:

Mike

' *** START OF MODULE CODE ***
Option Explicit
Private Declare Function SetWindowsHookEx _
  Lib "user32" Alias "SetWindowsHookExA" _
  (ByVal idHook As Long, ByVal lpfn As Long, _
  ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx _
  Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx _
  Lib "user32" (ByVal hHook As Long, _
  ByVal nCode As Long, ByVal wParam As Long, _
  lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
  Alias "RtlMoveMemory" (Destination As Any, _
  Source As Any, ByVal Length As Long)
Private Declare Function WindowFromPoint _
  Lib "user32.dll" (ByVal xPoint As Long, _
  ByVal yPoint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" _
  (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ClientToScreen Lib "user32" _
  (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" _
  (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
  X As Long
  Y As Long
End Type
Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type
Private Type MOUSELLHOOKSTRUCT
  point As POINTAPI
  data As Long
  flags As Long
  time As Long
  extra As Long
End Type
Private mousedata As MOUSELLHOOKSTRUCT
Private Const HC_ACTION = 0
Private Const WH_MOUSE As Long = 7 ' app level hook
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_NCMOUSEMOVE As Long = &HA0
Private Hook As Long
Private HookedForm As Form
Public LastActiveRTB As Long
Public windowHandles(1 To 100, 1 To 3) As Long
Public totalWindows As Long

Public Sub SetFormHook(f1 As Form)
Set HookedForm = f1
Hook = SetWindowsHookEx(WH_MOUSE, _
    AddressOf MouseProc, App.hInstance, App.ThreadID)
End Sub

Public Sub ReleaseFormHook()
UnhookWindowsHookEx Hook
End Sub

Private Function MouseProc(ByVal nCode As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long
Dim n As Long, mouseWindow As Long
Dim r1 As RECT, p1 As POINTAPI, wide As Long, high As Long
If (nCode = HC_ACTION) Then
  Select Case wParam
    Case WM_NCMOUSEMOVE
      ' It is a mousemove in the non client area
      ' of a window (in other words, in its border).
      ' This event is not passed on to VB so it does
      ' not trigger a VB MouseMove event, so we need
      ' to use this subclassing routine to change the
      ' pointer of the active RTB to the same as the
      ' Form (vbArrow) to signify that the window cannot
      ' be dragged using our code when the pointer is on
      ' the RTB border (because mouse moves and clicks in
      ' a border do not generate a VB mousemove or
      ' mouseclick event)
      CopyMemory mousedata, ByVal lParam, Len(mousedata)
      mouseWindow = WindowFromPoint _
        (mousedata.point.X, mousedata.point.Y)
      For n = 1 To totalWindows
        If windowHandles(n, 1) = mouseWindow Then Exit For
      Next n
      If n <= totalWindows Then
        ' we have found the window handle in our list
        If windowHandles(n, 2) = 1 Then ' it is a RTB
          HookedForm.RichTextBox1 _
            (windowHandles(n, 3)).MousePointer = vbArrow
        End If
        If windowHandles(n, 2) = 2 Then ' it is a PicBox
          HookedForm.Picture1 _
            (windowHandles(n, 3)).MousePointer = vbArrow
        End If
      End If
    Case WM_LBUTTONUP
      ' It is a left button up event. We don't know what
      ' the user was doing when he let go of the left mouse
      ' button but just in case he was dragging out a
      ' RichTextBox to a new size we will "tell VB" what the
      ' new size is because VB doesn't seem to update its
      ' own Width or Height properties of a RTB while the user
      ' is dragging it using the method we are using.
      ' (VB updates the Height and Width of other controls
      ' when dragging in this way, such as a Picture Box and
      ' a standard TextBox, but it does not do so for a RTB
      ' so we need to perform that task ourselves in code).
      If LastActiveRTB > 0 Then
        ' find the location of the client area of our VB Form
        ' relative to the top left corner of the screen (the
        ' result appears in the p1 POINTAPI type)
        ClientToScreen HookedForm.hwnd, p1
        ' find the location of the RTB relative to the screen
        ' and also its size (result appears in the r1 RECT type)
        GetWindowRect HookedForm.RichTextBox1(LastActiveRTB).hwnd, r1
        ' size the RTB accordingly
        wide = r1.Right - r1.Left ' the width of the RTB
        high = r1.Bottom - r1.Top ' the height of the RTB
        ' make sure VB has the same size data as the OS
        ' only set RTB's Width and Height if different
        If HookedForm.RichTextBox1(LastActiveRTB).Width <> wide _
            Or HookedForm.RichTextBox1(LastActiveRTB).Height <> high Then
          HookedForm.RichTextBox1(LastActiveRTB).Move _
            r1.Left - p1.X, r1.Top - p1.Y, wide, high
        End If
      End If
    Case WM_MOUSEMOVE
      If LastActiveRTB > 0 Then
        ' find the location of the client area of our VB Form
        ' relative to the top left corner of the screen (the
        ' result appears in the p1 POINTAPI type)
        ClientToScreen HookedForm.hwnd, p1
        ' find the location of the RTB relative to the screen
        ' and also its size (result appears in the r1 RECT type)
        GetWindowRect HookedForm.RichTextBox1(LastActiveRTB).hwnd, r1
        ' size the RTB accordingly
        wide = r1.Right - r1.Left ' the width of the RTB
        high = r1.Bottom - r1.Top ' the height of the RTB
        ' only set RTB's Width and Height if different
        If wide < 18 Then
          HookedForm.RichTextBox1(LastActiveRTB).Width = 18
        End If
        If high < 18 Then
          HookedForm.RichTextBox1(LastActiveRTB).Height = 18
        End If
      End If
    Case Else
      '
  End Select
End If
' our job is done so allow the system to carry on as normal
MouseProc = CallNextHookEx _
    (0, nCode, wParam, ByVal lParam)
End Function
' *** END OF MODULE CODE ***
'
' *** START OF FORM CODE ***
Option Explicit
Private Declare Function SendMessage Lib "user32" _
  Alias "SendMessageA" (ByVal hwnd As Long, _
  ByVal wMsg As Long, ByVal wParam As Integer, _
  ByVal lParam As Any) As Long
Private Declare Function ReleaseCapture _
  Lib "user32" () As Long
Private Const HTLEFT = 10
Private Const HTRIGHT = 11
Private Const HTTOP = 12
Private Const HTBOTTOM = 15
Private Const HTBOTTOMLEFT = 16
Private Const HTBOTTOMRIGHT = 17
Private Const HTTOPLEFT = 13
Private Const HTTOPRIGHT = 14
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1
Private pic(1 To 100) As StdPicture

Private Sub Form_Load()
Dim n As Long
Randomize Timer
Me.ScaleMode = vbPixels
Me.WindowState = vbMaximized
Picture1(0).ScaleMode = vbPixels
RichTextBox1(0).Text = "Anything will do just for " _
& "test purposes because the user can enter whatever " _
& "he wishes at run time (or in the IDE of course)"
For n = 1 To 10
  Load RichTextBox1(n)
  totalWindows = totalWindows + 1
  windowHandles(totalWindows, 1) = RichTextBox1(n).hwnd
  windowHandles(totalWindows, 2) = 1 ' to signify
...

read more »



Sun, 06 Feb 2011 19:48:30 GMT  
 Resize controls (labels or textboxes) during run-time


Quote:
> My program is like this:

> There's 1 drawing panel (a picture box) and there's 1 label which once
> i drag to the drawing panel, a window will appear for the user to type
> in the texts they want and change font, font size, bold/italic, the
> width and height of the labels containing those texts. Once the user
> finishes and click ok, a label containing those texts with the
> settings will be loaded onto the drawing panel. The user can move the
> label on the drawing panel. If the user wants to change the settings/
> texts of the label, they double click the label and the window
> mentioned above appears.

> My client requests us to improve it in which the user will be able to
> double click on the labels and go on to type the texts onto the
> labels. Once they click anywhere in the drawing panel which is not the
> labels, the label's caption will change to the typed texts. The other
> request is the user can resize the labels on run-time which once the
> mouse is on top of the labels, the "indication to resize" will appear
> on the corners of the labels. Then, the user can resize the label by
> dragging from the "indication".

> To do this, i have decided to change to use Textbox instead of label.
> Using textbox, i'm able to the "type the texts" part but i'm
> struggling with the "resize" part.

Uh....you might try explaining the part you're struggling with in detail. We
don't need to know the background and that's most of what your post was.

What we DO need to know is what you really need help with, and given that
you posted at 5:45 AM EST and now I'm replying at ~7:00 PM EST and you've
had NO replies yet, I'm guessing the problem you're having is not well
defined. <g>

--
Mike
Microsoft MVP Visual Basic



Mon, 07 Feb 2011 07:10:46 GMT  
 Resize controls (labels or textboxes) during run-time

Quote:

>The other
>> request is the user can resize the labels on run-time which once the
>> mouse is on top of the labels, the "indication to resize" will appear
>> on the corners of the labels. Then, the user can resize the label by
>> dragging from the "indication".

Check out Form Designer at this link.
http://www.softcircuits.com/sw_vbsrc.aspx

-mhd



Mon, 07 Feb 2011 08:35:53 GMT  
 Resize controls (labels or textboxes) during run-time

. . . by the way, I forgot to mention, if you want the RichTextBox to be
borderless then you'll also need to set its Appearance property to Flat.

Mike



Mon, 07 Feb 2011 10:21:47 GMT  
 Resize controls (labels or textboxes) during run-time
Wow! Both of the suggestions posted above worked for me.  Thanks,
Quote:

> My program is like this:

> There's 1 drawing panel (a picture box) and there's 1 label which once
> i drag to the drawing panel, a window will appear for the user to type
> in the texts they want and change font, font size, bold/italic, the
> width and height of the labels containing those texts. Once the user
> finishes and click ok, a label containing those texts with the
> settings will be loaded onto the drawing panel. The user can move the
> label on the drawing panel. If the user wants to change the settings/
> texts of the label, they double click the label and the window
> mentioned above appears.

> My client requests us to improve it in which the user will be able to
> double click on the labels and go on to type the texts onto the
> labels. Once they click anywhere in the drawing panel which is not the
> labels, the label's caption will change to the typed texts. The other
> request is the user can resize the labels on run-time which once the
> mouse is on top of the labels, the "indication to resize" will appear
> on the corners of the labels. Then, the user can resize the label by
> dragging from the "indication".

> To do this, i have decided to change to use Textbox instead of label.
> Using textbox, i'm able to the "type the texts" part but i'm
> struggling with the "resize" part.



Mon, 07 Feb 2011 10:52:01 GMT  
 Resize controls (labels or textboxes) during run-time

Quote:
> My program is like this:

> There's 1 drawing panel (a picture box) and there's 1 label which once
> i drag to the drawing panel, a window will appear for the user to type
> in the texts they want and change font, font size, bold/italic, the
> width and height of the labels containing those texts. Once the user
> finishes and click ok, a label containing those texts with the
> settings will be loaded onto the drawing panel. The user can move the
> label on the drawing panel. If the user wants to change the settings/
> texts of the label, they double click the label and the window
> mentioned above appears.

> My client requests us to improve it in which the user will be able to
> double click on the labels and go on to type the texts onto the
> labels. Once they click anywhere in the drawing panel which is not the
> labels, the label's caption will change to the typed texts. The other
> request is the user can resize the labels on run-time which once the
> mouse is on top of the labels, the "indication to resize" will appear
> on the corners of the labels. Then, the user can resize the label by
> dragging from the "indication".

> To do this, i have decided to change to use Textbox instead of label.
> Using textbox, i'm able to the "type the texts" part but i'm
> struggling with the "resize" part.

If you are open to a new approach I think I can help make
your life easy and your client really happy.

Your full set of needs ( and a lot more ) can be very easily met
by replacing the standard picturebox with our MetaDraw control.

If this is within your project options then the code is very very
simple
and MetaDraw will take care of most of the work for you.

Here is sample code to meet all the needs you described
to allow user to add text, select it,  resize it, move it,
and edit it, Set the Font, FontSize, FontBold :

    ' Put the following on a form
    '       Metadraw - will manage the canvas holding the text
    '       Button - cmd_ModeAddText - user clicks this to
    '                          allow him to click in MetaDraw and
start
    '                          typing.
    '       Button - cmd_ModeSelect - user clicks this to
    '                          allow him to click in MetaDraw
    '                          to select, move, resize text objects
    '                          or double click them to edit
    '       txt_FontName  - user enters desired font name here
    '       txt_FontSize  - user enters desired font size
here
    '       Button cmd_SetFontName  - user clicks to set Font Name
    '       Button cmd_SetFontSize  - user clicks to set Font Size
    '       Checkbox chk_FontBold  - user clicks to toggle bold
    '       Checkbox chk_FontItalic  - user clicks to toggle italics

            Sub cmd_ModeAddText_Click()
                ' - Put user in edit mode to click on canvas
                ' - and then type text at given location
                MetaDraw1.EditMode = ED_TEXT
            End Sub

             Sub cmd_ModeSelect_Click()
                ' - Put user in edit mode to select object
                '  - and then drag, resize
                ' - or doubleclick to do in-place text edit
                MetaDraw1.EditMode = ED_SELECT
            End Sub

            Sub cmd_SetFontSize_Click()
                '  - Set FONT NAME of selected object
                If MetaDraw1.ObjCount(OBJ_SELECTED) <> 0 Then
                    MetaDraw1.ObjMove = MOVE_SEL_FIRST
                    MetaDraw1.FontSize = Val(txt_FontSize.Text)
                End If
            End Sub

            Sub txt_SetFontName_Click()
                '  - Set FONT NAME of selected object
                If MetaDraw1.ObjCount(OBJ_SELECTED) <> 0 Then
                    MetaDraw1.ObjMove = MOVE_SEL_FIRST
                    MetaDraw1.FontName = txt_FontName.Text
                End If
            End Sub

            Sub chk_FontBold_Click()
                '  - Toggle BOLD status of selected object
                If MetaDraw1.ObjCount(OBJ_SELECTED) <> 0 Then
                    MetaDraw1.ObjMove = MOVE_SEL_FIRST
                    MetaDraw1.FontBold = chk_FontBold.Value
                End If
            End Sub

            Sub chk_FontItalic_Click()
                '  - Toggle ITALIC status of selected object
                If MetaDraw1.ObjCount(OBJ_SELECTED) <> 0 Then
                    MetaDraw1.ObjMove = MOVE_SEL_FIRST
                    MetaDraw1.FontItalic = chk_FontItalic.Value
                End If
            End Sub

             Sub MetaDraw1_Change(ByVal ChangeType As Integer)
                '  - Update the textboxes and checkboxes to show the
                '  - font of the selected text object
                If ChangeType = CHG_OBJECTADDED Then
                   MetaDraw1.ObjSelected = True
                End If
                If ChangeType = CHG_SELECTION    _
                             Or ChangeType = CHG_OBJECTADDED   _
                             Or ChangeType = CHG_OBJECTRESIZE   _
                             Then
                    If MetaDraw1.ObjCount(OBJ_SELECTED) <> 0 Then
                        txt_FontSize.Text = MetaDraw1.FontSize
                        txt_FontName.Text = MetaDraw1.FontName
                        chk_FontBold.Value = -1 * MetaDraw1.FontBold
                        chk_FontItalic.Value = -1 *
MetaDraw1.FontItalic
                    End If
                End If
            End Sub

You can download MetaDraw from the www.Bennet-Tec.com
web site for evaluation and to try out the above code.

MetaDraw will allow you to

       - Add Text with in-place editing
          ( you do not need separate textbox or label controls )

       - Select text objects, drag, resize, even rotate the text
         Your user will see "resize" handles for selected object
         to click and drag for resizing

       - of course change the text font /size/ bold/italic, etc

       - also allow adding images, shapes, freehand drawing

       - scroll

       - zoom

        - print at high resolution
            ( much better than VB PrintPicture or PrintForm )

         - Save and Reload - and continue to recognize
            text objects as independent entities

         - Export images in variety of formats
                       BMP, PNG, GIF, JPG
                       WMF,  EMF

I hope this is helpful to you.
* * Please include a copy of this message with any direct reply on
this topic

        Jeff Bennett

        *    Bennet-Tec Information Systems, Inc
        *    50 Jericho Tpk, Jericho, NY 11753
        *    Phone 516 997 5596,  Fax - 5597
        *    WWW.Bennet-Tec.Com

        RELIABLE Component Software
        and Custom Software Development Services

        *    Expert Systems     *   Text Processing
        *    Databases       *  Interactive Web Sites
        *    Diagramming,  Drawing,  Hotspot Graphics
        *    Data Input & Data Presentation Systems
        *    Desktop Windows,  Tablets,   Pocket PCs

             TList?   /   ALLText?  /  MetaDraw?  /  Web Signature?

=================================



Tue, 08 Feb 2011 02:38:22 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Resizing controls during run-time?

2. Need 2 learn 2 resize images during run time

3. Req: Resize a textbox/label at runtime???

4. putting a label on a form during run time

5. changing label on datareport during run-time

6. Adding TextBoxes during Run-time

7. How to create labels or Textboxes in Run-Time

8. Resize textbox in Run-time

9. how can I create Label or ListBox or TextBox at VB5 as run time

10. How to create labels or Textboxes in Run-Time

11. sstab control - resizing during runtime problem

12. Controlling Controls During Run-Time

 

 
Powered by phpBB® Forum Software