⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 listview的单元格编辑功能
💻 FRM
字号:
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   5355
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7455
   LinkTopic       =   "Form1"
   ScaleHeight     =   5355
   ScaleWidth      =   7455
   StartUpPosition =   3  'Windows Default
   Begin MSComctlLib.ImageList ImageList2 
      Left            =   480
      Top             =   3120
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      MaskColor       =   12632256
      _Version        =   393216
   End
   Begin VB.TextBox Text1 
      Height          =   495
      Left            =   120
      TabIndex        =   1
      Text            =   "Text1"
      Top             =   840
      Width           =   975
   End
   Begin MSComctlLib.ImageList ImageList1 
      Left            =   480
      Top             =   3840
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      MaskColor       =   12632256
      _Version        =   393216
   End
   Begin MSComctlLib.ListView ListView1 
      Height          =   3855
      Left            =   1800
      TabIndex        =   0
      Top             =   1200
      Width           =   3615
      _ExtentX        =   6376
      _ExtentY        =   6800
      View            =   3
      LabelWrap       =   -1  'True
      HideSelection   =   -1  'True
      FullRowSelect   =   -1  'True
      GridLines       =   -1  'True
      _Version        =   393217
      ForeColor       =   -2147483640
      BackColor       =   -2147483643
      BorderStyle     =   1
      Appearance      =   1
      NumItems        =   2
      BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         Text            =   "1"
         Object.Width           =   2540
      EndProperty
      BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
         SubItemIndex    =   1
         Text            =   "1"
         Object.Width           =   2540
      EndProperty
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private m_hwndLV     As Long         '   ListView1.hWnd
  Private m_hwndTB     As Long         '   TextBox1.hWnd
  Private m_iItem     As Long                     '   ListItem.Index   whose   SubItem   is   being   edited
  Private m_iSubItem     As Long         '   zero   based   index   of   ListView1.ListItems(m_iItem).SubItem   being   edited
  '
    
  Private Sub Form_Load()
      Dim i     As Long
      Dim item     As ListItem
        
  '     Text1.Appearance   =   ccFlat       '   ComctlLib   enum   value
      Text1.Visible = False
      m_hwndTB = Text1.hWnd
        
      '   Initialize   the   ImageLists
      With ImageList1
          .ImageHeight = 32
          .ImageWidth = 32
          .ListImages.Add Picture:=Icon
      End With
        
      With ImageList2
          .ImageHeight = 16
          .ImageWidth = 16
          .ListImages.Add Picture:=Icon
      End With
        
      '   Initialize   the   ListView
      With ListView1
  '         .LabelEdit   =   lvwManual
          .HideSelection = False
          .Icons = ImageList1
          .SmallIcons = ImageList2
          m_hwndLV = .hWnd
            
          For i = 1 To 4
              .ColumnHeaders.Add Text:="column" & i
          Next
            
          For i = 0 To &H3F
              Set item = .ListItems.Add(, , "item" & i, 1, 1)
              item.SubItems(1) = i * 10
              item.SubItems(2) = i * 100
              item.SubItems(3) = i * 1000
          Next
      End With
        
        
  End Sub
    
  Private Sub Form_Resize()
  '     ListView1.Move   0,   0,   ScaleWidth,   ScaleHeight
  End Sub
    
  Private Sub ListView1_DblClick()
      Dim lvhti     As LVHITTESTINFO
      Dim rc     As RECT
      Dim li     As ListItem
            
      '   If   a   left   button   double-click...   (change   to   suit)
      If (GetKeyState(vbKeyLButton) And &H8000) Then
        
          '   If   a   ListView   SubItem   is   double   clicked...
          Call GetCursorPos(lvhti.pt)
          Call ScreenToClient(m_hwndLV, lvhti.pt)
          If (ListView_SubItemHitTest(m_hwndLV, lvhti) <> LVI_NOITEM) Then
              If lvhti.iSubItem Then
                    
                  '   Get   the   SubItem's   label   (and   icon)   rect.
                  If ListView_GetSubItemRect(m_hwndLV, lvhti.iItem, lvhti.iSubItem, LVIR_LABEL, rc) Then
                        
                      '   Either   set   the   ListView   as   the   TextBox   parent   window   in   order   to
                      '   have   the   TextBox   Move   method   use   ListView   client   coords,   or   just
                      '   map   the   ListView   client   coords   to   the   TextBox's   paent   Form
      '                 Call   SetParent(m_hwndTB,   m_hwndLV)
                      Call MapWindowPoints(m_hwndLV, hWnd, rc, 2)
                      Text1.Move (rc.Left + 4) * Screen.TwipsPerPixelX, _
                                                              rc.Top * Screen.TwipsPerPixelY, _
                                                              (rc.Right - rc.Left) * Screen.TwipsPerPixelX, _
                                                              (rc.Bottom - rc.Top) * Screen.TwipsPerPixelY
                        
                      '   Save   the   one-based   index   of   the   ListItem   and   the   zero-based   index
                      '   of   the   SubItem(if   the   ListView   is   sorted   via   the     API,   then   ListItem.Index
                      '   will   be   different   than   lvhti.iItem   +1...)
                      m_iItem = lvhti.iItem + 1
                      m_iSubItem = lvhti.iSubItem
                        
                      '   Put   the   SubItem's   text   in   the   TextBox,   save   the   SubItem's   text,
                      '   and   clear   the   SubItem's   text.
                      Text1 = ListView1.ListItems(m_iItem).SubItems(m_iSubItem)
                      Text1.Tag = Text1
                      ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = ""
                        
                      '   Make   the   TextBox   the   topmost   Form   control,   make   the   it   visible,   select
                      '   its   text,   give   it   the   focus,   and   subclass   it.
                      Text1.ZOrder 0
                      Text1.Visible = True
                      Text1.SelStart = 0
                      Text1.SelLength = Len(Text1)
                      Text1.SetFocus
                      Call SubClass(m_hwndTB, AddressOf WndProc)
                        
                  End If         '   ListView_GetSubItemRect
              End If         '   lvhti.iSubItem
          End If         '   ListView_SubItemHitTest
      End If         '   GetKeyState(vbKeyLButton)
        
  End Sub
    
  '   Selects   the   ListItem   whose   SubItem   is   being   edited...
    
  Private Sub Text1_GotFocus()
      ListView1.ListItems(m_iItem).Selected = True
  End Sub
    
  '   If   the   TextBox   is   shown,   size   its   width   so   that   it's   always   a   little
  '   longer   than   the   length   of   its   Text.
    
  Private Sub Text1_Change()
      If m_iItem Then Text1.Width = TextWidth(Text1) + 180
  End Sub
    
  '   Update   the   SubItem   text   on   the   Enter   key,   cancel   on   the   Escape   Key.
    
  Private Sub Text1_KeyPress(KeyAscii As Integer)
        
      If (KeyAscii = vbKeyReturn) Then
          Call HideTextBox(True)
          KeyAscii = 0
      ElseIf (KeyAscii = vbKeyEscape) Then
          Call HideTextBox(False)
          KeyAscii = 0
      End If
    
  End Sub
    
  Friend Sub HideTextBox(fApplyChanges As Boolean)
        
      If fApplyChanges Then
          ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = Text1
      Else
          ListView1.ListItems(m_iItem).SubItems(m_iSubItem) = Text1.Tag
      End If
        
      Call UnSubClass(m_hwndTB)
      Text1.Visible = False
      Text1 = ""
  '     Call   SetParent(m_hwndTB,   hWnd)
  '     ListView1.SetFocus
      m_iItem = 0
        
  End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -