ListBox 如何改变某行的字体颜色

Option Explicit

Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type DRAWITEMSTRUCT
        CtlType As Long     ‘控件类型
        CtlID As Long       ‘控件ID
        itemID As Long      ‘菜单项、列表框或组合框中某一项的索引值
        itemAction As Long  ‘控件行为
        itemState As Long   ‘控件状态
        hwndItem As Long    ‘父窗口句柄或菜单句柄
        hdc As Long         ‘控件对应的绘图设备句柄
        rcItem As RECT      ‘控件所占据的矩形区域
        itemData As Long    ‘列表框或组合框中某一项的值
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
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
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Private Const COLOR_HIGHLIGHT = 13
Private Const COLOR_HIGHLIGHTTEXT = 14
Private Const COLOR_WINDOW = 5
Private Const COLOR_WINDOWTEXT = 8
Private Const LB_GETTEXT = &H189
Private Const WM_DRAWITEM = &H2B
Private Const GWL_WNDPROC = (-4)
Private Const ODS_FOCUS = &H10
Private Const ODT_LISTBOX = 2

Private lPrevWndProc As Long

Private Function SubClassedList(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim tItem As DRAWITEMSTRUCT
    Dim sBuff As String * 255
    Dim sItem As String
    Dim lBack As Long
    If Msg = WM_DRAWITEM Then   ‘绘制菜单消息
    Call CopyMemory(tItem, ByVal lParam, Len(tItem))
        If tItem.CtlType = ODT_LISTBOX Then  ‘只处理控件类型为listbox的控件
        Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff) ‘获得具体值
             sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
            If (tItem.itemState And ODS_FOCUS) Then  ‘判断某项是否具有焦点
               lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
                Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
                Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
                TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
                DrawFocusRect tItem.hdc, tItem.rcItem
            Else  ‘如果没有焦点,则
                lBack = CreateSolidBrush(GetSysColor(COLOR_WINDOW))
                Call FillRect(tItem.hdc, tItem.rcItem, lBack)
                Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
                Call SetTextColor(tItem.hdc, tItem.itemData)
                TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
            End If
            Call DeleteObject(lBack)
            SubClassedList = 0
            Exit Function
                     End If
             End If
    SubClassedList = CallWindowProc(lPrevWndProc, hWnd, Msg, wParam, lParam)
End Function

Public Sub SubLists(ByVal hWnd As Long)
    lPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassedList)
End Sub

Public Sub RemoveSubLists(ByVal hWnd As Long)
    Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWndProc)
End Sub

‘窗体中 :

Private Sub Form_Load()
    Dim I As Integer
    For I = 0 To 15
        List1.AddItem "Color " & I
        List2.AddItem "FDSF"
    Next
    SubLists hWnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
    RemoveSubLists hWnd
End Sub

‘如果你想让list1的第五行的字体颜色为红色,则
    List1.itemData(4) = RGB(255, 0, 0)
    List1.Refresh

‘listbox的style设置为checkbox

sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
‘改成Trim(sBuff)即可显示全部中文。
Call SetBkColor(tItem.hdc, GetSysColor(COLOR_WINDOW))
Call SetTextColor(tItem.hdc, tItem.itemData)
‘这里是改背景和字体颜色的地方。
时间: 2024-11-03 18:44:24

ListBox 如何改变某行的字体颜色的相关文章

【JavaScript】轻易改变的背景和字体颜色页面

JavaScript,点击button改变页面背景和字体颜色,网页有N颜色的变化button.点击不同button,网页字体和背景将被改变为不同的颜色. 非常easy的JavaScript小程序. 一.基本目标 一打开网页首先提示问候信息"你好" 网页中有N个改变颜色的button,当中返回是返回网页的默认颜色,背景是白的,字体是黑的 点击不同的button.网页的字体与背景就会改变成不同的颜色. 本来想做出彩虹起色的,但原理全然一样就不多写button了. 二.基本思想 关键是对bo

在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色

原文:在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色 例如根据学生的年龄来修改,年龄小于18岁的,该行为红色显示,如何做到 解决方法 1: 在你需要加载的键入代码,我一般放在TabControl的selected事件中 ?Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, new Action(方法名)); ? 我个人定义了一个方法 ?for (i

修改linux终端命令行各字体颜色

最近在学习linux操作系统(CentOS 6 & CentOS 7).觉得linux终端命令行全部为白色,会经常导致命令与输出内容难以分辨.于是上网找到修改linux终端命令行颜色的方法,发现通过定义PS1环境变量即可实现,下面我以root用户身份进行操作. 1.了解PS1 PS1是Linux终端用户的一个环境变量,用来定义命令行提示符的参数. 在终端输入命令: # set | grep PS1 可得到当前PS1的定义值: PS1='[\[email protected]\h \W]\$ '

一个div里有多个a标签,改变a标签的字体颜色方法

<script type="text/javascript">var all=document.getElementById("big");var allDiv=all.getElementsByTagName("div"); for(var i=1;i<allDiv.length+1;i++){allDiv[i-1].onmouseover=function(){var alla=this.getElementsByTagNa

sdk改变ListCtrl某一行的字体颜色或者背景颜色

ListCtrl的字体颜色如果直接通过SetTextColour设置的话会改变所有行的字体颜色,sdk的对话框工程想要改变ListCtrl的某一行颜色需要注意一下几点: 1.在ListCtrl的父窗口响应WM_NOTIFY消息. 2.处理WM_NOTIFY的NM_CUSTOMDRAW消息,处理完之后通过SetWindowLong设置对话框返回结果. 3.处理完返回结果后对话框的回调函数要返回1. 具体代码如下: VOID OnNotify(HWND hdlg, WPARAM wp, LPARAM

mfc EDIT字体颜色

改变Edit字体颜色: 1.CMyDlg类中添加成员变量: CBrush m_Brush; 2.OnInitDialog中初进行设置: m_brush.CreateSolidBrush(RGB(0,255,255)); 3.添加消息响应函数:OnCtlColor if(nCtlColor == CTLCOLOR_EDIT) { if(pWnd->GetDlgCtrlID()== IDC_EDIT1) { pDC->SetTextColor(RGB(255,255,0)); //文字颜色 pDC

EXTJS根据值Value改变gridpanel单元格背景颜色或者设置整行字体颜色

第一步. 设置样式 .reportColor4{background: #93A9C1;}.reportColor5{background: #EEEEEE;} 第二步.修改单元格样式 header : '流通类型',width : 80,sortable : true,align : 'center',dataIndex: 'BR_TYPE',// css : 'background: #acdaf4;', // ----- 设置整个单元格的样式,但是不能根据条件变化renderer:func

android自定义控件实现TextView按下后字体颜色改变

今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片             第一张是按下后截的图,功能很简单,也很容易实现,下面来看一下如何通过重写TextView来实现 一共三个文件  TextViewM.java,MainActivity.java,activity_textview.xml TextViewM.java 1 package landptf.control; 2 3 import and

linux终端c语言改变输出字体颜色

Linux下C改变输出字体颜色 例: #include int main() { printf("\033[31mThis is RED.\n\033[0m"); return 0; } 31m代表字体为红色,0m代表关闭所有属性. 常用的ANSI控制码如下(有些不支持): \033[0m 关闭所有属性 \033[1m 高亮 \033[2m 亮度减半 \033[3m 斜体 \033[4m 下划线 \033[5m 闪烁 \033[6m 快闪 \033[7m 反显 \033[8m 消隐 \