怪了!!!

mfczhiwang 2008-11-01 02:33:15
我这段代码怎么执行错误啊
我定义了个CStringArray strchar;
每按下一个键,就存到strchar中,然后刷新,在ondraw()中输出
我想让其实现换行,但。。。
CString str;
TEXTMETRIC tm;
CRect rectClient;
GetClientRect(&rectClient);
pDC->GetTextMetrics(&tm);
int i=pDoc->strchars.GetSize();
int ncount=rectClient.Width()/tm.tmMaxCharWidth;
int nline=i/ncount+1;
for(int m=0;m<nline;m++)
{
for(int j=0;j<ncount;j++)
{
str=pDoc->strchars.GetAt(j+m*ncount);
pDC->TextOut(j*tm.tmMaxCharWidth,m*16,str);
}
}
编译没问题,执行出错。请大侠指错啊。
另外,怎么实现光标跟随输出移动啊?!!
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfczhiwang 2008-11-02
  • 打赏
  • 举报
回复
我解决了:
CString str;
TEXTMETRIC tm;
CRect rectClient;
GetClientRect(&rectClient);
pDC->GetTextMetrics(&tm);
int i=pDoc->strchars.GetSize();
int ncount=rectClient.Width()/tm.tmMaxCharWidth;
int nline=1+i/ncount;
int hangliang;
for(int m=0;m<nline;m++)
{
if(m==nline-1)
{
hangliang=i-(nline-1)*ncount;
}
else
hangliang=ncount;
for(int j=0;j<hangliang;j++)
{
str=pDoc->strchars.GetAt(j+m*ncount);
pDC->TextOut(j*tm.tmMaxCharWidth,16*m,str);
}
}
kingcrab 2008-11-01
  • 打赏
  • 举报
回复
一下是我的一个测试控件实现数据输入的相关的几个函数,希望对你有用:
void CTestControlCtrl::OnSetFocus(CWnd* pOldWnd)
{
COleControl::OnSetFocus(pOldWnd);
m_isFocus = TRUE;
CDC *pdc = GetDC();
if(NULL != pdc)
{
TEXTMETRIC temp;
pdc->GetTextMetrics(&temp);
int m_cursorHeigh = temp.tmHeight;
::CreateCaret(m_hWnd,NULL,1,m_cursorHeigh);
::ShowCaret(m_hWnd);
SetCursorPos(pdc);
ReleaseDC(pdc);
}
}

void CTestControlCtrl::OnKillFocus(CWnd* pNewWnd)
{
COleControl::OnKillFocus(pNewWnd);
m_isFocus = FALSE;
::DestroyCaret();
}

void CTestControlCtrl::ShowText(CDC *pdc)
{
if(NULL != pdc)
{
pdc->SetBkMode(TRANSPARENT);
pdc->TextOut(0,0,m_str,m_strlen);
pdc->SetBkMode(OPAQUE);
}
SetCursorPos(pdc);
}

void CTestControlCtrl::SetCursorPos(CDC *pdc)
{
if((TRUE == m_isFocus) && (NULL != pdc))
{
CSize size = pdc->GetTextExtent(m_str,m_strlen);
::SetCaretPos(size.cx,0);
}
}
kingcrab 2008-11-01
  • 打赏
  • 举报
回复
光标跟随:

CreateCaret;
ShowCaret;
SetCaretPos
其中的的SetCaretPos位置参数可以使用GetTextExtent获得

laomig 2008-11-01
  • 打赏
  • 举报
回复
strchars.GetAt(m)直接可以得到一个CString类型的对象,而不是一个字符,所以你GetAt(j+m*ncount)肯定越界
不明白你到底想实现什么,第一个循环的退出条件不论怎样都应该是m<i,因为i表示array中有几个CString字符串,第二个循环位置换一下位置
int len,cnt,sPos;
CString temp;
for(int m=0;m <i;m++)
{
str=pDoc->strchars.GetAt(m); //CStringArray是用GetAt还是 [ ]我记不清了
len=str.GetLength();
nline=len/nCount; //nCount是不是一行最多显示的字符数?
for(int j=0;j <nline;j++)
{
cnt=min(len, nCount);
len-=nCount;
temp=str.Mid(j*nCount, cnt);
pDC->TextOut(0,m*16,temp);
}
}
菜牛 2008-11-01
  • 打赏
  • 举报
回复
j+m*ncount超出数组边界了。你这里直接用GetAt(m)就可以了,循环也是m<i。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧