在网上找了很很久windows mobile上c++版本的九宫格,发现没有什么好的案例。后来想自己写。感觉成果很好。现在拿出来与大家共享
重载CListCtrl
Oncreate(....)
产生list元素
填充CImageList
//绑定ImageList与listctrl控件
SetImageList(m_pImageList, LVSIL_NORMAL);
//设置每个元素的位置
SetItemPosition(nItem,pt);
处理每个元素的click动作
ON_WM_LBUTTONUP
处理一些绘制动作
OnNMCustomdraw
像
if (pNMCD->dwDrawStage == CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
CDC* pDC = CDC::FromHandle(pNMCD->hdc);
DrawGridLine(pDC);
DrawFoucsFrame(pDC);
}
。。。。。。。。。。。。。
处理背景
ON_WM_ERASEBKGND
像 RECT rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
pDC->FillRect(&rect,&brush);
return TRUE;
。。。。。。。。。。。。。。
效果见下面链接
/Files/frankhearted/6.JPG
不得转载。。。。。。。。。。。谢谢!!
//
#include " stdafx.h "
#include " Police.h "
#include " Grid.h "
// CGrid
IMPLEMENT_DYNAMIC(CGrid, CWnd)
CGrid::CGrid(GRIDPROP grid)
{
RegisterWindowClass();
m_grid.uCount = grid.uCount;
m_grid.item = new RIGHTITEM[grid.uCount];
ZeroMemory(m_grid.item, sizeof (RIGHTITEM) * (grid.uCount));
CopyMemory(m_grid.item, grid.item, sizeof (RIGHTITEM) * (grid.uCount));
m_pGridItem = NULL;
m_gridDescripe = grid;
strPicFormat = TEXT( " image\\%s.png " );
}
CGrid:: ~ CGrid()
{
delete [] m_grid.item;
delete [] m_pGridItem;
DeleteObject(m_brush);
}
BEGIN_MESSAGE_MAP(CGrid, CWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
ON_WM_KEYUP()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CGrid message handlers
BOOL CGrid::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if ( ! (::GetClassInfo(hInst, NINEGRID_CLASSNAME, & wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0 ;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp() -> LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1 );
// wndcls.hbrBackground = brush;
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = NINEGRID_CLASSNAME;
if ( ! AfxRegisterClass( & wndcls)) {
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
void CGrid::CreateListItem()
{
m_pGridItem = new CGridItem[m_grid.uCount];
if (NULL == m_pGridItem)
return ;
m_pGridItem[m_gridDescripe.uCurSel].SetItemState(ACTION);
}
void CGrid::OnPaint()
{
wprintf(L " CGrid::OnPaint++\r\n " );
// CPaintDC dc(this); // device context for painting
PAINTSTRUCT ps;
::BeginPaint(m_hWnd, & ps);
::EndPaint(m_hWnd, & ps);
DrawGrid();
wprintf(L " CGrid::OnPaint--\r\n " );
}
void CGrid::DrawGrid()
{
CRect rect;
CDC MemDC;
CBitmap MemBitmp;
int nIndex = 0 ;
CString strFileName;
m_pGridItem[m_gridDescripe.uCurSel].SetItemState(ACTION);
if (NULL == m_pGridItem)
return ;
DWORD dwfirst = GetTickCount();
GetClientRect( & rect);
MemDC.CreateCompatibleDC(NULL);
MemBitmp.CreateCompatibleBitmap(GetDC(),rect.Width(),rect.Height());
CBitmap * pOldBitmap = MemDC.SelectObject( & MemBitmp);
CGlobalFun::GetInstance() -> DrawImage(MemDC,rect,GRID_BACKIMAGE);
// Initializes a CFont object with the specified characteristics.
CFont font;
// VERIFY(font.CreateFont(
// 12, // nHeight
// 0, // nWidth
// 0, // nEscapement
// 0, // nOrientation
// FW_BOLD , // nWeight
// FALSE, // bItalic
// FALSE, // bUnderline
// 0, // cStrikeOut
// ANSI_CHARSET, // nCharSet
// OUT_DEFAULT_PRECIS, // nOutPrecision
// CLIP_DEFAULT_PRECIS, // nClipPrecision
// DEFAULT_QUALITY, // nQuality
// DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
// TEXT("宋体")) ); // lpszFacename
VERIFY(font.CreateFont( 12 ,
0 ,
0 ,
0 ,
FW_SEMIBOLD,
FALSE,
FALSE,
0 ,
GB2312_CHARSET,
3 ,
CLIP_STROKE_PRECIS,
DEFAULT_QUALITY,
VARIABLE_PITCH | FF_MODERN,
TEXT( " 宋体 " )) ); // lpszFacename
CFont * def_font = MemDC.SelectObject( & font);
MemDC.SetBkMode(TRANSPARENT );
COLORREF oldTextColor = MemDC.SetTextColor(GRID_TITILE_COLOR);
int col = GetSystemMetrics(SM_CXSCREEN) < GetSystemMetrics(SM_CYSCREEN) ? ROW_NORMAL : ROW_WIDE;
int nrow = (m_grid.uCount / col) + (((m_grid.uCount % col) > 0 ) ? 1 : 0 );
for ( int y = 0 ; y < nrow && nIndex < m_grid.uCount;y ++ )
{
for ( int x = 0 ; x < col && nIndex < m_grid.uCount;x ++ )
{
CRect rc;
rc.left = x * m_gridDescripe.wIcon;
rc.top = y * m_gridDescripe.hIcon;
rc.right = rc.left + m_gridDescripe.wIcon;
rc.bottom = rc.top + m_gridDescripe.hIcon;
if (m_pGridItem[nIndex].GetItemState() == ACTION)
{
strFileName.Format(strPicFormat,SEL_PIC);
CGlobalFun::GetInstance() -> DrawImage(MemDC,rc,strFileName);
}
strFileName.Format(strPicFormat,m_grid.item[nIndex].id);
CGlobalFun::GetInstance() -> DrawImage(MemDC,rc,strFileName);
m_pGridItem[nIndex].SetRect(rc);
rc.bottom -= PIC_MIDDLE_TITEL;
MemDC.DrawText(m_grid.item[nIndex].desc,rc,DT_CENTER | DT_BOTTOM);
nIndex ++ ;
}
}
MemDC.SelectObject(def_font);
font.DeleteObject();
MemDC.SetTextColor(oldTextColor);
GetDC() -> BitBlt( 0 , 0 ,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN), & MemDC, 0 , 0 ,SRCCOPY);
DeleteObject(MemBitmp);
DeleteDC(MemDC);
}
void CGrid::OnLButtonDown(UINT nFlags, CPoint point)
{
wprintf(L " CGrid::OnLButtonDown++\r\n " );
// TODO: Add your message handler code here and/or call default
for ( int k = 0 ; k < m_grid.uCount;k ++ )
{
if (m_pGridItem[k].PtInRect(point))
{
m_gridDescripe.uCurSel = k;
ClearActionState();
m_pGridItem[k].SetItemState(ACTION);
wprintf(L " CGrid::OnLButtonDown m_gridDescripe.func++\r\n " );
m_gridDescripe.func( & k, m_gridDescripe.paret);
wprintf(L " CGrid::OnLButtonDown m_gridDescripe.func--\r\n " );
break ;
}
}
// CWnd::OnLButtonDown(nFlags, point);
wprintf(L " CGrid::OnLButtonDown--\r\n " );
}
void CGrid::ClearActionState()
{
for ( int k = 0 ; k < m_grid.uCount; k ++ )
{
m_pGridItem[k].SetItemState(UNACTION);
}
}
int CGrid::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == - 1 )
return - 1 ;
// TODO: Add your specialized creation code here
CreateListItem();
return 0 ;
}
void CGrid::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
// if(::GetFocus() == *this)
{
int rowitem = GetSystemMetrics(SM_CXSCREEN) < GetSystemMetrics(SM_CYSCREEN) ? ROW_NORMAL : ROW_WIDE;
int nrow = (m_grid.uCount / rowitem) + (((m_grid.uCount % rowitem) > 0 ) ? 1 : 0 );
switch (nChar)
{
case VK_UP:
m_gridDescripe.uCurSel = (m_gridDescripe.uCurSel / rowitem) > 0 ? (m_gridDescripe.uCurSel - rowitem):m_gridDescripe.uCurSel;
break ;
case VK_DOWN:
m_gridDescripe.uCurSel = (UINT)(m_gridDescripe.uCurSel / rowitem) < (UINT)(nrow - 1 ) ? (m_gridDescripe.uCurSel + rowitem):m_gridDescripe.uCurSel;
break ;
case VK_RIGHT:
m_gridDescripe.uCurSel = (((m_gridDescripe.uCurSel + 1 ) % rowitem) == 0 ) ? m_gridDescripe.uCurSel:m_gridDescripe.uCurSel + 1 ;
break ;
case VK_LEFT:
m_gridDescripe.uCurSel = (((m_gridDescripe.uCurSel) % rowitem) == 0 ) ? m_gridDescripe.uCurSel:m_gridDescripe.uCurSel - 1 ;
break ;
case VK_RETURN:
m_gridDescripe.uCurSel = m_gridDescripe.uCurSel;
m_gridDescripe.func( & m_gridDescripe.uCurSel, m_gridDescripe.paret);
return ;
default :
return ;
}
ClearActionState();
DrawGrid();
}
CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
}
BOOL CGrid::OnEraseBkgnd(CDC * pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
// return CWnd::OnEraseBkgnd(pDC);
}
---------------------------------------------------------------------------
#pragma once
#include " GridItem.h "
// CGrid
class CGrid : public CWnd
{
DECLARE_DYNAMIC(CGrid)
public :
CGrid(GRIDPROP grid);
virtual ~ CGrid();
protected :
DECLARE_MESSAGE_MAP()
private :
BOOL RegisterWindowClass();
void CreateListItem();
void DrawGrid();
void ClearActionState();
private :
GRIDITEM m_grid;
CGridItem * m_pGridItem;
GRIDPROP m_gridDescripe;
CBrush m_brush;
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
CString strPicFormat;
afx_msg BOOL OnEraseBkgnd(CDC * pDC);
};