📄 digiedit.c
字号:
/******************************************************************************
*
* Copyright 2006 National ASIC Center, All right Reserved
*
* FILE NAME: DigiEdit.c
* PROGRAMMER: ming.c
* Date of Creation: 2006/08/8
*
* DESCRIPTION:
* DigiEdit
* NOTE:
*
* FUNCTIONS LIST:
* -----------------------------------------------------------------------------
*
* -----------------------------------------------------------------------------
*
* MODIFICATION HISTORY
* LastModify 2006/10/30
******************************************************************************/
#include "mingui.h"
//---------------------------------------------------------------------------
#define MaxDigiWidth 8
//---------------------------------------------------------------------------
typedef struct t_DigiEdit
{ int CaretStart,CaretWidth,CaretHeight,CaretTop,CaretLeft,subfocus;
float min,max,value;
char DigiWidth,fbitcount,Digit[MaxDigiWidth];
}TDigiEdit;
//---------------------------------------------------------------------------
void DigiEditDataSync(TDigiEdit *edt,BOOL direction)
{ int i,d,dw;
float fd;
dw=(edt->fbitcount>0)? edt->DigiWidth-edt->fbitcount-1:edt->DigiWidth;
if(direction==true) // edt->Digit[] --> edt->Value
{ for(i=0,d=0;i<dw;i++)
{ d=d*10+edt->Digit[i]-'0';
}
for(i=0,fd=0;i<edt->fbitcount;i++)
{ fd=fd+edt->Digit[edt->DigiWidth-i-1]-'0';
fd=fd*(float)0.1;
}
edt->value=d+fd;
}
else // edt->Value --> edt->Digit[]
{ d=(int)edt->value;
fd=edt->value-d;
for(i=0;i<dw;i++)
{ edt->Digit[dw-i-1]='0'+(d % 10);
d=d/10;
}
if(edt->fbitcount>0)
{ edt->Digit[dw]='.';
for(i=0;i<edt->fbitcount;i++)
{ fd=fd*10;
edt->Digit[dw+i+1]='0'+((int)fd % 10);
}
}
edt->Digit[edt->DigiWidth]='\0';
}
}
//---------------------------------------------------------------------------
void DigiEdit_Write(HWND hWnd,float value)
{ if(hWnd)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
if(edt)
{ edt->value=value;
DigiEditDataSync(edt,false);
Invalidate(hWnd);
}
}
}
//---------------------------------------------------------------------------
float DigiEdit_Read(HWND hWnd)
{ if(hWnd)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
if(edt)
{ DigiEditDataSync(edt,true);
return edt->value;
}
}
return 0;
}
//---------------------------------------------------------------------------
void DigiEditFormat(HWND obj,TDigiEdit *edt,char *fmt)
{ bool Digi=true;
char ch;
float data[3]={0,0,0},fbit;
int i,d,dotcount,fbitcount[3]={0,0,0};
if(!fmt || !edt)return;
for(i=0;i<3;i++)
{ for(ch=*fmt;ch==32;ch=*++fmt);
for(ch=*fmt,dotcount=0,fbit=(float)0.1;Digi&&ch!=32&&ch!=0;ch=*++fmt)
{ if(ch>='0' && ch<='9')
{ if(dotcount==0) data[i]=data[i]*10 + ch-'0';
else if (dotcount==1)
{ data[i]=data[i] + fbit*(ch-'0');
fbit*=(float)0.1;
fbitcount[i]++;
}
}
else if(ch=='.') dotcount++;
else Digi=false;
}
}
if(!Digi)
{ edt->min=0;
edt->max=9;
edt->value=0;
edt->fbitcount=0;
edt->DigiWidth=1;
}
else
{ if(fbitcount[0]>=fbitcount[1] && fbitcount[0]>=fbitcount[2])
edt->fbitcount=fbitcount[0];
else if(fbitcount[1]>=fbitcount[0] && fbitcount[1]>=fbitcount[2])
edt->fbitcount=fbitcount[1];
else edt->fbitcount=fbitcount[2];
edt->min=data[0];
edt->max=data[1];
edt->value=data[2];
if(edt->value>=edt->max)edt->value=edt->max-1; /*保留1个字节作为结束符*/
else if(edt->value<edt->min)edt->value=edt->min;
d=(int)edt->max;
edt->DigiWidth=(edt->fbitcount)?edt->fbitcount+1:0;
if(edt->DigiWidth>MaxDigiWidth)edt->DigiWidth=MaxDigiWidth;
do
{ edt->DigiWidth++;
d=d/10;
} while(d>0);
DigiEditDataSync(edt,false);
}
}
//---------------------------------------------------------------------------
void DigiEditCreate(HWND hWnd)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
memset(edt,0,sizeof(TDigiEdit));
DigiEditFormat(hWnd,edt,(char *)WndExtraData(hWnd));
edt->CaretWidth=SYS_FONT_WIDTH+2;
edt->CaretHeight=SYS_FONT_HEIGHT;
edt->CaretTop = (crHeight(hWnd) - edt->CaretHeight)/2;
edt->CaretStart = ( crWidth(hWnd) - edt->DigiWidth*(SYS_FONT_WIDTH+1) +1 )/2;
edt->CaretLeft=edt->CaretStart-1;
}
//---------------------------------------------------------------------------
void DigiEdit_Repaint(HWND hWnd)
{ HDC dc=BeginPaint(hWnd);
int textx;
TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
char *pDataText=edt->Digit;
if(!WndGetAttr(hWnd,WS_FOCUS))
{ int digitcount=edt->DigiWidth;
while(*pDataText=='0' && *(pDataText+1)!='.')
{ pDataText++;
digitcount--;
}
textx= (crWidth(hWnd)-digitcount*SYS_FONT_WIDTH)/2;
}
else
{ textx=edt->CaretStart;
SetColSpace(dc,1);
}
TextOut(dc,textx,edt->CaretTop,pDataText);
EndPaint(hWnd);
}
//---------------------------------------------------------------------------
void DigiEditChangeSubFocus(HWND hWnd,BOOL direction)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
if(direction)
{ edt->subfocus++;
if(edt->Digit[edt->subfocus]=='.')edt->subfocus++;
}
else
{ edt->subfocus--;
if(edt->Digit[edt->subfocus]=='.')edt->subfocus--;
}
edt->CaretLeft=edt->CaretStart+(SYS_FONT_WIDTH+1)*edt->subfocus-1;
SetCaretPos(hWnd,edt->CaretLeft,edt->CaretTop);
}
//---------------------------------------------------------------------------
void DigiEditChangeValue(HWND hWnd,BOOL increase)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
char textchar[2];
int tx=edt->CaretStart+(SYS_FONT_WIDTH+1)*edt->subfocus;
BOOL hide=HideCaret(hWnd);
HDC dc=GetClientDC(hWnd);
if(increase)
{ if(edt->Digit[edt->subfocus]<'9')edt->Digit[edt->subfocus]++;
else edt->Digit[edt->subfocus]='0';
}
else
{ if(edt->Digit[edt->subfocus]>'0')edt->Digit[edt->subfocus]--;
else edt->Digit[edt->subfocus]='9';
}
textchar[0]=edt->Digit[edt->subfocus];
textchar[1]='\0';
GroupOn(dc);
EraseBackground(dc,tx,edt->CaretTop,SYS_FONT_WIDTH,SYS_FONT_HEIGHT);
TextOut(dc,tx,edt->CaretTop,textchar);
GroupOff(dc,tx,edt->CaretTop,SYS_FONT_WIDTH,SYS_FONT_HEIGHT);
ReleaseDC(dc);
if(hide)ShowCaret(hWnd);
}
//---------------------------------------------------------------------------
void DigiEditGetChar(HWND hWnd,char keyvalue)
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
char textchar[2];
int tx=edt->CaretStart+(SYS_FONT_WIDTH+1)*edt->subfocus;
BOOL hide=HideCaret(hWnd);
HDC dc=GetClientDC(hWnd);
textchar[0]=keyvalue;
textchar[1]='\0';
GroupOn(dc);
EraseBackground(dc,tx,edt->CaretTop,SYS_FONT_WIDTH,SYS_FONT_HEIGHT);
TextOut(dc,tx,edt->CaretTop,textchar);
GroupOff(dc,tx,edt->CaretTop,SYS_FONT_WIDTH,SYS_FONT_HEIGHT);
ReleaseDC(dc);
if(hide)ShowCaret(hWnd);
}
//---------------------------------------------------------------------------
static HRESULT CALLBACK DigiEditProc(HWND hWnd,UINT Message,WPARAM WParam,LPARAM LParam)
{ switch(Message)
{
case WM_KEYDOWN:
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
switch(WParam)
{ case VK_UP:
DigiEditChangeValue(hWnd,false);
return 0;
case VK_DOWN:
DigiEditChangeValue(hWnd,true);
return 0;
case VK_RIGHT:
if(edt->subfocus<edt->DigiWidth-1)
{ DigiEditChangeSubFocus(hWnd,true);
return 0;
} else break; /*DefWindowProc() Will deal focus change*/
case VK_LEFT:
if(edt->subfocus>0)
{ DigiEditChangeSubFocus(hWnd,false);
return 0;
} else break;/*DefWindowProc() Will deal focus change*/
}
}
break;
case WM_CHAR:
{ if(WParam>='0' && WParam<='9')
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
if(edt->Digit[edt->subfocus]!=(char)WParam)
{ DigiEditGetChar(hWnd,(char)WParam);
edt->Digit[edt->subfocus]=(char)WParam;
if(edt->subfocus<edt->DigiWidth-1)
{ DigiEditChangeSubFocus(hWnd,true);
}
}
}
}
case WM_VSCROLL:
DigiEditChangeValue(hWnd,((int)LParam>0));
return 0;
case WM_LBUTTONDOWN:
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
int xpos=LOWORD(LParam),ypos=HIWORD(LParam);
if(PointInRect(xpos, ypos, &WNDPTR(hWnd)->ClientRect))
{ int newedtpos=( xpos-WNDPTR(hWnd)->ClientRect.left - edt->CaretStart ) / (SYS_FONT_WIDTH+1);
if(newedtpos<0)newedtpos=0;
else if(newedtpos>=edt->DigiWidth)newedtpos=edt->DigiWidth-1;
if( newedtpos != edt->subfocus && edt->Digit[newedtpos]!='.')
{ edt->subfocus=newedtpos;
edt->CaretLeft=edt->CaretStart+(SYS_FONT_WIDTH+1)*edt->subfocus-1;
SetCaretPos(hWnd,edt->CaretLeft,edt->CaretTop);
}
}
}
break;
case WM_CREATE:
DigiEditCreate(hWnd);
return 0;
case WM_PAINT:
DigiEdit_Repaint(hWnd);
return 0;
case WM_SETFOCUS:
{ TDigiEdit *edt=(TDigiEdit *)WndClsBuf(hWnd);
CreateCaret (hWnd, edt->CaretWidth, edt->CaretHeight);
SetCaretPos (hWnd,edt->CaretLeft, edt->CaretTop);
Invalidate(hWnd);
}
return 0;
case WM_KILLFOCUS:
DestroyCaret(hWnd);
Invalidate(hWnd);
return 0;
}
return DefWindowProc(hWnd,Message,WParam,LParam);
}
//---------------------------------------------------------------------------
void CM_RegisterDigiEdit(void)
{ TWNDCLASS wc;
memset(&wc,0,sizeof(wc));
wc.dwStyle=WS_BORDER_LOWERED;
wc.clForeground=CL_WINDOWTEXT;
wc.clBackground=CL_WHITE;
wc.cbWndExtra=sizeof(TDigiEdit);
wc.lpfnWndProc=DigiEditProc;
wc.lpszClassName="DigiEdit";
RegisterClass(&wc);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -