Results 1 to 2 of 2

Thread: How to simuilate moving border when copying a cell using a grid, like excel does.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Location
    Florida, USA
    Posts
    93

    How to simuilate moving border when copying a cell using a grid, like excel does.

    I'm using VSFlexGrid, and I have an option to copy and paste cells, I want to simulate what excel does, when choosing copy in excel it creates a moving border around the cell....how do I do that in VB?.....the grid does not support that....HELP

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: How to simuilate moving border when copying a cell using a grid, like excel does.

    You can achieve a similar effect if you want to do a little work...

    In a Form with a MSFlexGrid and Timer control:
    VB Code:
    1. Option Explicit
    2.  
    3. ' Win32 API Declarations:
    4. Private Type RECT
    5.   Left As Long
    6.   Top As Long
    7.   Right As Long
    8.   Bottom As Long
    9. End Type
    10.  
    11. Private Type POINTAPI
    12.   x As Long
    13.   y As Long
    14. End Type
    15.  
    16. Private Const R2_XORPEN = 7
    17. Private Const PS_DOT = 2
    18. Private Const PS_DASHDOT = 3
    19.  
    20. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    21. Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    22. Private Declare Function SetROP2 Lib "gdi32" (ByVal hdc As Long, ByVal nDrawMode As Long) As Long
    23. Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
    24. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    25. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    26. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    27. Private Declare Function SelectClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long) As Long
    28. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    29. Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    30.  
    31. ' Custom structure to hold information about a user's selection
    32. Private Type SelectionType
    33.   Col As Long               ' ColumnIndex of first selected column
    34.   Row As Long               ' RowIndex of first selected row
    35.   Columns As Long           ' Number of Columns in the selection
    36.   Rows As Long              ' Number of Rows in the selection
    37.   AnimationIndex As Long    ' Arbitrary value to indicate some kind of animation index
    38.   Active As Boolean         ' Is the selection currently active
    39. End Type
    40.  
    41. ' Current selection information
    42. Private m_selection As SelectionType
    43.  
    44. Private Sub Form_Load()
    45.   ' Configure the FlexGrid and Timer, then clear the selection variable
    46.   With MSFlexGrid1
    47.     .Cols = 20
    48.     .Rows = 20
    49.     .FixedCols = 1
    50.     .FixedRows = 1
    51.     .BackColorSel = vbHighlight
    52.     .FocusRect = flexFocusHeavy
    53.   End With
    54.   Timer1.Interval = 200
    55.   ClearSelection
    56. End Sub
    57.  
    58. Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
    59.   ' Capture a user's keypress in the FlexGrid
    60.   Select Case KeyCode
    61.   Case vbKeyC And ((Shift And vbCtrlMask) = vbCtrlMask)
    62.       ' Select a cell/cells using CTRL+C
    63.       SelectCurrentCells
    64.    
    65.   Case vbKeyUp, vbKeyDown, vbKeyLeft, vbKeyRight, _
    66.        vbKeyMenu, vbKeyControl, vbKeyShift, vbKeyCapital, vbKeyTab, _
    67.        vbKeyPageUp, vbKeyPageDown, vbKeyEnd, vbKeyHome
    68.        
    69.     ' Do nothing special when using navigational/special keys
    70.    
    71.   Case Else
    72.     ' Any other key clears the current selection
    73.     ClearSelection
    74.    
    75.   End Select
    76. End Sub
    77.  
    78. Private Sub SelectCurrentCells()
    79.   ' Mark the currently highlighted cells as "Selected"
    80.   With MSFlexGrid1
    81.     ' Get the Left-most Column in the selection
    82.     m_selection.Col = IIf(.Col < .ColSel, .Col, .ColSel)
    83.     ' Calculate the number of columns in the selection
    84.     m_selection.Columns = IIf(.Col > .ColSel, .Col, .ColSel) - m_selection.Col + 1
    85.     ' Get the Top-most Row in the selection
    86.     m_selection.Row = IIf(.Row < .RowSel, .Row, .RowSel)
    87.     ' Calculate the number of rows in the sleection
    88.     m_selection.Rows = IIf(.Row > .RowSel, .Row, .RowSel) - m_selection.Row + 1
    89.     ' Activate the selection - which will cause the Timer to draw/animate it
    90.     m_selection.Active = True
    91.   End With
    92. End Sub
    93.  
    94. Private Sub ClearSelection()
    95.   ' Deactivate the current selection - stops the timer drawing/animating it
    96.   m_selection.Active = False
    97.   ' Refresh the grid to remove any remaining image
    98.   MSFlexGrid1.Refresh
    99. End Sub
    100.  
    101. Private Sub DrawSelectionRect(ByRef selection As SelectionType)
    102.   ' Draw the current selection rectangle
    103.   Dim tRECT As RECT
    104.   Dim lDC As Long
    105.   Dim lPen As Long
    106.   Dim lOldPen As Long
    107.   Dim tPoints(1 To 5) As POINTAPI
    108.   Dim lOldMixMode As Long
    109.   Dim lIndex As Long
    110.   Dim lRegion As Long
    111.    
    112.   With MSFlexGrid1
    113.     ' Redraw the grid
    114.     .Refresh
    115.     ' Get the Device Context of the Grid
    116.     lDC = GetWindowDC(.hwnd)
    117.    
    118.     ' Get the client rectangle of the FlexGrid
    119.     Call GetClientRect(.hwnd, tRECT)
    120.    
    121.     ' Now push in the Top and Left sides to exclude any Fixed Rows/Columns
    122.     ' So that when we draw the selection box, it won't draw over anything
    123.     ' it shouldn't
    124.     For lIndex = 0 To .FixedCols - 1
    125.       tRECT.Left = tRECT.Left + (.ColWidth(lIndex) / Screen.TwipsPerPixelX)
    126.     Next
    127.     For lIndex = 0 To .FixedRows - 1
    128.       tRECT.Top = tRECT.Top + (.RowHeight(lIndex) / Screen.TwipsPerPixelY)
    129.     Next
    130.     ' Create a Region using this new Client Rect
    131.     lRegion = CreateRectRgn(tRECT.Left + 2, tRECT.Top + 2, tRECT.Right + 2, tRECT.Bottom + 2)
    132.     ' Set the new region as the Clipping Region (anything outside the region won't be drawn)
    133.     Call SelectClipRgn(lDC, lRegion)
    134.    
    135.     ' Create a Rectangle using the Coord's of the selected cells in the grid
    136.     tRECT.Left = _
    137.       (.ColPos(selection.Col) / Screen.TwipsPerPixelX) + 2
    138.      
    139.     tRECT.Top = _
    140.       (.RowPos(selection.Row) / Screen.TwipsPerPixelY) + 2
    141.      
    142.     tRECT.Right = _
    143.       ((.ColPos(selection.Col + selection.Columns - 1) + _
    144.       .ColWidth(selection.Col + selection.Columns - 1)) / _
    145.       Screen.TwipsPerPixelX) + 2
    146.      
    147.     tRECT.Bottom = _
    148.       ((.RowPos(selection.Row + selection.Rows - 1) + _
    149.       .RowHeight(selection.Row + selection.Rows - 1)) / _
    150.       Screen.TwipsPerPixelY) + 2
    151.        
    152.     ' Get the current mix mode of the device context,
    153.     ' while changing it to XOR, so that anything we draw
    154.     ' is XOR'd with the underlying pixels
    155.     lOldMixMode = SetROP2(lDC, R2_XORPEN)
    156.    
    157.     ' Convert the rectangle into an array of POINT structures
    158.     ' So that we can draw an outline of the rectangle
    159.     tPoints(1).x = tRECT.Left
    160.     tPoints(1).y = tRECT.Top
    161.     tPoints(2).x = tRECT.Right
    162.     tPoints(2).y = tRECT.Top
    163.     tPoints(3).x = tRECT.Right
    164.     tPoints(3).y = tRECT.Bottom
    165.     tPoints(4).x = tRECT.Left
    166.     tPoints(4).y = tRECT.Bottom
    167.     tPoints(5).x = tRECT.Left
    168.     tPoints(5).y = tRECT.Top
    169.    
    170.     ' Perform some rudamentry animation...
    171.     Select Case selection.AnimationIndex
    172.     Case 0
    173.       ' If the Index is Zero, use a DOT pen (......)
    174.       lPen = CreatePen(PS_DOT, 1, vbBlack)
    175.      
    176.     Case Else
    177.       ' If the Index is One, use a DOT/DASH pen (._._._._.)
    178.       lPen = CreatePen(PS_DASHDOT, 1, vbBlack)
    179.      
    180.     End Select
    181.    
    182.     ' Create the pen, storing the old one
    183.     lOldPen = SelectObject(lDC, lPen)
    184.    
    185.     ' Draw the rectangle outline
    186.     Polyline lDC, tPoints(1), 5
    187.    
    188.     ' Reset the clipping region
    189.     Call SelectClipRgn(lDC, 0)
    190.     DeleteObject lRegion
    191.    
    192.     ' Restore the old mix-mode
    193.     Call SetROP2(lDC, lOldMixMode)
    194.    
    195.     ' Restore the old pen
    196.     SelectObject lDC, lOldPen
    197.     ' Delete the pen we created
    198.     DeleteObject lPen
    199.        
    200.     ' Release the Device Context handle we obtained
    201.     ReleaseDC .hwnd, lDC
    202.   End With
    203. End Sub
    204.  
    205. Private Sub Timer1_Timer()
    206.   ' Animate the selection...
    207.   Static bOn As Boolean
    208.   ' Only animate the selection if it's active
    209.   If m_selection.Active Then
    210.     ' Toggle the "bOn" flag
    211.     bOn = Not bOn
    212.     ' If "On", set the AnimationIndex to 1 (DOT/DASH pen)
    213.     ' Otherwise, set the AnimationIndex to 0 (DOT pen)
    214.     m_selection.AnimationIndex = IIf(bOn, 1, 0)
    215.     ' Draw the selection on the FlexGrid
    216.     DrawSelectionRect m_selection
    217.   End If
    218. End Sub
    Regards,

    - Aaron.

Posting Permissions

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



Click Here to Expand Forum to Full Width