MSFlexGrid实现Ctrl+鼠标多选.虽然自己写了一个,但表格中行数较多时,效果不太好.请大家给个方案优化一下哈.
Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long, j As Long
If MSFlexGrid1.MouseRow = 0 Then Exit Sub
If Button = 1 Then
If Shift = 2 Then
With MSFlexGrid1
For j = 1 To .Cols - 1
.Col = j
If .CellBackColor = &H8000000D Then
.CellForeColor = &H80000008
.CellBackColor = &H80000005
.RowData(.Row) = 0
Else
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
.RowData(.Row) = 1
End If
Next
End With ElseIf Shift = 1 Then
With MSFlexGrid1
If .Row <= .RowSel Then
For i = .Row To .RowSel
.RowData(i) = 1
Next
Else
For i = .Row To .RowSel Step -1
.RowData(i) = 1
Next
End If
End With
End If
End If
End SubPrivate Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long, j As Long
Dim lngRow As Long, lngRowSel As Long
If MSFlexGrid1.MouseRow = 0 Then Exit Sub
DoEvents
If Shift = 0 Then
With MSFlexGrid1
lngRow = .Row
lngRowSel = .RowSel
For i = 1 To .Rows - 1
If i <> lngRowSel And .RowData(i) = 1 Then
.Row = i
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H80000008
.CellBackColor = &H80000005
Next
.RowData(i) = 0
End If
Next
.Row = lngRow
If .Row <= lngRowSel Then
For i = lngRow To lngRowSel
.Row = i
.RowData(i) = 1
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
Next
Next
Else
For i = lngRow To lngRowSel Step -1
.Row = i
.RowData(i) = 1
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
Next
Next
End If
End With
End If
End Sub
Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long, j As Long
If MSFlexGrid1.MouseRow = 0 Then Exit Sub
If Button = 1 Then
If Shift = 2 Then
With MSFlexGrid1
For j = 1 To .Cols - 1
.Col = j
If .CellBackColor = &H8000000D Then
.CellForeColor = &H80000008
.CellBackColor = &H80000005
.RowData(.Row) = 0
Else
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
.RowData(.Row) = 1
End If
Next
End With ElseIf Shift = 1 Then
With MSFlexGrid1
If .Row <= .RowSel Then
For i = .Row To .RowSel
.RowData(i) = 1
Next
Else
For i = .Row To .RowSel Step -1
.RowData(i) = 1
Next
End If
End With
End If
End If
End SubPrivate Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long, j As Long
Dim lngRow As Long, lngRowSel As Long
If MSFlexGrid1.MouseRow = 0 Then Exit Sub
DoEvents
If Shift = 0 Then
With MSFlexGrid1
lngRow = .Row
lngRowSel = .RowSel
For i = 1 To .Rows - 1
If i <> lngRowSel And .RowData(i) = 1 Then
.Row = i
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H80000008
.CellBackColor = &H80000005
Next
.RowData(i) = 0
End If
Next
.Row = lngRow
If .Row <= lngRowSel Then
For i = lngRow To lngRowSel
.Row = i
.RowData(i) = 1
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
Next
Next
Else
For i = lngRow To lngRowSel Step -1
.Row = i
.RowData(i) = 1
For j = 1 To .Cols - 1
.Col = j
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
Next
Next
End If
End With
End If
End Sub
其实主要不是闪不闪,而是效果不好,反应速度不够快.
Me.MSFlexGrid1.Rows = 500
Me.MSFlexGrid1.Cols = 200
Me.MSFlexGrid1.SelectionMode = flexSelectionFree
Me.MSFlexGrid1.FillStyle = flexFillRepeat
End SubPrivate Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long, j As Long
If MSFlexGrid1.MouseRow = 0 Then Exit Sub
If Button = 1 Then
If Shift = 2 Then
Dim nCol As Long, nColSel As Long
With MSFlexGrid1
nCol = .Col
nColSel = .ColSel
.Col = 1
.ColSel = .Cols - 1
If .CellBackColor = &H8000000D Then
.CellForeColor = &H80000008
.CellBackColor = &H80000005
Else
.CellForeColor = &H8000000E
.CellBackColor = &H8000000D
End If
.Col = nCol
.ColSel = nColSel
End With
ElseIf Shift = 1 Then
With MSFlexGrid1
If .Row <= .RowSel Then
For i = .Row To .RowSel
.RowData(i) = 1
Next
Else
For i = .Row To .RowSel Step -1
.RowData(i) = 1
Next
End If
End With
End If
End If
End Sub
MSFlexGrid1.row 是选中的行
ctrl 之后 选中的行 不停增多
.
.
.
MSFlexGrid1.Redraw = True
用Redraw属性基本上就没问题了还有就是既然使用RowData属性标记了选中行,就没必要把所有行都刷新一遍颜色
完全可以在Scroll事件中只把显示出来的几行刷新一下以前做过一个程序,需要对Msflexgrid隔行变色,就用了Scroll事件,很好用。
MouseUp主要就是处理按住Ctrl多选某些行之后 , 直接点击鼠标左键选某一行需要去掉表格中原已选中行的颜色.
加上redraw的效果比之前好多了,O(∩_∩)O谢谢;
我的列不多,之前没看出来效果,忘了CellBackColor,CellForeColor 可以直接用到选中"区域"上的