CEditBar.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:6k
- #include "stdafx.h"
- #include "CEditBar.h"
- #include "Common.h"
- extern void InputInitial(long x,long y,char *buffer,char Mode);
- extern void InputFinish(void);
- extern LRESULT HandleCompPaint(void);
- extern LRESULT HandleCandPaint(void);
- int CEditBar::Initiate(HWND hwndParent,LPTSTR lpstrBmp,int iCaretWidth,int iCaretHeight,int iMaxEditLen,LPRECT lprcRange,LPTSTR lpstr,BOOL bStar)
- {
- hwnd=hwndParent;
- sizeEdit.cx=0;sizeEdit.cy=0;
- rcEditPos.left=ptCaretPos.x=lprcRange->left;
- rcEditPos.top=ptCaretPos.y=lprcRange->top;
- rcEditPos.right=lprcRange->right;
- rcEditPos.bottom=lprcRange->bottom;
- CEditBar::iCaretWidth=iCaretWidth;
- CEditBar::iCaretHeight=iCaretHeight;
- bEnable=FALSE;
- bPassword=bStar;
- if(!(lpstrPassword=new TCHAR[iMaxEditLen+1]))
- return ERR_SYSTEM;
- for(int i=0;i<iMaxEditLen;i++)
- lpstrPassword[i]='*';
- if(!(lpstrEdit=new TCHAR[iMaxEditLen+1]))
- return ERR_SYSTEM;
- else
- iEditMaxLen=iMaxEditLen;
- if(lpstr)
- {
- if(iEditMaxLen<(iEditLen=lstrlen(lpstr)))
- iEditLen=iEditMaxLen;
- iEditPos=iEditLen;
- if(NULL==(lstrcpyn(lpstrEdit,lpstr,iEditLen+1)))
- {
- iEditLen=0;
- iEditPos=0;
- }
- }
- else
- {
- iEditLen=0;
- iEditPos=0;
- }
- cChina=0;
- SetNULL();
- if(lpstrBmp)
- {
- if(!(hBmp=(HBITMAP)LoadImage(GetModuleHandle(NULL),lpstrBmp,IMAGE_BITMAP,0,0,LR_LOADTRANSPARENT)))
- hBmp=(HBITMAP)LoadImage(NULL,lpstrBmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_LOADTRANSPARENT);
- }
- else
- hBmp=NULL;
- return ERR_NO;
- }
- void CEditBar::Release()
- {
- Disable();
- if(hBmp)
- {
- DeleteObject(hBmp);
- hBmp=NULL;
- }
- if(lpstrEdit)
- {
- delete[] lpstrEdit;
- lpstrEdit=NULL;
- }
- if(lpstrPassword)
- {
- delete[] lpstrPassword;
- lpstrPassword=NULL;
- }
- }
- void CEditBar::Enable()
- {
- InputInitial(rcEditPos.left,rcEditPos.top,lpstrEdit,0);
- bEnable=TRUE;
- }
- void CEditBar::Disable()
- {
- if(bEnable)
- {
- InputFinish();
- bEnable=FALSE;
- }
- }
- void CEditBar::PutChar(WPARAM wParam,int iRepeat)
- {
- if(!bEnable)
- return;
- for(int i=0;i<iRepeat;i++){
- switch(wParam){
- case 'b':
- DeleteBack();
- break;
- case 'x1B':
- iEditLen=0;
- iEditPos=0;
- SetNULL();
- break;
- case 't':
- case 'n':
- case 'r':
- break;
- default:
- SetChar((TCHAR)wParam);
- break;
- }
- }
- }
- void CEditBar::ControlEdit(WPARAM wParam)
- {
- if(!bEnable)
- return;
- switch (wParam)
- {
- case VK_HOME:
- iEditPos=0;
- break;
- case VK_END:
- iEditPos=iEditLen;
- break;
- /* case VK_PRIOR:
- yCaret=0;
- break;
- case VK_NEXT:
- yCaret=cyBuffer-1;
- break;
- */ case VK_LEFT:
- GetLeft();
- break;
- case VK_RIGHT:
- GetRight();
- break;
- /* case VK_UP:
- yCaret=max(yCaret-1,0);
- break;
- case VK_DOWN:
- yCaret=min(yCaret+1,cyBuffer-1);
- break;
- */ case VK_DELETE:
- DeleteChar();
- break;
- }
- }
- inline int CEditBar::SetCaretPos()
- {
- if(TRUE==::SetCaretPos(ptCaretPos.x+sizeEdit.cx,ptCaretPos.y))
- return ERR_DONE;
- return ERR_NO;
- }
- int CEditBar::ShowEdit(IDirectDrawSurface *pddsBack,IDirectDrawSurface *pdds,LPRECT lprcCaret,HFONT font,COLORREF colorEnable,COLORREF colorDisable,BOOL bShow)
- {
- HDC hdc;
- HRESULT hr=pddsBack->GetDC(&hdc);
- if(FAILED(hr))
- {
- if(DDERR_SURFACELOST==hr)
- {
- pddsBack->Restore();
- pddsBack->GetDC(&hdc);
- }
- else
- return ERR_SYSTEM;
- }
- if(font)
- fontOld=(HFONT)SelectObject(hdc,font);
- if(!GetTextExtentPoint32(hdc,lpstrEdit,iEditPos,&sizeEdit))
- {
- sizeEdit.cx=0;
- sizeEdit.cy=0;
- }
- if(bEnable){
- SetTextColor(hdc,colorEnable);
- }else{
- SetTextColor(hdc,colorDisable);
- }
- SetBkMode(hdc,TRANSPARENT);
- if(bPassword)
- DrawText(hdc,lpstrPassword,iEditLen,&rcEditPos,DT_NOCLIP | DT_EDITCONTROL | DT_EXPANDTABS | DT_NOPREFIX);
- else
- DrawText(hdc,lpstrEdit,-1,&rcEditPos,DT_NOCLIP | DT_EDITCONTROL | DT_EXPANDTABS | DT_NOPREFIX);
- if(fontOld)
- SelectObject(hdc,fontOld);
- hr=pddsBack->ReleaseDC(hdc);
- if(DDERR_SURFACELOST==hr)
- {
- pddsBack->Restore();
- pddsBack->ReleaseDC(hdc);
- }
- if(bEnable && bShow)
- {
- if(FAILED(hr=pddsBack->BltFast(ptCaretPos.x+sizeEdit.cx,ptCaretPos.y,pdds,lprcCaret,DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY)))
- {
- if(DDERR_SURFACELOST==hr)
- {
- pddsBack->Restore();
- pddsBack->BltFast(ptCaretPos.x+sizeEdit.cx,ptCaretPos.y,pdds,lprcCaret,DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
- }
- else
- return hr;
- }
- }
- if(bEnable)
- {
- HandleCompPaint();
- HandleCandPaint();
- }
- return ERR_NO;
- }
- void CEditBar::Clear()
- {
- iEditLen=0;
- iEditPos=0;
- SetNULL();
- }
- inline void CEditBar::SetChar(TCHAR Char)
- {
- int i;
- if(iEditLen>=iEditMaxLen)
- {
- cChina=0;
- return;
- }
- if(0!=cChina)
- {
- if(iEditLen>(iEditMaxLen-2))
- {
- cChina=0;
- return;
- }
- for(i=iEditLen;i>=iEditPos;i--)
- lpstrEdit[i+2]=lpstrEdit[i];
- lpstrEdit[iEditPos++]=cChina;
- lpstrEdit[iEditPos++]=Char;
- iEditLen+=2;
- cChina=0;
- }
- else
- {
- if(Char&0x80)
- {
- cChina=Char;
- return;
- }
- for(i=iEditLen;i>=iEditPos;i--)
- lpstrEdit[i+1]=lpstrEdit[i];
- lpstrEdit[iEditPos++]=Char;
- iEditLen++;
- }
- }
- inline void CEditBar::DeleteChar()
- {
- if(iEditPos>=iEditLen)
- return;
- int offset=1;
- if((lpstrEdit[iEditPos]&0x80)&&(iEditLen>=2))
- offset=2;
- for(int i=iEditPos;i<iEditLen;i++)
- lpstrEdit[i]=lpstrEdit[i+offset];
- iEditLen-=offset;
- }
- inline void CEditBar::DeleteBack()
- {
- int left;
- if(0==iEditPos)
- return;
- for(int i=0;i<iEditPos;i++)
- {
- left=i;
- if(lpstrEdit[i]&0x80)
- i++;
- }
- int offset=1;
- if((lpstrEdit[left]&0x80)&&(iEditLen>=2))
- offset=2;
- for(i=left;i<iEditLen;i++)
- lpstrEdit[i]=lpstrEdit[i+offset];
- iEditLen-=offset;
- iEditPos-=offset;
- }
- inline void CEditBar::SetNULL()
- {
- lpstrEdit[iEditLen]=NULL;
- }
- inline void CEditBar::GetLeft()
- {
- int left;
- if(0==iEditPos)
- return;
- for(int i=0;i<iEditPos;i++)
- {
- left=i;
- if(lpstrEdit[i]&0x80)
- i++;
- }
- iEditPos=left;
- }
- inline void CEditBar::GetRight()
- {
- if(iEditPos>=iEditLen)
- return;
- if(iEditPos<=(iEditLen-2))
- {
- if(lpstrEdit[iEditPos]&0x80)
- {
- iEditPos+=2;
- return;
- }
- }
- iEditPos+=1;
- }