handlecombo.cpp

来自「IO函数调用测试」· C++ 代码 · 共 233 行

CPP
233
字号
// HandleCombo.cpp : implementation file
//

#include "stdafx.h"
#include "IOExplorer.h"
#include "TraceEvent.h"
  #include "Handle.h"
    #include "HandleCombo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHandleCombo

CHandleCombo::CHandleCombo()
{
 maxlen = 0;
}

CHandleCombo::~CHandleCombo()
{
}


BEGIN_MESSAGE_MAP(CHandleCombo, CComboBox)
	//{{AFX_MSG_MAP(CHandleCombo)
	ON_WM_DRAWITEM()
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHandleCombo message handlers

void CHandleCombo::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	CComboBox::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

int CHandleCombo::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
{
	// TODO: Add your code to determine the sorting order of the specified items
	// return -1 = item 1 sorts before item 2
	// return 0 = item 1 and item 2 sort the same
	// return 1 = item 1 sorts after item 2
	
	return 0;
}

void CHandleCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	// TODO: Add your code to determine the size of specified item
	
}

void CHandleCombo::DeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CComboBox::DeleteItem(lpDeleteItemStruct);
}

/****************************************************************************
*                           CHandleCombo::DrawItem
* Inputs:
*       LPDRAWITEMSTRUCT dis:
* Result: void
*       
* Effect: 
*       Handles the drawing
****************************************************************************/

void CHandleCombo::DrawItem(LPDRAWITEMSTRUCT dis) 
{
 CRect r = dis->rcItem;
 SIZE w;
 CDC * dc = CDC::FromHandle(dis->hDC);
 CBrush br;
 CBrush * oldbrush;
 COLORREF txcolor;
 COLORREF bkcolor;
 COLORREF hcolor;
 CHandle * ho = (CHandle * )dis->itemData;

 if(dis->itemState & ODS_SELECTED)
    { /* selected */
     bkcolor = GetSysColor(COLOR_HIGHLIGHT);
     txcolor = GetSysColor(COLOR_HIGHLIGHTTEXT);
     hcolor = RGB(0, 255, 255);
    } /* selected */
 else
    { /* unselected */
     if(dis->itemState & (ODS_DISABLED | ODS_GRAYED))
        { /* disabled */
	 txcolor = GetSysColor(COLOR_GRAYTEXT);
	 hcolor  = RGB(127, 0, 0);
	} /* disabled */
     else
        { /* enabled */
	 txcolor = GetSysColor(COLOR_WINDOWTEXT);
	 hcolor  = RGB(255, 0, 0);
	} /* enabled */

     bkcolor = GetSysColor(COLOR_WINDOW);
    } /* unselected */

 dc->SetBkColor(bkcolor);

 br.CreateSolidBrush(bkcolor);
 oldbrush = dc->SelectObject(&br);

 dc->PatBlt(r.left, r.top, r.Width(), r.Height(), PATCOPY);

 dc->SelectObject(oldbrush);
 br.DeleteObject();

 if(dis->itemID != (UINT)-1 
    && (dis->itemState & (ODS_DISABLED | ODS_GRAYED)) == 0)
    { /* has item */
     CString s;
     s.Format(_T("%08x"), ho->h);

     dc->SetTextColor(hcolor);
     dc->TextOut(r.left, r.top, s);

     w = dc->GetTextExtent(_T("00000000 "), 9);
     
     dc->SetTextColor(txcolor);
     dc->TextOut(r.left + w.cx, r.top, ho->Name);
    } /* has item */
	
 if(dis->itemState & ODS_FOCUS && dis->itemAction != ODA_SELECT)
    dc->DrawFocusRect(&r);
}

void CHandleCombo::OnDropdown() 
{
 int n = GetCount();
 n = max(n, 2);

 int ht = GetItemHeight(0);
 CRect r;
 GetWindowRect(&r);

 if(maxlen > 0)
    n = max(maxlen, 2);

 CSize sz;
 sz.cx = r.Width();
 sz.cy = ht * (n + 2);

 if(maxlen < 0)
    { /* screen limit */
     if(r.top - sz.cy < 0 || r.bottom + sz.cy > ::GetSystemMetrics(SM_CYSCREEN))
        { /* invoke limit */
         // Compute the largest distance the dropdown can appear, 
         // relative to the screen (not the window!)

         int k = max( (r.top / ht), 
                      (::GetSystemMetrics(SM_CYSCREEN) - r.bottom) / ht);

         // compute new space. Note that we don't really fill the screen.
         // We only have to use this size if it is smaller than the max size
         // actually required
         int ht2 = ht * k;
         sz.cy = min(ht2, sz.cy);
        } /* invoke limit */
    } /* screen limit */

 SetWindowPos(NULL, 0, 0, sz.cx, sz.cy, SWP_NOMOVE | SWP_NOZORDER);
}

/****************************************************************************
*                           CHandleCombo::addHandle
* Inputs:
*       CHandle * ho: Handle object to add
* Result: int
*       Position at which it was added
* Effect: 
*       Adds the handle object to the list box
****************************************************************************/

int CHandleCombo::AddString(CHandle * h)
    {
     int i = CComboBox::AddString((LPCTSTR)h);
     return i;
    }

/****************************************************************************
*                          CHandleCombo::SelectItem
* Inputs:
*       CHandle * ho: Handle to select
* Result: int
*       Index of selected item
* Effect: 
*       Selects the item whose itemdata matches
****************************************************************************/

int CHandleCombo::SelectItem(CHandle * ho)
    {
     int n = GetCount();
     for(int i = 0; i < n; i++)
        { /* select */
	 if(CComboBox::GetItemDataPtr(i) == ho)
	    { /* found it */
	     CComboBox::SetCurSel(i);
	     return i;
	    } /* found it */
	} /* select */
     return CB_ERR;
    }

/****************************************************************************
*                           CHandleCombo::GetCurHandle
* Result: CHandle *
*       or NULL if no selection active
****************************************************************************/

CHandle * CHandleCombo::GetCurHandle()
    {
     int i = CComboBox::GetCurSel();
     if(i == CB_ERR)
	return NULL;

     return (CHandle *)CComboBox::GetItemDataPtr(i);
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?