VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1222|回复: 10

有人用过MfcGridCtrl吗,若想在一个cell里面,不同的字符用不同的颜色,有什么方法?

[复制链接]

2

主题

18

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
发表于 2020-2-14 22:00:02 | 显示全部楼层 |阅读模式
MfcGridCtrl据说是最好的grid
回复

使用道具 举报

0

主题

11

帖子

11.00

积分

新手上路

Rank: 1

积分
11.00
发表于 2020-6-14 13:45:01 | 显示全部楼层
要实现你的要求必须修改gridctrl里面的设置文字函数
必须另外加一个参数 用来将你要的字体颜色传递过去 估计修改起来会比较麻烦
你必须对gridctrl类很熟悉
回复

使用道具 举报

0

主题

11

帖子

11.00

积分

新手上路

Rank: 1

积分
11.00
发表于 2020-8-4 14:45:02 | 显示全部楼层
我觉得的确修改起来比较麻烦
本身GridCtrl类就很烦琐 要实现你说的功能 不是一句两句能说清的
所以大家都不好回答吧
我想还不如自己写个CWnd 画网格 每个Cell里填充CRichEdit
你可以上codeproject下载这个MultilineListControl看看
回复

使用道具 举报

2

主题

18

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
 楼主| 发表于 2020-8-5 14:45:01 | 显示全部楼层
楼上说的对,但是每个cell里填充RichEdit会不会很占用资源阿。
我去看看你说的MultilineListControl
回复

使用道具 举报

2

主题

18

帖子

13.00

积分

新手上路

Rank: 1

积分
13.00
发表于 2020-8-6 09:15:01 | 显示全部楼层
楼上说的对.

你为什么不在listctrl的基础重绘呢?
那样资源又低.
回复

使用道具 举报

0

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-8-6 23:15:01 | 显示全部楼层
BOOL CGridCellBase::Draw(CDC* pDC, int nRow, int nCol, CRect rect,  BOOL bEraseBkgnd /*=TRUE*/)
{
.....
  //pDC->SetTextColor();修改字符的颜色,在这加你的代码
   DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);//文本输出

    pDC->RestoreDC(nSavedDC);

    return TRUE;
}
回复

使用道具 举报

2

主题

18

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
 楼主| 发表于 2020-8-7 10:45:01 | 显示全部楼层
楼上看来是用过MFCGridCtrl这个控件,但是你那个方法只是修改了整个Cell的字体颜色。
我想在一个cell(多行显示)里不同的字符(比如搜索到的字符用红色)想用不同的颜色。
回复

使用道具 举报

2

主题

18

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
 楼主| 发表于 2020-8-7 22:45:01 | 显示全部楼层
我Spy了一下别人写的程序(实现了我说的功能),高人们能从中看到别人是怎么做的吗?

<02381> 00100458 S WM_COMMAND wNotifyCode:BN_CLICKED wID:2026 hwndCtl:000D0558 [wParam:000007EA lParam:000D0558]
<02382> 000A05C2 S .WM_VSCROLL nScrollCode:SB_PAGEDOWN hwndScrollBar:(null) [wParam:00000003 lParam:00000000]
<02383> 000D0558 S ..WM_KILLFOCUS hwndGetFocus:000A05C2 [wParam:000A05C2 lParam:00000000]
<02384> 00100458 S ...WM_CTLCOLORBTN hdcButton:01010FA1 hwndButton:000D0558 [wParam:01010FA1 lParam:000D0558]
<02385> 00100458 R ...WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02386> 00100458 S ...WM_DRAWITEM idCtl:2026 lpdis:0012EF64 [wParam:000007EA lParam:0012EF64]
<02387> 00100458 R ...WM_DRAWITEM fProcessed:True [lResult:00000001]
<02388> 000D0558 R ..WM_KILLFOCUS lResult:00000000
<02389> 000E0514 S ..message:0x0287 [Unknown] wParam:00000018 lParam:000D0558
<02390> 000D0558 S ...WM_IME_SETCONTEXT fSet:0 (LONG)iShow:C000000F [wParam:00000000 lParam:C000000F]
<02391> 000E0514 S ....WM_IME_SETCONTEXT fSet:0 (LONG)iShow:C000000F [wParam:00000000 lParam:C000000F]
<02392> 000E050C S .....WM_IME_SETCONTEXT fSet:0 (LONG)iShow:C000000F [wParam:00000000 lParam:C000000F]
<02393> 000E050C R .....WM_IME_SETCONTEXT lResult:00000000
<02394> 000E0514 R ....WM_IME_SETCONTEXT lResult:00000000
<02395> 000D0558 R ...WM_IME_SETCONTEXT lResult:00000000
<02396> 000E0514 R ..message:0x0287 [Unknown] lResult:00000000
<02397> 000E0514 S ..message:0x0287 [Unknown] wParam:00000017 lParam:000A05C2
<02398> 000A05C2 S ...WM_IME_SETCONTEXT fSet:1 (LONG)iShow:C000000F [wParam:00000001 lParam:C000000F]
<02399> 000E0514 S ....WM_IME_SETCONTEXT fSet:1 (LONG)iShow:C000000F [wParam:00000001 lParam:C000000F]
<02400> 000E050C S .....WM_IME_SETCONTEXT fSet:1 (LONG)iShow:C000000F [wParam:00000001 lParam:C000000F]
<02401> 000E050C R .....WM_IME_SETCONTEXT lResult:00000000
<02402> 000E0514 R ....WM_IME_SETCONTEXT lResult:00000000
<02403> 000A05C2 R ...WM_IME_SETCONTEXT lResult:00000000
<02404> 000E0514 R ..message:0x0287 [Unknown] lResult:00000000
<02405> 000A05C2 S ..WM_SETFOCUS hwndLoseFocus:000D0558 [wParam:000D0558 lParam:00000000]
<02406> 000A05C2 R ..WM_SETFOCUS lResult:00000000
<02407> 000A05C2 R .WM_VSCROLL lResult:00000000
<02408> 00100458 R WM_COMMAND lResult:00000001
<02409> 000E050C P message:0xC16A [Registered:"MSPY Private"] wParam:00000119 lParam:00000000 time:7:40:22.875 point:(824, 416)
<02410> 000E050C P message:0xC16A [Registered:"MSPY Private"] wParam:00000105 lParam:00000000 time:7:40:22.875 point:(824, 416)
<02411> 000E050C P message:0xC16A [Registered:"MSPY Private"] wParam:00000131 lParam:001A051B time:7:40:22.875 point:(824, 416)
<02412> 000E050C P message:0xC16A [Registered:"MSPY Private"] wParam:00000119 lParam:00000000 time:7:40:22.875 point:(824, 416)
<02413> 000E050C P message:0xC16A [Registered:"MSPY Private"] wParam:00000105 lParam:00000000 time:7:40:22.875 point:(824, 416)
<02414> 000F05DA P message:0xC0B4 [Registered:"MSUIM.Msg.MuiMgrDirtyUpdate"] wParam:00000000 lParam:00000000 time:7:40:22.875 point:(824, 416)
<02415> 000D0558 S WM_NCHITTEST xPos:824 yPos:416 [wParam:00000000 lParam:01A00338]
<02416> 000D0558 R WM_NCHITTEST nHittest:HTCLIENT [lResult:00000001]
<02417> 000D0558 S WM_NCHITTEST xPos:824 yPos:416 [wParam:00000000 lParam:01A00338]
<02418> 000D0558 R WM_NCHITTEST nHittest:HTCLIENT [lResult:00000001]
<02419> 000D0558 S WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02420> 00100458 S .WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02421> 00100446 S ..WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02422> 0021045C S ...WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT
回复

使用道具 举报

2

主题

18

帖子

16.00

积分

新手上路

Rank: 1

积分
16.00
 楼主| 发表于 2020-8-7 23:00:01 | 显示全部楼层
wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02423> 000E0460 S ....WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02424> 00180532 S .....WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02425> 000C051C S ......WM_SETCURSOR hwnd:000D0558 nHittest:HTCLIENT wMouseMsg:WM_MOUSEMOVE [wParam:000D0558 lParam:02000001]
<02426> 000C051C R ......WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02427> 00180532 R .....WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02428> 000E0460 R ....WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02429> 0021045C R ...WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02430> 00100446 R ..WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02431> 00100458 R .WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02432> 000D0558 R WM_SETCURSOR fHaltProcessing:False [lResult:00000000]
<02433> 000D0558 P WM_MOUSEMOVE fwKeys:0000 xPos:7 yPos:11 [wParam:00000000 lParam:000B0007 time:7:40:22.875 point:(824, 416)]
<02434> 000F04F6 P WM_PAINT hdc:00000000 [wParam:00000000 lParam:00000000 time:7:40:22.890 point:(824, 416)]
<02435> 000F04F6 S WM_NCPAINT hrgn:00000001 [wParam:00000001 lParam:00000000]
<02436> 000F04F6 R WM_NCPAINT lResult:00000000
<02437> 000F04F6 S WM_ERASEBKGND hdc:4D011AF1 [wParam:4D011AF1 lParam:00000000]
<02438> 00100508 S .WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000F04F6 [wParam:4D011AF1 lParam:000F04F6]
<02439> 00100508 R .WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02440> 000F04F6 R WM_ERASEBKGND fErased:True [lResult:00000001]
<02441> 00100508 S WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000F04F6 [wParam:4D011AF1 lParam:000F04F6]
<02442> 00100508 R WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02443> 00100508 S WM_DRAWITEM idCtl:2024 lpdis:0012F978 [wParam:000007E8 lParam:0012F978]
<02444> 00100508 R WM_DRAWITEM fProcessed:True [lResult:00000001]
<02445> 000E0486 P WM_PAINT hdc:00000000 [wParam:00000000 lParam:00000000 time:7:40:22.890 point:(824, 416)]
<02446> 000E0486 S WM_NCPAINT hrgn:00000001 [wParam:00000001 lParam:00000000]
<02447> 000E0486 R WM_NCPAINT lResult:00000000
<02448> 000E0486 S WM_ERASEBKGND hdc:4D011AF1 [wParam:4D011AF1 lParam:00000000]
<02449> 00100508 S .WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E0486 [wParam:4D011AF1 lParam:000E0486]
<02450> 00100508 R .WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02451> 000E0486 R WM_ERASEBKGND fErased:True [lResult:00000001]
<02452> 00100508 S WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E0486 [wParam:4D011AF1 lParam:000E0486]
<02453> 00100508 R WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02454> 00100508 S WM_DRAWITEM idCtl:2025 lpdis:0012F978 [wParam:000007E9 lParam:0012F978]
<02455> 00100508 R WM_DRAWITEM fProcessed:True [lResult:00000001]
<02456> 000E0474 P WM_PAINT hdc:00000000 [wParam:00000000 lParam:00000000 time:7:40:22.890 point:(824, 416)]
<02457> 000E0474 S WM_NCPAINT hrgn:00000001 [wParam:00000001 lParam:00000000]
<02458> 000E0474 R WM_NCPAINT lResult:00000000
<02459> 000E0474 S WM_ERASEBKGND hdc:4D011AF1 [wParam:4D011AF1 lParam:00000000]
<02460> 00100508 S .WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E0474 [wParam:4D011AF1 lParam:000E0474]
<02461> 00100508 R .WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02462> 000E0474 R WM_ERASEBKGND fErased:True [lResult:00000001]
<02463> 00100508 S WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E0474 [wParam:4D011AF1 lParam:000E0474]
<02464> 00100508 R WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02465> 00100508 S WM_DRAWITEM idCtl:2026 lpdis:0012F978 [wParam:000007EA lParam:0012F978]
<02466> 00100508 R WM_DRAWITEM fProcessed:True [lResult:00000001]
<02467> 000E05A2 P WM_PAINT hdc:00000000 [wParam:00000000 lParam:00000000 time:7:40:22.890 point:(824, 416)]
<02468> 000E05A2 S WM_NCPAINT hrgn:00000001 [wParam:00000001 lParam:00000000]
<02469> 000E05A2 R WM_NCPAINT lResult:00000000
<02470> 000E05A2 S WM_ERASEBKGND hdc:4D011AF1 [wParam:4D011AF1 lParam:00000000]
<02471> 00100508 S .WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E05A2 [wParam:4D011AF1 lParam:000E05A2]
<02472> 00100508 R .WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02473> 000E05A2 R WM_ERASEBKGND fErased:True [lResult:00000001]
<02474> 00100508 S WM_CTLCOLORBTN hdcButton:4D011AF1 hwndButton:000E05A2 [wParam:4D011AF1 lParam:000E05A2]
<02475> 00100508 R WM_CTLCOLORBTN hBrush:0110005A [lResult:0110005A]
<02476> 00100508 S WM_DRAWITEM idCtl:2027 lpdis:0012F978 [wParam:000007EB lParam:0012F978]
<02477> 00100508 R WM_DRAWITEM fProcessed:True [lResult:00000001]
<02478> 000A05C2 P WM_PAINT hdc:00000000 [wParam:00000000 lParam:00000000 time:7:40:22.890 point:(824, 416)]
<02479> 000A05C2 S WM_ERASEBKGND hdc:0D010F8E [wParam:0D010F8E lParam:00000000]
<02480> 000A05C2 R WM_ERASEBKGND fErased:True [lResult:00000001]
<02481> 000D0558 S WM_NCHITTEST xPos:824 yPos:416 [wParam:00000000 lParam:01A00338]
<02482> 000D0558 R WM_NCHITTEST nHittest:HTCLIENT [lResult:00000001]
<02483> 000D0558 S WM_NCHITTEST xPos:824 yPos:416 [wParam:00000000 lParam:01A00338]
<02484> 000D0558 R WM_NCHITTEST nHittest:HTCLIENT [lResult:00000001]
回复

使用道具 举报

0

主题

2

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-8-9 15:30:01 | 显示全部楼层
他是用WM_CTLCOLORBTN进行消息反射修改控件的背景色
这在MFCGridCtrl中不能达到你的要求
你自己试一下我说的方法
将这段代码替代
CString strText = GetText();
        CString strTemp = strText.Left(1);
        if ( strTemp == "1" )
        {               
                CSize szText = pDC->GetTextExtent( strTemp );               
                for ( int x = 0; x < strText.GetLength(); x++ )
                {
                        strTemp = strText.Mid(x, 1);                       
                        rect.left += szText.cx*x;
                        if ( x % 2 )
                        {
                                pDC->SetTextColor( 0x00ff0000);
                        } else
                        {
                                pDC->SetTextColor( 0x0000ff00);
                        }

                        DrawText(pDC->m_hDC, strTemp, -1, rect, GetFormat() | DT_NOPREFIX);
                }               
        } else DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);
   


原来的
DrawText(pDC->m_hDC, GetText(), -1, rect, GetFormat() | DT_NOPREFIX);

看看demo中是否字符是变色显示的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表