15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


How to remove highlight/focus from an ownerdraw CListCtrl

Gary_Shank@stercomm.com
Thursday, April 11, 1996

     I'm using MSVC 4.1 on Windows NT 4.0 beta.
     I've got an ownerdraw CListCtrl and I'm trying to write a function 
     that will remove the highlight and focus of an item that is selected.  
     Below is some code that I've tried but failed to produce the desired 
     results.  I do get the correct item using GetItem but then it's as if 
     SetItemState isn't working.  I've tried using Update by itself, then 
     InvalidateRect by itself, then Update and InvalidateRect together, but 
     nomatter what, my DrawItem function gets an itemState (from 
     DRAWITEMSTRUCT) of ODS_SELECTED and ODS_FOCUS.  Any ideas?
     
     void CMyCListCtrl::RemoveHighlight()
     {
        // get selected item
        memset(&m_lvItem, NULL, sizeof(m_lvItem));
        m_lvItem.mask = LVIF_STATE;
        m_lvItem.state = LVIS_SELECTED;
        m_lvItem.stateMask = LVIS_SELECTED;
        if ( GetItem(&m_lvItem) == FALSE )
           return;
     
        SetItemState(m_lvItem.iItem, 0, LVIF_STATE);
        Update(m_lvItem.iItem);
        InvalidateRect(NULL, TRUE);
     }
     
     Gary_Shank@stercomm.com



Glen Okita -- go@svpal.org
Monday, April 15, 1996

[Mini-digest: 3 responses]

The 3rd parameter to SetItemState() is a bit mask.  The constant
LVIF_STATE has the value 0x0008, which means the low order focus
and select bits are never affected.  Use a mask value of 0x000F or
possibly (LVIS_FOCUSED | LVIS_SELECTED | LVIS_CUT | LVIS_DROPHILITED).

BTW - I have noticed that my state icon does not update properly
when I grab LVN_ITEMCHANGING and modify the state icon bits.
I need to post a message to myself and call SetItemState() to get
the display corrected.  (I don't call Update() cause it repaints the
entire control unnecessarily.)  Anyone else notice something like this?

                                                               Glen Okita
ti desrever sediT -- Tides reversed it                         go@svpal.org



-----From: "Joseph M. Koral" 

>> SetItemState(m_lvItem.iItem, 0, LVIF_STATE);

You need to provide the stateMask as the third parameter to SetItemState, not the mask.  So in your case, you would want:

     SetItemState(m_lvItem.iItem, 0, LVIS_SELECTED);


Also, why are you using GetItem to find the selected item?  How will this work when multiple items are selected?

- Joseph
     
-----From: "Frederic Steppe" 

>I've got an ownerdraw CListCtrl and I'm trying to write a function 
>that will remove the highlight and focus of an item that is selected.

Try something like this (not tested, but it should work) :

void CMyCListCtrl::RemoveState(int nState)
{
    int nItem = -1;

    while((nItem = GetNextItem(nItem, nState)) >= 0)
    {
        SetItemState(nItem, 0, nState);
    }
}

void CMyCListCtrl::RemoveHighlight()
{
    int nItem = -1;

    RemoveState(LVIS_SELECTED);
    RemoveState(LVIS_FOCUSED);
}

Frederic. (frederics@msn.com)



Joseph Koral -- jkoral@ftp.com
Thursday, April 18, 1996

Frederic. (frederics@msn.com) wrote:

>> void CMyCListCtrl::RemoveState(int nState)
>> {
>>     int nItem = -1;
>>     while((nItem = GetNextItem(nItem, nState)) >= 0)
>>     {
>>         SetItemState(nItem, 0, nState);
>>     }
>> }
>> 
>> 
>> void CMyCListCtrl::RemoveHighlight()
>> {
>>     int nItem = -1;
>> 
>>     RemoveState(LVIS_SELECTED);
>>     RemoveState(LVIS_FOCUSED);
>> }


Ouch.  Why so expensive?  You can change multiple states at the same
time.  Why not just RemoveState(LVIS_SELECTED | LVIS_FOCUSED)?

- Joseph





| Вернуться в корень Архива |