菜鸟请教!关于键盘与光标的程序!
具体描述如下:
SDI下
初始化时,光标在视图左上角,键盘输入字符,最后光标在字符最后面显示!
问题点数:20、回复次数:7Top
1 楼weakwater(我是河南人)回复于 2004-09-03 10:01:32 得分 5
BOOL SetCursorPos( int x, int y);Top
2 楼tyboy007(tyboy007)回复于 2004-09-03 11:35:28 得分 0
有具体一点的代码吗?
我的功能是初始化有光标在左上角,
可以输入文字,
输入完,光标落在文字最后!
要用到Focus吗?
Top
3 楼weakwater(我是河南人)回复于 2004-09-03 11:52:17 得分 0
那个光标是你自己画的图,一闪一闪的也是你让它做的动作
都是通过编程实现的Top
4 楼tyboy007(tyboy007)回复于 2004-09-03 13:52:33 得分 0
怎样做呢?Top
5 楼yaozijian110()回复于 2004-09-03 14:17:50 得分 15
选择视图类的基类为CEditView就行了。
如果要自己编程实现的话,下面是一个很简单的例子。当然,它很不完善。
void CCursorView::OnSetFocus(CWnd* pOldWnd)
{
CView::OnSetFocus(pOldWnd);
//创建插入符
POINT pt;
HDC hdc;
TEXTMETRIC tm;
hdc = ::GetDC(m_hWnd);
GetTextMetrics(hdc,&tm);
pt.x = 2;//tm.tmAveCharWidth;
pt.y = tm.tmHeight + tm.tmExternalLeading;
::ReleaseDC(m_hWnd,hdc);
::CreateCaret(m_hWnd,NULL,pt.x,pt.y);
::SetCaretPos(0,0);
ShowCaret();
}
void CCursorView::OnKillFocus(CWnd* pNewWnd)
{
CView::OnKillFocus(pNewWnd);
//销毁插入符
HideCaret();
DestroyCaret();
}
void CCursorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
TCHAR *pText;
int iLen;
iLen = ::GetWindowTextLength(m_hWnd);
pText = (LPTSTR)malloc(sizeof(TCHAR) * (iLen+2));
memset(pText,0,sizeof(TCHAR) * (iLen+1));
::GetWindowText(m_hWnd,pText,iLen+2);
pText[iLen] = nChar;
pText[iLen+1] = 0;
SetWindowText(pText);
free(pText);
HideCaret();
Invalidate();
UpdateWindow();
//更新插入符位置
HDC hdc;
POINT pt;
TEXTMETRIC tm;
hdc = ::GetDC(m_hWnd);
GetTextMetrics(hdc,&tm);
::GetCaretPos(&pt);
pt.x += tm.tmAveCharWidth;
::ReleaseDC(m_hWnd,hdc);
::SetCaretPos(pt.x,pt.y);
ShowCaret();
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CCursorView::OnInitialUpdate()
{
CView::OnInitialUpdate();
::SetWindowText(m_hWnd,TEXT(""));
}
void CCursorView::OnDraw(CDC* pDC)
{
CCursorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
LPTSTR pText;
int iLen;
iLen = ::GetWindowTextLength(m_hWnd);
pText = (LPTSTR)malloc(sizeof(TCHAR) * (iLen+1));
memset(pText,0,sizeof(TCHAR) * (iLen+1));
::GetWindowText(m_hWnd,pText,iLen+1);
pDC->TextOut(0,0,pText);
}Top
6 楼yaozijian110()回复于 2004-09-03 14:20:23 得分 0
你自己编程的时候,要考虑各个字符的宽度不一样,输入不同的字符时候插入符(所谓的光标的正确名字应该是“插入符”)位置的改变是不一样的;要处理回车;要考虑窗口大小的改变;要处理Delete,回车,光标移动键等键的作用;等等。Top
7 楼tyboy007(tyboy007)回复于 2004-09-04 08:29:05 得分 0
Thanks!Top




