int CSimulateView::ShowList()
{
int nItem = 0;
int i = 0;
CString str;
CClientDC dc( &m_listCtrl );
POINT pt;
m_listCtrl.DeleteAllItems();
//m_listResult为CList<CResult,CResult&>,CResult是自己定义的类
for ( POSITION pos = m_listResult.GetHeadPosition(); pos; m_listResult.GetNext( pos ) )
{
if ( m_listResult.GetAt( pos ).m_bFlag )
{
i = m_listCtrl.InsertItem( nItem, " " );
m_listCtrl.SetItemText( i, 1, m_listResult.GetAt( pos ).m_strName );
str.Format( "%.4f", m_listResult.GetAt( pos ).m_fPro );
m_listCtrl.SetItemText( i, 2, str );
nItem++;
dc.SelectObject( &( m_listResult.GetAt( pos ).m_pen) );
m_listCtrl.GetItemPosition( i, &pt );
dc.MoveTo( pt.x, pt.y + 4 );
dc.LineTo( pt.x + m_listCtrl.GetColumnWidth( 0 ) - 2, pt.y + 4 );
}
}
return nItem;
}
1.为什么添加的行总是添加在头部?怎么添加在ListCtrl的尾部?
2.为什么画线总是画不出,是不是因为dc是个临时变量的原因,为什么放在Ondraw中能画出来?该如何解决呢?
谢谢//bow
{
int nItem = 0;
int i = 0;
CString str;
CClientDC dc( &m_listCtrl );
POINT pt;
m_listCtrl.DeleteAllItems();
//m_listResult为CList<CResult,CResult&>,CResult是自己定义的类
for ( POSITION pos = m_listResult.GetHeadPosition(); pos; m_listResult.GetNext( pos ) )
{
if ( m_listResult.GetAt( pos ).m_bFlag )
{
i = m_listCtrl.InsertItem( nItem, " " );
m_listCtrl.SetItemText( i, 1, m_listResult.GetAt( pos ).m_strName );
str.Format( "%.4f", m_listResult.GetAt( pos ).m_fPro );
m_listCtrl.SetItemText( i, 2, str );
nItem++;
dc.SelectObject( &( m_listResult.GetAt( pos ).m_pen) );
m_listCtrl.GetItemPosition( i, &pt );
dc.MoveTo( pt.x, pt.y + 4 );
dc.LineTo( pt.x + m_listCtrl.GetColumnWidth( 0 ) - 2, pt.y + 4 );
}
}
return nItem;
}
1.为什么添加的行总是添加在头部?怎么添加在ListCtrl的尾部?
2.为什么画线总是画不出,是不是因为dc是个临时变量的原因,为什么放在Ondraw中能画出来?该如何解决呢?
谢谢//bow
在
if ( m_listResult.GetAt( pos ).m_bFlag )
{
i = m_listCtrl.InsertItem( nItem, " " );
这间加一句,改为
if ( m_listResult.GetAt( pos ).m_bFlag )
{
nItem = m_listCtrl.GetItemCount(); //增加的
i = m_listCtrl.InsertItem( nItem, " " );
可实现在尾部添加。
但用在Ondraw中时,控件刷新,Ondraw()函数是会被执行的,所以可行。
建议:要对界面进行作图最好将作图的语句放在有关刷新或重画函数体中,如Ondraw()、OnPaint()等。
2.绘图函数必须在 Ondraw 里调用, 每次刷新时会调用Ondraw,
另外我没大看明白你画线的意思, 如果是想画网格线, 用m_list.SetExtendedStyle(LVS_EX_GRIDLINES);即可
不是画网格线,是为了在每行的中部显示一条线
两位提供的对1的方法怎么在我的程序中都没用:(
m_listCtrl.InsertItem(100000, " "); 这个返回值总是0啊
m_listCtrl.InsertItem(100000, " "); 这个返回值总是0啊你只插入了一条记录吧, 再调用一次就会返回1
void CSimulateView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
CSize sizeTotal( 12500, 8000 );
SetScrollSizes( MM_TWIPS, sizeTotal );
m_rectDraw = CRect( 400, -800, 13000, -6000 );
}void CSimulateView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
//画方框
CRect rect = m_rectDraw;
pDC->DrawEdge( &rect, EDGE_SUNKEN, BF_RECT );//为什么这个方框画不出?
}
{
// TODO: Add your specialized code here and/or call the base class
//画方框
CRect rect = m_rectDraw;
pDC->DrawEdge( &rect, EDGE_SUNKEN, BF_RECT );//为什么这个方框画不出?
}
你的ListCtrl是不是覆盖在整个客户区上? 如果是, 肯定画不出, 被ListCtrl盖住了.试试
void CSimulateView::OnDraw(CDC* pDC)
{
CRect rect;
m_listCtrl.GetClientRect(rect);
rect.Defalte(10, 10);
CClientDC dc(&m_listCtrl);
dc.DrawEdge(rect, EDGE_SUNKEN, BF_RECT );
}
你将插入记录的代码贴出来看看
按照你的方法的代码:
for ( POSITION pos = m_listResult.GetHeadPosition(); pos; m_listResult.GetNext( pos ) )
{
if ( m_listResult.GetAt( pos ).m_bFlag )
{
i = m_listCtrl.InsertItem( 10000, " " ); m_listCtrl.SetItemText( i, 1, m_listResult.GetAt( pos ).m_strName );
str.Format( "%.4f", m_listResult.GetAt( pos ).m_fPro );
m_listCtrl.SetItemText( i, 2, str );
}
}
另外我的ListCtrl只是在客户区底部
用
pDC->MoveTo( m_rectDraw.left, m_rectDraw.top );
pDC->LineTo( m_rectDraw.left, m_rectDraw.bottom );
pDC->LineTo( m_rectDraw.right, m_rectDraw.bottom );
pDC->LineTo( m_rectDraw.right, m_rectDraw.top );
pDC->LineTo( m_rectDraw.left, m_rectDraw.top );可以画出方框
按照你的方法的代码:
for ( POSITION pos = m_listResult.GetHeadPosition(); pos; m_listResult.GetNext( pos ) )
{
if ( m_listResult.GetAt( pos ).m_bFlag )
{
i = m_listCtrl.InsertItem( 10000, " " ); m_listCtrl.SetItemText( i, 1, m_listResult.GetAt( pos ).m_strName );
str.Format( "%.4f", m_listResult.GetAt( pos ).m_fPro );
m_listCtrl.SetItemText( i, 2, str );
}
}
另外我的ListCtrl只是在客户区底部
用
pDC->MoveTo( m_rectDraw.left, m_rectDraw.top );
pDC->LineTo( m_rectDraw.left, m_rectDraw.bottom );
pDC->LineTo( m_rectDraw.right, m_rectDraw.bottom );
pDC->LineTo( m_rectDraw.right, m_rectDraw.top );
pDC->LineTo( m_rectDraw.left, m_rectDraw.top );可以画出方框
for (int i=0; i<50; i++)
{
int index = m_list.InsertItem(10000, " ");
}
pDC->DrawEdge( &rect, EDGE_SUNKEN, BF_RECT )的效果与VIEW的边框太相似了, 你改变一下rect的位置大小试试
for (int i=0; i<50; i++)
{
int index = m_list.InsertItem(10000, " ");//返回值还是0,郁闷!是不是与控件的风格关系?
}
pDC->DrawEdge( &rect, EDGE_SUNKEN, BF_RECT )的效果与VIEW的边框太相似了, 你改变一下rect的位置大小试试//还是显示不出,本来是为了画得漂亮一点,可是显示不出。当时设置为MM_TEXT显示方式时可以画出来