⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owndraw.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
字号:
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.
 
 $Id: OwnDraw.c,v 1.3 2002/10/01 20:32:16 wjb Exp $
__________________________________________________________________________*/
/*
 * OwnDraw.c  ListView draw routines to accommidate italics/strikeout
 *
 * These routines use the ownerdraw characteristic of the standard 
 * listview to provide custom drawing routines.
 *
 */
#include "precomp.h"
#include "pgpUnicode.h"

void CreateDrawElements(DRAWSTRUCT *ds)
{
	LOGFONT lf;
	int iNumBits;
	HDC hDC;
	HBITMAP hBmp;

	ds->DisplayMarginal=FALSE;

	PGPscGetValidityDrawPrefs(PGPsc,&(ds->DisplayMarginal));
	PGPscGetMarginalInvalidPref(PGPsc,&(ds->MarginalInvalid));

    ds->stdbarbrush = 
        CreateSolidBrush (GetSysColor (COLOR_3DSHADOW));
    ds->spcbarbrush = 
        CreateHatchBrush(HS_BDIAGONAL, GetSysColor (COLOR_WINDOW));
    ds->g_seltextpen = 
        CreatePen (PS_SOLID, 0, GetSysColor (COLOR_WINDOWTEXT));
    ds->g_unseltextpen =
        CreatePen (PS_SOLID, 0, GetSysColor (COLOR_WINDOW));

	ds->barcolor = GetSysColor (COLOR_3DSHADOW);

    ds->buttonpen = CreatePen(PS_SOLID, 0, GetSysColor (COLOR_3DSHADOW));
	ds->hilightpen = CreatePen (PS_SOLID, 0, 
								GetSysColor (COLOR_3DHILIGHT));
	ds->shadowpen = CreatePen (PS_SOLID, 0, 
								GetSysColor (COLOR_3DDKSHADOW));

    ds->barbgbrush = CreateSolidBrush (GetSysColor (COLOR_3DFACE));

    ds->HighBrush=CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
    ds->BackBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));

    SystemParametersInfo (SPI_GETICONTITLELOGFONT, 
		sizeof(LOGFONT), &lf, 0);

    ds->hFont=CreateFontIndirect (&lf);
    lf.lfItalic = !lf.lfItalic;
    ds->hItalic=CreateFontIndirect (&lf);
    lf.lfItalic = !lf.lfItalic;
    lf.lfStrikeOut=TRUE;
    ds->hStrikeOut = CreateFontIndirect (&lf);

	// ImageList Init

	hDC = GetDC (NULL);		// DC for desktop
	iNumBits = GetDeviceCaps (hDC, BITSPIXEL) * GetDeviceCaps (hDC, PLANES);
	ReleaseDC (NULL, hDC);

	if (iNumBits <= 8) {
		ds->hIml =	ImageList_Create (16, 16, ILC_COLOR|ILC_MASK, 
							NUM_IMAGELIST_ICONS, 0); 
		hBmp = LoadBitmap (g_hinst, MAKEINTRESOURCE (IDB_IMAGES4BIT));
		ImageList_AddMasked (ds->hIml, hBmp, RGB(255, 0, 255));
		DeleteObject (hBmp);
	}
	else {
		ds->hIml =	ImageList_Create (16, 16, ILC_COLOR24|ILC_MASK, 
							NUM_IMAGELIST_ICONS, 0); 
		hBmp = LoadBitmap (g_hinst, MAKEINTRESOURCE (IDB_IMAGES24BIT));
		ImageList_AddMasked (ds->hIml, hBmp, RGB(255, 0, 255));
		DeleteObject (hBmp);
	}
}

void DeleteDrawElements(DRAWSTRUCT *ds)
{
    DeleteObject(ds->stdbarbrush);
    DeleteObject(ds->spcbarbrush);
    DeleteObject(ds->g_seltextpen);
    DeleteObject(ds->g_unseltextpen);
	DeleteObject(ds->hilightpen);
	DeleteObject(ds->shadowpen);
	DeleteObject(ds->buttonpen);

    DeleteObject(ds->barbgbrush); 
    DeleteObject(ds->HighBrush); 
    DeleteObject(ds->BackBrush); 
	DeleteObject(ds->hFont);
	DeleteObject(ds->hItalic);
	DeleteObject(ds->hStrikeOut);
	ImageList_Destroy(ds->hIml);
}

void DrawBar(DRAWSTRUCT *ds,HDC hdc,RECT *ptrBarRect,
             int DataValue,int MaxValue,BOOL Selected)
{
    RECT rc;
    HBRUSH oldbrush,barbrush;
    HPEN oldpen;
    COLORREF oldbkcolor;
	int cx;

	CopyRect(&rc,ptrBarRect);

    if(Selected)
    {
        FillRect (hdc, &rc, ds->HighBrush);
        oldpen = SelectObject (hdc, ds->g_unseltextpen);
    }
    else
    {
        FillRect (hdc, &rc, ds->BackBrush);
        oldpen = SelectObject (hdc, ds->g_seltextpen);
    }

    rc.top+=5;
    rc.left+=5;
    rc.bottom-=5;
    rc.right-=5;

	if(rc.right<=rc.left)
		return;

	cx=rc.right-rc.left;

	oldbrush = SelectObject (hdc, ds->barbgbrush);
	SelectObject (hdc, ds->buttonpen);

	Rectangle (hdc, rc.left - 1, rc.top - 1, 
		rc.right + 2, rc.bottom + 2);

	SelectObject (hdc, ds->shadowpen);
	MoveToEx (hdc, rc.left, rc.bottom, NULL);
	LineTo (hdc, rc.left, rc.top);
	LineTo (hdc, rc.right, rc.top);

	SelectObject (hdc, ds->hilightpen);
	LineTo (hdc, rc.right, rc.bottom);
	LineTo (hdc, rc.left, rc.bottom);
														
	if (MaxValue != 0) 
	{
		if (DataValue > MaxValue) 
		{
			barbrush = ds->spcbarbrush;
			rc.right = rc.left + cx;
		}
		else 
		{
			barbrush = ds->stdbarbrush;
			rc.right = rc.left + 
				(int)(((float)DataValue / 
				(float)MaxValue)
				* (float)cx);
		}
	}
	else 
		rc.right = rc.left;

	rc.top++;
	rc.left++;

	if (rc.right > rc.left) 
	{
		oldbkcolor=SetBkColor (hdc, ds->barcolor);
		FillRect (hdc, &rc, barbrush); 

		rc.top--;
		rc.left--;

		// hilight pen already selected 
		MoveToEx (hdc, rc.right, rc.top, NULL);
		LineTo (hdc, rc.left, rc.top);
		LineTo (hdc, rc.left,rc.bottom);

		SelectObject (hdc, ds->shadowpen);
		LineTo (hdc, rc.right, rc.bottom);
		LineTo (hdc, rc.right, rc.top);
		SetBkColor(hdc,oldbkcolor);
	}

	SelectObject (hdc, oldbrush);
	SelectObject (hdc, oldpen);
}

void DrawNoviceButton(DRAWSTRUCT *ds,HDC hdc,RECT *ptrBarRect,
             int DataValue,int MaxValue,BOOL Selected)
{
	DWORD NoviceIcon;
//	HICON hIcon;

    if(Selected)
    {
        FillRect (hdc, ptrBarRect, ds->HighBrush);
    }
    else
    {
        FillRect (hdc, ptrBarRect, ds->BackBrush);
    }

	if((ptrBarRect->right-ptrBarRect->left)>=CX_SMICON)
	{
		NoviceIcon=IDX_INVALID;

		if(DataValue>2)
			NoviceIcon=IDX_AXIOMATIC;

		if(DataValue==2)
			NoviceIcon=IDX_VALID;

		if((!ds->MarginalInvalid)&&(DataValue==1))
			NoviceIcon=IDX_VALID;

#ifdef _WIN32
		ImageList_Draw(ds->hIml,NoviceIcon,
			hdc,
			ptrBarRect->left+((ptrBarRect->right-ptrBarRect->left-16)/2),
			ptrBarRect->top+((ptrBarRect->bottom-ptrBarRect->top-16)/2),
			ILD_TRANSPARENT);
#else
		hIcon=LoadIcon(g_hinst,MAKEINTRESOURCE(NoviceIcon));
	
		DrawIcon(hdc,
			ptrBarRect->left+((ptrBarRect->right-ptrBarRect->left-32)/2),
			ptrBarRect->top+((ptrBarRect->bottom-ptrBarRect->top-32)/2),
			hIcon);
	
		DeleteObject(hIcon);
#endif
	}
}

LISTSTRUCT *GetListStruct(HWND hwndList)
{
	return &logList;
}

DRAWSTRUCT *GetDrawStruct(HWND hDlg)
{
	return &logDraw;
}

void DrawStuff(LPDRAWITEMSTRUCT lpDrawItem)
{
	DRAWDATA *dd;
	DRAWSTRUCT *ds;
	LISTSTRUCT *ls;
	int index;
	RECT rc;
//	HICON hIcon;
	HWND hDlg,hwndList;
	int max;

	hwndList=lpDrawItem->hwndItem;
	hDlg=GetParent(hwndList);
	
	ls=GetListStruct(hwndList);
	ds=GetDrawStruct(hDlg);

	dd=(DRAWDATA *)(lpDrawItem->itemData);
	CopyRect(&rc,&(lpDrawItem->rcItem));
/*
	hIcon=LoadIcon(g_hinst,MAKEINTRESOURCE(dd->icon));
	
	DrawIcon(lpDrawItem->hDC,
		rc.left+((CX_SMICON-32)/2),
		rc.top+((CY_SMICON-32)/2),
		hIcon);
	
	DeleteObject(hIcon);
*/
	ImageList_Draw(ds->hIml,dd->icon,
		lpDrawItem->hDC,
		rc.left+((CX_SMICON-16)/2),
		rc.top+((CY_SMICON-16)/2),
		ILD_TRANSPARENT);

	rc.right=rc.left;
	rc.left=rc.left+16;

	max=0;

	for(index=0;index<NUMCOLUMNS;index++)
	{
#if LISTBOX
		max=max+ls->colwidth[index];
#else
		max=max+
			ListView_GetColumnWidth(lpDrawItem->hwndItem,index);
#endif
	}

	for(index=0;index<NUMCOLUMNS;index++)
	{
#if LISTBOX
		rc.right=rc.right+ls->colwidth[index];
#else
		rc.right=rc.right+
			ListView_GetColumnWidth(lpDrawItem->hwndItem,index);
#endif
		switch(dd->type[index])
		{
			case PGP_DDERROR:
			{
				rc.right=max;
				DrawItemColumn(lpDrawItem->hDC,
					(char *)dd->data1[index],
					&rc);
				break;
			}

			case PGP_DDTEXT:
			{
				DrawItemColumn(lpDrawItem->hDC,
					(char *)dd->data1[index],
					&rc);
				break;
			}

			case PGP_DDBAR:
			{
				// Don't display validity for bad signature
				if(dd->icon==IDX_REVCERT)
				{
					if(lpDrawItem->itemState & ODS_SELECTED)
					{
						FillRect (lpDrawItem->hDC, &rc, ds->HighBrush);
					}
					else
					{
						FillRect (lpDrawItem->hDC, &rc, ds->BackBrush);
					}
					break;
				}

				if(ds->DisplayMarginal)
					DrawBar(ds,lpDrawItem->hDC,&rc,
						(int)dd->data1[index],(int)dd->data2[index],
						(lpDrawItem->itemState & ODS_SELECTED));
				else
					DrawNoviceButton(ds,lpDrawItem->hDC,&rc,
					(int)dd->data1[index],(int)dd->data2[index],
					(lpDrawItem->itemState & ODS_SELECTED));
				break;
			}

		}
		rc.left=rc.right;
	}
	lpDrawItem->rcItem.right=max;
}

//
//  DrawListViewItem
//
//  This routine, given a standard Windows LPDRAWITEMSTRUCT, draws the
//  elements of our custom listview (adapted from a routine in the Microsoft
//  Knowledge base)
//

void DrawListViewItem(LPDRAWITEMSTRUCT lpDrawItem)
{
    UINT uiFlags;

	uiFlags=ILD_TRANSPARENT;

    // Check to see if this item is selected
    if (lpDrawItem->itemState & ODS_SELECTED)
    {
        // Set the text background and foreground colors
        SetTextColor(lpDrawItem->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
        SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_HIGHLIGHT));
    
        // Also add the ILD_BLEND50 so the images come out selected
        uiFlags |= ILD_BLEND50;
    }
    else
    {
        // Set the text background and foreground colors to the 
        // standard window colors
        SetTextColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOWTEXT));
        SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOW));
    }

	DrawStuff(lpDrawItem);

	lpDrawItem->rcItem.left=lpDrawItem->rcItem.left+CX_SMICON;

    // If we changed the colors for the selected item, undo it
    if (lpDrawItem->itemState & ODS_SELECTED)
    {
        // Set the text background and foreground colors
        SetTextColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOWTEXT));
        SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOW));
    }

    // If the item is focused, now draw a focus rect around the entire row
    if (lpDrawItem->itemState & ODS_FOCUS)
    {
        // Draw the focus rect
        DrawFocusRect(lpDrawItem->hDC, &(lpDrawItem->rcItem));
    }

    return;
}

//	_______________________________________________
//
//  Truncate text string

static BOOL sTruncateText (
		HDC		hdc, 
		WCHAR*	wsz, 
		int		xmax) 
{
	BOOL bShowToolTip	= FALSE;
	SIZE s;
	int l, w;

	// truncate at <newline>, if present
	l = 0;
	while (wsz[l])
	{
		if (wsz[l] == '\n')
		{
			wsz[l] = 0;
			bShowToolTip = TRUE;
			break;
		}
		l++;
	}

	GetTextExtentPoint32W (hdc, wsz, lstrlenW (wsz), &s);
	w = s.cx + 4;

	l = lstrlenW (wsz);
	if (l < 3) 
	{
		if (w > xmax) 
		{
			bShowToolTip = TRUE;
			wsz[0] = 0;
		}
	}
	else 
	{
		l = lstrlenW (wsz) - 3;

		while ((w > xmax) && (l >= 0))
		{
			bShowToolTip = TRUE;
			lstrcpyW (&wsz[l], L"...");
			GetTextExtentPoint32W (hdc, wsz, lstrlenW (wsz), &s);
			w = s.cx + 4;
			l--;
		}

		if (l < 0) 
			wsz[0] = 0;
	}

	return bShowToolTip;
}

#define MAXUSERIDSTRING 300

// DrawItemColumn
//
// Given a clipping rectange and some text, see how well we can fit
// it in there, and tack on ... if we can't

void DrawItemColumn(HDC hdc, LPTSTR lpsz, LPRECT prcClip)
{
	WCHAR wsz[MAXUSERIDSTRING];
	PGPUInt32 ulen;

	pgpUTF8StringToUCS2 (lpsz, kPGPUnicodeNullTerminated, 
			wsz, MAXUSERIDSTRING, &ulen);

	sTruncateText (hdc, wsz, prcClip->right - prcClip->left);

	// print the text
	ExtTextOutW(hdc, prcClip->left, prcClip->top, 
			   ETO_CLIPPED | ETO_OPAQUE,
			   prcClip, 
			   wsz, 
			   lstrlenW(wsz), 
			   NULL);
}


// Main_OnDrawItem
//
// Entry function for the message handler. Basically, we want to draw
// the whole thing no matter what.

BOOL Main_OnDrawItem(HWND hwnd, const DRAWITEMSTRUCT * lpDrawItem)
{

    // Make sure the control is the listview control
    if ((lpDrawItem->CtlType != ODT_LISTVIEW)&&
		(lpDrawItem->CtlType != ODT_LISTBOX))
        return FALSE;

    switch (lpDrawItem->itemAction)
    {
        case ODA_DRAWENTIRE:
        case ODA_FOCUS:
        case ODA_SELECT:
            DrawListViewItem((LPDRAWITEMSTRUCT)lpDrawItem);
            break;
    }

    return TRUE;
}

// Main_OnMeasureItem
//
// Entry function for the message handler. We need to get the width and
// height of the font we're using.

void Main_OnMeasureItem(HWND hwnd, MEASUREITEMSTRUCT * lpMeasureItem)
{
    TEXTMETRIC tm;
    HDC hdc;
    HWND hwndLV;
    HFONT hFont;

    // Make sure the control is the listview control
    if ((lpMeasureItem->CtlType != ODT_LISTVIEW)&&
		(lpMeasureItem->CtlType != ODT_LISTBOX))
        return;

    // Get the handle of the ListView control we're using
    hwndLV = GetDlgItem(hwnd, lpMeasureItem->CtlID);

    // Get the font the control is currently using
    hFont = (HFONT)(DWORD) SendMessage(hwndLV, WM_GETFONT, 0, 0L);

    // Set the font of the DC to the same font the control is using
    hdc = GetDC(hwndLV);
    SelectObject(hdc, hFont);

    // Get the height of the font used by the control
    if (!GetTextMetrics(hdc, &tm))
        return;

    // Add a little extra space between items
    lpMeasureItem->itemHeight = tm.tmHeight + 1;

    // Make sure there is enough room for the images which are CY_SMICON high
    if (lpMeasureItem->itemHeight < (CY_SMICON + 1))
        lpMeasureItem->itemHeight = CY_SMICON + 1;

    // Clean up
    ReleaseDC(hwndLV, hdc);
}


/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

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