回复 14# chzj589
你看下面的例子吧
意義是一樣的 想法是一樣的
只是 這個是 在LISTVIEW 中的
你點擊 其中任何一個 就可以看到變化
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiEdit.au3>
#include <FontConstants.au3>
#include <WinAPI.au3>
#include <ColorConstants.au3>
#include <GDIPlus.au3>
#include <GuiRichEdit.au3>
#include <ButtonConstants.au3>
Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0, $Index
Global $stRect = DllStructCreate("int;int;int;int")
Global $Start = True
Global $Clear = False
Global $tRect
Global $RichEdit
$Font = _WinAPI_CreateFont(15, 5, 0, 0, 400, False, False, False, Default, Default, Default, Default, 0, 'Arial')
$hGUI = GUICreate("ListView Subitems edit in place", 300, 210)
$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems", 2, 2, 296, 210)
_WinAPI_SetFont($hListView, $Font, True)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
;~ ControlDisable($hGUI, "", HWnd(_GUICtrlListView_GetHeader($hListView)))
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 2, $LVSCW_AUTOSIZE_USEHEADER)
For $i = 1 To 10
_GUICtrlListView_AddItem($hListView, "1000 " )
_GUICtrlListView_AddSubItem($hListView, $i - 1, "1000000 " , 1)
Next
$hButton = GUICtrlCreateButton("", 0, 0, 0, 0, BitOR($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
GUICtrlSetState($hButton, $GUI_HIDE)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hButton
Update_Text()
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMHDR, $hWndFrom, $iCode
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iCode = DllStructGetData($tNMHDR, 3)
Switch $hWndFrom
Case $hListView
Switch $iCode
Case $NM_CUSTOMDRAW
; If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $ilParam), $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $iColor1, $iColor2, $iColor3
$iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
Switch $iDrawStage
Case $CDDS_PREPAINT
Return $CDRF_NOTIFYITEMDRAW
Case $CDDS_ITEMPREPAINT
Return $CDRF_NOTIFYSUBITEMDRAW
Case $CDDS_ITEMPOSTPAINT
Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
$iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
$iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then
$hDC = _WinAPI_GetDC($hWndFrom)
$tRect = DllStructCreate($tagRECT)
If $iSubitem = 0 Then
_SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
DllStructSetData($tRect, 1, 2)
; DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) - 2)
_WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_CreateSolidBrush(0xFF9933));0XEAEAEA 灰色 0xFF9933 藍色
;DllStructSetData($tCustDraw, "clrText", 0xFFFFFF) ;高亮後文字改為白色
EndIf
DllStructSetData($tRect, 1, 2)
DllStructSetData($tRect, 2, $iSubitem)
_SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
_WinAPI_SelectObject($hDC, $Font)
_WinAPI_SetBkMode($hDC, $TRANSPARENT)
$GetColumnWidth = _GUICtrlListView_GetColumnWidth($hWndFrom, 0) / 2
$GetStringWidth = _GUICtrlListView_GetStringWidth($hWndFrom, $sText)
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1)+ $GetColumnWidth -($GetStringWidth/2)-2)
DllStructSetData($tRect, 2, DllStructGetData($tRect, 2) + 2)
;_WinAPI_SetTextColor($hDC, 0xFFFFFF) ;選中後改為白色字體
_WinAPI_DrawText($hDC, StringRegExpReplace($sText, "(?<=(?<!\.)\d)(?=(?:\d{3})+(?:\D|\Z))", ",") , $tRect, $DT_CENTER)
_WinAPI_ReleaseDC($hWndFrom, $hDC)
Return $CDRF_SKIPDEFAULT
EndIf
Return $CDRF_NEWFONT
Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
Case Else
EndSwitch
Case $LVN_COLUMNCLICK
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
$iCol = DllStructGetData($tInfo, "SubItem")
$bSortSense = False
_GUICtrlListView_SimpleSort($hListView, $bSortSense, $iCol, False)
;~ Case $NM_CLICK
;~ If $Clear=True Then
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) - 5)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) - 1)
;~ _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_CreateSolidBrush(0xFFFFFF))
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) + 5)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) + 1)
;~ _WinAPI_DrawText($hDC, $bText, $tRect,BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))
;~ EndIf
;~ $Info = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
;~ $Index = DllStructGetData($Info, "Index")
;~ $SubItem= DllStructGetData($Info, "SubItem")
;~ $Sub_Rect=_GUICtrlListView_GetSubItemRect($hListView,$Index,$SubItem)
;~ $hDC = _WinAPI_GetDC($hWndFrom)
;~ $tRect = DllStructCreate($tagRECT)
;~ DllStructSetData($tRect, 1, 2)
;~ DllStructSetData($tRect, 2, $SubItem)
;~ _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $Index, DllStructGetPtr($tRect))
;~ If $SubItem < 1 Then
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) - 3)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) - 2)
;~ DllStructSetData($tRect, 3, DllStructGetData($tRect, 3) - 1)
;~ DllStructSetData($tRect, 2, DllStructGetData($tRect, 2) + 1)
;~ ElseIf $SubItem > 0 Then
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) + 2)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) - 2)
;~ DllStructSetData($tRect, 3, DllStructGetData($tRect, 3) - 1)
;~ DllStructSetData($tRect, 2, DllStructGetData($tRect, 2) + 1)
;~ EndIf
;~ _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_CreateSolidBrush(0xD0D0D0))
;~ If $SubItem < 1 Then
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) + 5)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) + 1)
;~ ElseIf $SubItem > 0 Then
;~ DllStructSetData($tRect, 1, DllStructGetData($tRect, 1) + 4)
;~ DllStructSetData($tRect, 4, DllStructGetData($tRect, 4) + 1)
;~ EndIf
;~ Global $sText = _GUICtrlListView_GetItemText($hWndFrom, $Index, $SubItem)
;~ _WinAPI_SetBkMode($hDC, $TRANSPARENT)
;~ _WinAPI_SelectObject($hDC, _SendMessage($hWndFrom, $WM_GETFONT))
;~ _WinAPI_DrawText($hDC, $sText, $tRect,BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))
;~ Global $bText=$sText
;~ $Clear=True
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
$iCode = BitShift($wParam, 16)
Switch $lParam
Case $RichEdit
Switch $iCode
Case $EN_KILLFOCUS
Update_Text()
EndSwitch
EndSwitch
EndFunc ;==>WM_COMMAND
Func Update_Text()
$RichEdit_Text = _GUICtrlEdit_GetText($RichEdit)
_GUICtrlListView_SetItemText($hListView, $Index, StringStripWS($RichEdit_Text, 3), $SubItem)
_WinAPI_ReleaseDC($RichEdit, $hDC)
_WinAPI_DestroyWindow($RichEdit)
EndFunc ;==>Update_Text
|