The following code shows how you can trap out-of-memory errors and automatically switch to monochrome mode using the PictureType property. In addition, it shows how to create a picture within the MSHFlexGrid that includes only the current selection.
Note If you are using the MSFlexGrid, substitute "MSHFlexGrid1" with "MSFlexGrid1."
Sub CopySelectedPictureToClipboard (myFlex As _
MSHFlexGrid)
Dim i As Integer, tr As Long, lc As Long, _
hl As Integer
' Get ready to operate.
MyFlex.Redraw =False ' To eliminate flicker.
hl =MyFlex.HighLight ' Save current settings.
tr =MyFlex.TopRow
lc =MyFlex.LeftCol
MyFlex.HighLight =0 ' No highlight on picture.
' Hide nonselected rows and columns.
' (Save original sizes in RowData/ColData
' properties.)
For i =MyFlex.FixedRows To MyFlex.Rows - 1
If i < MyFlex.Row Or i > MyFlex.RowSel Then
MyFlex.RowData(i) =MyFlex.RowHeight(i)
MyFlex.RowHeight(i) =0
End If
Next
For i =MyFlex.FixedCols To MyFlex.Cols - 1
If i < MyFlex.Col Or i > MyFlex.ColSel Then
MyFlex.ColData(i) =MyFlex.ColWidth(i)
MyFlex.ColWidth(i) =0
End If
Next
' Scroll to top left corner.
MyFlex.TopRow =MyFlex.FixedRows
MyFlex.LeftCol =MyFlex.FixedCols
' Copy picture.
clipboard.Clear
On Error Resume Next
MyFlex.PictureType =0 ' Color.
clipboard.SetData MyFlex.Picture
If Error <> 0 Then
MyFlex.PictureType =1 ' Monochrome.
clipboard.SetData MyFlex.Picture
Endif
' Restore control.
For i =MyFlex.FixedRows To MyFlex.Rows - 1
If i < MyFlex.Row Or i > MyFlex.RowSel Then
MyFlex.RowHeight(i) =MyFlex.RowData(i)
End If
Next
For i =MyFlex.FixedCols To MyFlex.Cols - 1
If i < MyFlex.Col Or i > MyFlex.ColSel Then
MyFlex.ColWidth(i) =MyFlex.ColData(i)
End If
Next
MyFlex.TopRow =tr
MyFlex.LeftCol =lc
MyFlex.HighLight =hl
MyFlex.Redraw =True
End Sub
The following example shows how to set the MSHFlexGrid's Picture property to a PictureBox control:
Note If you are using the MSFlexGrid, substitute "MSHFlexGrid1" with "MSFlexGrid1."
Private Sub Form_Click ()
Set Picture1.Picture =MSHFlexGrid1.Picture
End Sub