实现的方法各有千秋,你为了好看,可以贴图上去,什么都可以。但是大多都有一个中心思想,那就是处理消息。一个是输入消息,一个是删除消息。我实现的这个很简单,贴出来做参考,只实现了输入,与删除,还有Spinbox的加减实现。
-----------------头文件很简单,还没考虑好,为了习惯性还是贴吧 可以放在Cpp文件中
/2012/9/13 15:27:47
//IPSpinBox header file
#include "SystemMenu.h"
#define CLASS_CTRL_IPSPINBOX "IPSpinBox"
#define IDC_SPINBOX_IP 200
-------------------cpp文件---------------------------------------
/ define my IPSpinBox control
/2012/9/13 15:26:32
/
#include "IPSpinBox.h"
static WNDPROC old_spinbox_proc;
static int iAddr1,iAddr2,iAddr3,iAddr4;
static int iCursor = 0;
static RECT rect;
static RECT rcIP1,rcIP2,rcIP3,rcIP4;
/计算鼠标点击在那个区域
static int ParseClickRect(int x,int y)
{
if(y > rcIP1.bottom && y < rcIP1.top )
{
iCursor = 0;
return 1;
}
if(x > rcIP1.left && x < rcIP1.right)
{
iCursor = 1;
}
else if(x > rcIP2.left && x < rcIP2.right)
{
iCursor = 2;
}
else if(x > rcIP3.left && x < rcIP3.right)
{
iCursor = 3;
}
else if(x > rcIP4.left && x < rcIP4.right)
{
iCursor = 4;
}
return 0;
}
static int DrawIPAdress(HDC hdc)
{
char buf[20];
PLOGFONT logFont = CreateLogFont ("type1", "arial", "GB2312",
FONT_WEIGHT_BOOK | FONT_WEIGHT_BOLD,
FONT_SLANT_ROMAN, FONT_FLIP_NIL,
FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE,
30, 0);
SelectFont(hdc,logFont);
SetTextCharacterExtra(hdc,2);
if(iCursor == 0 )
{
sprintf(buf,"%3d.%3d.%3d.%3d",iAddr1,iAddr2,iAddr3,iAddr4);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,20,10,buf);
}
else if(iCursor == 1)
{
sprintf(buf,"%3d",iAddr1);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,100));
TextOut(hdc,20,10,buf);
int x = strlen(buf)*(GetSysCharWidth()+GetTextCharacterExtra(hdc)) + 20 ;
sprintf(buf,".%3d.%3d.%3d",iAddr2,iAddr3,iAddr4);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,x,10,buf);
}
else if(iCursor == 2)
{
sprintf(buf,"%3d.",iAddr1);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,20,10,buf);
int x = strlen(buf)*(GetSysCharWidth()+GetTextCharacterExtra(hdc)) + 20 ;
sprintf(buf,"%3d",iAddr2);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,100));
TextOut(hdc,x,10,buf);
x += strlen(buf)*GetSysCharWidth() + GetTextCharacterExtra(hdc);
sprintf(buf,".%3d.%3d",iAddr3,iAddr4);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,x,10,buf);
}
else if(iCursor == 3)
{
sprintf(buf,"%3d.%3d.",iAddr1,iAddr2);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,20,10,buf);
int x = strlen(buf)*(GetSysCharWidth()+GetTextCharacterExtra(hdc)) + 20 ;
sprintf(buf,"%3d",iAddr3);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,100));
TextOut(hdc,x,10,buf);
x += strlen(buf)*GetSysCharWidth()+GetTextCharacterExtra(hdc);
sprintf(buf,".%3d",iAddr4);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,x,10,buf);
}
else
{
sprintf(buf,"%3d.%3d.%3d.",iAddr1,iAddr2,iAddr3);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,204));
TextOut(hdc,20,10,buf);
int x = strlen(buf)*(GetSysCharWidth()+GetTextCharacterExtra(hdc)) + 20 ;
sprintf(buf,"%3d",iAddr4);
SetBkColor(hdc,RGB2Pixel(hdc,63,72,100));
TextOut(hdc,x,10,buf);
}
DestroyLogFont(logFont);
}
static int BackSpaceValue(int num)
{
if(num > 100)
{
int x = num/100;
int y = (num - x*100)/10;
return x*10+y;
}
else if(num >10)
{
int x = num/10;
return x;
}
else
{
return 0;
}
}
static int IPSpinboxControlProc(HWND hwnd,int message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case MSG_PAINT:
{
HDC hdc = BeginPaint(hwnd);
RECT rect;
GetClientRect(hwnd,&rect);
SetBrushColor(hdc,RGB2Pixel(hdc,63,72,204));
FillBox(hdc,rect.left,rect.top,rect.right,rect.bottom);
DrawIPAdress(hdc);
EndPaint(hwnd,hdc);
break;
}
case CB_SPIN:
{
if(iCursor <= 1)
{
if(wParam == 0)
{
if(iAddr1 >= 255) iAddr1 = -1;
++iAddr1;
}
else
{
if(iAddr1 <= 0) iAddr1 = 256;
--iAddr1;
}
}
else if(iCursor == 2)
{
if(wParam == 0)
{
if(iAddr2 >= 255) iAddr2 = -1;
++iAddr2;
}
else
{
if(iAddr2 <= 0) iAddr2 = 256;
--iAddr2;
}
}
else if(iCursor == 3)
{
if(wParam == 0)
{
if(iAddr3 >= 255) iAddr3 = -1;
++iAddr3;
}
else
{
if(iAddr3 <= 0) iAddr3 = 256;
--iAddr3;
}
}
else if(iCursor ==4)
{
if(wParam == 0)
{
if(iAddr4 >= 255) iAddr4 = -1;
++iAddr4;
}
else
{
if(iAddr4 <= 0) iAddr4 = 256;
--iAddr4;
}
}
SendMessage(hwnd,MSG_PAINT,wParam,lParam);
return 0;
}
case MSG_KEYDOWN:
{
if(wParam == SCANCODE_BACKSPACE)
{
int iOne,iTen,iHun;
switch(iCursor)
{
case 0:
case 1:
{
iAddr1 = BackSpaceValue(iAddr1);
break;
}
case 2:
{
iAddr2 = BackSpaceValue(iAddr2);
break;
}
case 3:
{
iAddr3 = BackSpaceValue(iAddr3);
break;
}
case 4:
{
iAddr4 = BackSpaceValue(iAddr4);
break;
}
}
}
SendMessage(hwnd,MSG_PAINT,wParam,lParam);
return 0;
}
case MSG_CHAR:
{
if((wParam >= 'A' && wParam <= 'Z' && wParam) || (wParam >= 'a' && wParam <= 'z')) return 0;
else if(wParam >= '0' && wParam <= '9')
{
switch(iCursor)
{
case 0:
case 1:
{
if(iAddr1 >100)
{
return 0;
}
if((iAddr1 = iAddr1*10+(int)wParam - 48) > 255)
iAddr1 = 255;
break;
}
case 2:
{
if(iAddr2 >100)
{
return 0;
}
if((iAddr2 = iAddr2*10+(int)wParam - 48) > 255)
iAddr2 = 255;
break;
}
case 3:
{
if(iAddr3 >100)
{
return 0;
}
if((iAddr3 = iAddr3*10+(int)wParam - 48) > 255)
iAddr3 = 255;
break;
}
case 4:
{
if(iAddr4 >100)
{
return 0;
}
if((iAddr4 = iAddr4*10+(int)wParam - 48) > 255)
iAddr4 = 255;
break;
}
}
}
SendMessage(hwnd,MSG_PAINT,wParam,lParam);
return 0;
}
case MSG_LBUTTONDOWN:
{
int pos_x,pos_y;
pos_x = LOWORD(lParam);
pos_y = HIWORD(lParam);
ParseClickRect(pos_x,pos_y);
SendMessage(hwnd,MSG_PAINT,wParam,lParam);
break;
}
}
return (*old_spinbox_proc)(hwnd,message,wParam,lParam);
}
HWND CreateIPSpinBoxWindow(const char* spCation,int id,int x,int y,int w,int h,HWND hParentWnd,DWORD dwAddData)
{
iAddr1 = iAddr2 = iAddr3 = iAddr4 = 0;
HWND hwnd = CreateWindow(CTRL_COMBOBOX,spCation,WS_VISIBLE | WS_CHILD | CBS_AUTOSPIN | CBS_AUTOLOOP,id,x,y,w,h,hParentWnd,dwAddData);
HDC hdc;
HWND hwnd_edit,hwnd_list;
SendMessage(hwnd,CB_GETCHILDREN,(WPARAM)&hwnd_edit,(LPARAM)&hwnd_list);
EnableWindow(hwnd_edit,false);
hdc = GetClientDC(hwnd_edit);
GetClientRect(hwnd_edit,&rect);
rcIP1.top = rcIP2.top = rcIP3.top = rcIP4.top = rect.top;
rcIP1.bottom = rcIP2.bottom = rcIP3.bottom = rcIP4.bottom = rect.bottom;
rcIP1.left = rect.left;
rcIP1.right = rect.right/4;
rcIP2.left = rect.right/4;
rcIP2.right = rect.right/2;
rcIP3.left = rect.right/2;
rcIP3.right = rect.right - rect.right/3;
rcIP4.left = rect.right - rect.right/3;
rcIP4.right = rect.right;
SetWindowBkColor(hwnd_edit,RGB2Pixel(hdc,63,72,204));
old_spinbox_proc = SetWindowCallbackProc(hwnd,IPSpinboxControlProc);
ReleaseDC(hdc);
}
static DLGTEMPLATE DlgYourTaste =
{
WS_BORDER,
WS_EX_NOCLOSEBOX ,
0, 0, PROGRAM_WIDTH, PROGRAM_HEIGHT,
#ifdef _LANG_ZHCN
"mywindow 欢迎",
#else
"hello",
#endif
0, 0,
0, NULL,
0
};
//dialog control data
static CTRLDATA CtrlYourTaste[] =
{
};
//dialog proc
static int DialogBoxProc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case MSG_INITDIALOG:
{
CreateIPSpinBoxWindow("IP",IDC_SPINBOX_IP,50,50,200,40,hDlg,0);
break;
}
case MSG_PAINT:
{
HDC hdc;
hdc = BeginPaint(hDlg);
Rectangle(hdc,50,50,250,90);
EndPaint(hDlg,hdc);
return 0;
}
case MSG_COMMAND:
{
switch(wParam)
{
}
}
case MSG_MOUSEMOVE:
{
break;
}
case MSG_TIMER:
{
break;
}
}
return DefaultDialogProc (hDlg, message, wParam, lParam);
}
int MiniGUIMain (int argc, const char* argv[])
{
#ifdef _MGRM_PROCESSES
JoinLayer(NAME_DEF_LAYER , "helloworld" , 0 , 0);
#endif
if (!InitMiniGUIExt())
return -1;
DlgYourTaste.controls = CtrlYourTaste;
DialogBoxIndirectParam (&DlgYourTaste, HWND_DESKTOP, DialogBoxProc, 0L);
return 0;
}
贴图就算了,感觉日志加图好麻烦,能不能改哈呢。 直接想QQ粘贴就,那多好。