有关代码修改的问题

smallfool 北京秀迈时空信息技术有限公司 CTO  2003-07-15 02:25:50
我最近在浏览公司的一个VC高手写的代码。
看了一些,基本理清了类的的层次关系,但是这里面有一个问题,纯粹是编码风格,我想征求一下大家的看法。
代码中用到了网上比较流行的MFCGridCtrl的原代码,这位高手没有从这个类进行派生,而是直接在MFCGridCtrl的实现函数进行了修改。经过我细致的区别,发现修改比较大,我想问的是,大家对这样的修改的态度是什么?

...全文
7 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallfool 2003-07-22
没有人理睬啊????
回复
smallfool 2003-07-17
只好贴代码了。纵观整个的CSplitterWndEx的实现代码,几乎就是把MFC中CSplitterWnd的实现文件拷贝过来,几乎每个函数中都有被注释的部分。不知道各位对这样的编码风格有什么看法?
回复
smallfool 2003-07-17
BOOL CSplitterWndEx::CreateView(int row, int col,
CRuntimeClass* pViewClass, SIZE sizeInit, CCreateContext* pContext)
{
#ifdef _DEBUG
ASSERT_VALID(this);
ASSERT(row >= 0 && row < m_nRows);
ASSERT(col >= 0 && col < m_nCols);
ASSERT(pViewClass != NULL);
ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));

if (GetDlgItem(IdFromRowCol(row, col)) != NULL)
{
TRACE2("Error: CreateView - pane already exists for row %d, col %d.\n",
row, col);
ASSERT(FALSE);
return FALSE;
}
#endif

// set the initial size for that pane
m_pColInfo[col].nIdealSize = sizeInit.cx;
m_pRowInfo[row].nIdealSize = sizeInit.cy;

BOOL bSendInitialUpdate = FALSE;

CCreateContext contextT;
if (pContext == NULL)
{
// if no context specified, generate one from the currently selected
// client if possible
CView* pOldView = (CView*)GetActivePane();
if (pOldView != NULL && pOldView->IsKindOf(RUNTIME_CLASS(CView)))
{
// set info about last pane
ASSERT(contextT.m_pCurrentFrame == NULL);
contextT.m_pLastView = pOldView;
contextT.m_pCurrentDoc = pOldView->GetDocument();
if (contextT.m_pCurrentDoc != NULL)
contextT.m_pNewDocTemplate =
contextT.m_pCurrentDoc->GetDocTemplate();
}
pContext = &contextT;
bSendInitialUpdate = TRUE;
}

CWnd* pWnd;
TRY
{
pWnd = (CWnd*)pViewClass->CreateObject();
if (pWnd == NULL)
AfxThrowMemoryException();
}
CATCH_ALL(e)
{
TRACE0("Out of memory creating a splitter pane.\n");
// Note: DELETE_EXCEPTION(e) not required
return FALSE;
}
END_CATCH_ALL

ASSERT_KINDOF(CWnd, pWnd);
ASSERT(pWnd->m_hWnd == NULL); // not yet created

DWORD dwStyle = AFX_WS_DEFAULT_VIEW;
// if (afxData.bWin4)
// dwStyle &= ~WS_BORDER;

// Create with the right size (wrong position)
CRect rect(CPoint(0,0), sizeInit);
if (!pWnd->Create(NULL, NULL, dwStyle,
rect, this, IdFromRowCol(row, col), pContext))
{
TRACE0("Warning: couldn't create client pane for splitter.\n");
// pWnd will be cleaned up by PostNcDestroy
return FALSE;
}
ASSERT((int)((UINT)(WORD)::GetDlgCtrlID(pWnd->m_hWnd)) == IdFromRowCol(row, col));

// send initial notification message
if (bSendInitialUpdate)
pWnd->SendMessage(WM_INITIALUPDATE);

return TRUE;
}

void CSplitterWndEx::OnPaint()
{
ASSERT_VALID(this);
CPaintDC dc(this);

CRect rectClient;
GetClientRect(&rectClient);
rectClient.InflateRect(-m_cxBorder, -m_cyBorder);

CRect rectInside;
GetInsideRect(rectInside);

// draw the splitter boxes
if (m_bHasVScroll && m_nRows < m_nMaxRows)
{
OnDrawSplitter(&dc, splitBox,
CRect(rectInside.right /*+ afxData.bNotWin4*/, rectClient.top,
rectClient.right, rectClient.top + m_cySplitter));
}

if (m_bHasHScroll && m_nCols < m_nMaxCols)
{
OnDrawSplitter(&dc, splitBox,
CRect(rectClient.left, rectInside.bottom /*+ afxData.bNotWin4*/,
rectClient.left + m_cxSplitter, rectClient.bottom));
}

// extend split bars to window border (past margins)
DrawAllSplitBars(&dc, rectInside.right, rectInside.bottom);

if (1/*!afxData.bWin4*/)
{
// draw splitter intersections (inside only)
GetInsideRect(rectInside);
dc.IntersectClipRect(rectInside);
CRect rect;
rect.top = rectInside.top;
for (int row = 0; row < m_nRows - 1; row++)
{
rect.top += m_pRowInfo[row].nCurSize + m_cyBorderShare;
rect.bottom = rect.top + m_cySplitter;
rect.left = rectInside.left;
for (int col = 0; col < m_nCols - 1; col++)
{
rect.left += m_pColInfo[col].nCurSize + m_cxBorderShare;
rect.right = rect.left + m_cxSplitter;
OnDrawSplitter(&dc, splitIntersection, rect);
rect.left = rect.right + m_cxBorderShare;
}
rect.top = rect.bottom + m_cxBorderShare;
}
}
/*
CWnd* pView = CViewEx::GetActive();
if( pView != NULL )
{
pView->GetClientRect(&rectClient);
static CWnd* pPreView = pView;
if( pPreView != pView )
{
if( ::IsWindow(pPreView->m_hWnd) )
{
pPreView->GetClientRect(&rectClient);
DrawFocus(rectClient);
}
pPreView = pView;
}
DrawFocus(rectClient);
}
*/
}
回复
xtuzi 2003-07-17
我觉得没必要拘泥于什么形式吧,是直接改还是重载,怎么样方便就怎么样写吧
回复
smallfool 2003-07-17
没有人理睬我了?关注???
回复
smallfool 2003-07-15
没有其它人嘛??高手给指点指点?????
回复
pcitman 2003-07-15
大面积的修改其实已经不能算修改了,跟重写差不多,只留下类的框架
回复
smallfool 2003-07-15
pcitman,你修改的时候是不是大面积的修改呢?这里面是不是有个平衡问题?
回复
pcitman 2003-07-15
有时候我也不喜欢对类进行派生,直接修改比较灵活些
回复
smallfool 2003-07-15
另外,改动的时候,最多是注释掉了很多的代码,但几乎都没有任何的注释,这样我在比较的时候,有时候就很模糊,不知道为什么要注释这样的一段代码。我相信MFCGridCtrl原代码应该是比较成熟和稳定。
回复
发动态
发帖子
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
社区公告
暂无公告