วันจันทร์ที่ 25 กรกฎาคม พ.ศ. 2554

mshflexgrid

***เทคนิค : ปรับความสูงของเซล ใน mshflexgrid อัตโนมัติ เพื่อให้แสดงข้อความในเซลได้หมด
***เป็น โค๊ดในส่วนหนึ่งของโปรแกรม FindEndPromise


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETLINECOUNT = &HBA

Public Sub AutoRowHeight()
Dim ColLoop As Long
         Dim RowLoop As Long

         'Turn off redrawing to avoid flickering
         MSHFlexGrid1.Redraw = False
   With Me.MSHFlexGrid1
       .RowHeight(1) = Me.TextHeight("d") * 1
       .RowHeight(2) = Me.TextHeight("d") * 1
       .RowHeight(3) = Me.TextHeight("d") * 1
       .RowHeight(4) = Me.TextHeight("d") * 1
       .RowHeight(5) = Me.TextHeight("d") * 1

   End With
      
            'MSHFlexGrid1.ColWidth(ColLoop) = 2500
           'ColLoop = 4
            For RowLoop = 1 To MSHFlexGrid1.Rows - 1
                       For ColLoop = 2 To 5
                            ReSizeCellHeight RowLoop, ColLoop
                       Next ColLoop
          Next RowLoop

         'Turn redrawing back on
         MSHFlexGrid1.Redraw = True
      End Sub

      Public Sub ReSizeCellHeight(MyRow As Long, MyCol As Long)
         Dim LinesOfText As Long
         Dim HeightOfLine As Long

         'Set MSFlexGrid to appropriate Cell
         MSHFlexGrid1.Row = MyRow
         MSHFlexGrid1.Col = MyCol

         'Set textbox width to match current width of selected cell
         Text1.Width = MSHFlexGrid1.ColWidth(MyCol)

         'Set font info of textbox to match FlexGrid control
         Text1.Font.Name = MSHFlexGrid1.Font.Name
         Text1.Font.Size = MSHFlexGrid1.Font.Size
         Text1.Font.Bold = MSHFlexGrid1.Font.Bold
         Text1.Font.Italic = MSHFlexGrid1.Font.Italic
         Text1.Font.Strikethrough = MSHFlexGrid1.Font.Strikethrough
         Text1.Font.Underline = MSHFlexGrid1.Font.Underline

         'Set font info of form to match FlexGrid control
         Me.Font.Name = MSHFlexGrid1.Font.Name
         Me.Font.Size = MSHFlexGrid1.Font.Size
         Me.Font.Bold = MSHFlexGrid1.Font.Bold
         Me.Font.Italic = MSHFlexGrid1.Font.Italic
         Me.Font.Strikethrough = MSHFlexGrid1.Font.Strikethrough
         Me.Font.Underline = MSHFlexGrid1.Font.Underline

         'Put the text from the selected cell into the textbox
         Text1.Text = Trim(MSHFlexGrid1.Text)

         'Get the height of the text in the textbox
         HeightOfLine = Me.TextHeight(Text1.Text)

         'Call API to determine how many lines of text are in text box
         LinesOfText = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, 0&)

         'Check to see if row is not tall enough
        If MSHFlexGrid1.RowHeight(MyRow) < (LinesOfText * HeightOfLine) Then
            'Adjust the RowHeight based on the number of lines in textbox
            MSHFlexGrid1.RowHeight(MyRow) = LinesOfText * HeightOfLine
       End If
End Sub

ไม่มีความคิดเห็น:

แสดงความคิดเห็น