自定义IP控件添加到MINIGUI源码中

来源:互联网 发布:linux查看日志文件 编辑:程序博客网 时间:2024/06/04 18:04

刚开始接触minigui,需要用到很多的自定义控件,原有的控件有限,所以需要将自定义控件添加到源码中。

使用MINIGUI1.6.10  自定义控件IP

 

添加方法:

1、首先编写IP控件(最后附加程序)

2、修改configure

 

    $ vim configure

 

    仿照edit 在963行添加 enable_ctrlipedit

 

               在1707行添加   --enable-ctrlipedit      include IPEDIT control <default=yes>

 

               在9778行添加 build_ctrl_ipedit="yes"

 

               在10223行添加     # Check whether --enable-ctrlipedit was given.
                                      if test "${enable_ctrlipedit+set}" = set; then :
                                      enableval=$enable_ctrlipedit; build_ctrl_ipedit=$enableval
                                      fi


               在12252行添加    if test "x$build_ctrl_ipedit" = "xyes"; then
                                     
                                     $as_echo "#define _CTRL_IPEDIT 1" >>confdefs.h
                                     
                                     fi
3、 修改configure.in

 

     $ vim configure.in

 

     在249行添加  build_ctrl_ipedit="yes"

 

     在560行添加  AC_ARG_ENABLE(ctrlipedit,
                      [  --enable-ctrlipedit      include IPEDIT control <default=yes>],
                      build_ctrl_ipedit=$enableval)

 

    在2003行添加  if test "x$build_ctrl_ipedit" = "xyes"; then
                       AC_DEFINE(_CTRL_IPEDIT, 1,
                       [Define if include IPEDIT control])
                       fi

 

4、 $ vim mgconfig.h

 

     添加 #define _CTRL_IPEDIT 1

 

5、 $ vim src/gui/ctrlclass.c

 

    添加  #ifdef _CTRL_IPEDIT
           extern BOOL RegisterIpEditControl(void);
           #endif

 

    添加 #ifdef _CTRL_IPEDIT
           if (!RegisterIpEditControl())
              return FALSE;
           #endif

 

6、 $ vim include/ctrl/Makefile.am

 

    在libminiguiinclude_HEADERS= 下添加ipedit.h

 

7、  $ vim include/ctrl/Makefile.in

 

      仿照edit.h的添加方式添加ipedit.h

 

8、 $ vim src/include/control/Makefile.am

  

      添加ipedit_impl.h

 

9、 $ vim src/include/control/Makefile.in

 

     noinst_HEADERS = 下添加ipedit_impl.h

 

10 、$ vim src/control/Makefile.am

 

     添加SRC_FILES = ctrlmisc.c ipedit.c/

 

11、$ vim src/control/Makefile.in

     SRC_FILES = ctrlmisc.c ipedit.c/

12 $ vim src/include/control.h  

 

     #ifdef _CTRL_IPEDIT
     #include "ctrl/ipedit.h"
     #endif

 

 

make

make install

include/ctrl/ipedit.h代码

 

#define CTRL_IPEDIT "ipedit"


#define  MSG_GETIPADDRESS  0x0801

#define  MSG_SETIPADDRESS  0x0802

#define  MSG_CLEARIPADDRESS 0x0803

 

 

 

src/include/control/ipedit_impl.h代码

 

 


#ifdef  __cplusplus
extern  "C" {
#endif

#define TOTALITEM   4 /* 编辑框的个数 */
#define POINTWIDTH   4 /* 圆点的宽度 */
#define POINTHEIGHT  4 /* 圆点的高度 */
#define ITEMDIS    8 /* 两个编辑框之间的间距  */


typedef  struct tagIPEDIT
{
 int lWidth;    /* 编辑框的宽度 */
 int lHeight;    /* 编辑框的高度 */
 int lItemSel;    /* 当前选中的编辑框 (0 - 3) */

 char IpBuffer[4][4];  /* 存放各个编辑框的IP地址 */

 HWND ahEdit[4];   /* 编辑框的句柄  */
 
   
}IPEDIT;
typedef IPEDIT *PIPEDIT;

BOOL RegisterIpEditControl (void);

#ifdef  __cplusplus
}
#endif

 

 

 

src/control/ipedit.c代码

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "minigui.h"
#include "gdi.h"
#include "window.h"
#include "ctrl/ctrlhelper.h"
#include "ctrl/ipedit.h"
#include "ctrl/edit.h"
#include "cliprect.h"
#include "internals.h"
#include "ctrlclass.h"
#include "ctrlmisc.h"
#include "ipedit_impl.h"


static WNDPROC OldEditProc = NULL;

static int IDC_IPEDIT[] = {50, 51, 52, 53};

static int IpEditCtrlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam);

BOOL RegisterIpEditControl (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = CTRL_IPEDIT;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = GetWindowElementColor (BKC_EDIT_DEF);
    WndClass.WinProc     = IpEditCtrlProc;

    return AddNewControlClass (&WndClass) == ERR_OK;
}


static void GetSelItem(HWND hWnd, IPEDIT *pstData, int lX,  int lY)
{
 int i = 0;
 LONG lLeft = 0;
 RECT stRect;
 RECT stRcClient;

 memset(&stRect, 0, sizeof(RECT));
 memset(&stRcClient, 0, sizeof(RECT));

 GetClientRect(hWnd, &stRcClient);
 
 
 for (i = 0; i < TOTALITEM; i++)
 {
  lLeft = stRcClient.left + i * (pstData->lWidth + ITEMDIS);
  SetRect(&stRect,  lLeft, stRcClient.top, lLeft + pstData->lWidth, stRcClient.top + pstData->lHeight);
  if (TRUE == PtInRect(&stRect, lX, lY))
  {
   pstData->lItemSel = i;
   return;
  }
 }

 return;
}


static int OldEditBox(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
 HWND hParent = 0;
 hParent = GetParent(hWnd);
 char Buf[4] = {'0'}; 
 int lLen = 0;
 int lCaretPos = 0;
 int i = 0;
 int lIpNum = 0;

 IPEDIT *pstData = (IPEDIT *) GetWindowAdditionalData2(hParent);
 if (NULL == pstData)
 {
  fprintf(stderr, "OldEditBox pstData NULL/n");
  return -1;
 }
 
 switch(message)
 {
  case MSG_CHAR:
  {
   if ((wParam >= '0') && (wParam <= '9'))
   {
    if (pstData->lItemSel > 3)
    {
     pstData->lItemSel = 3;
     return -1;
    }
    
    (*OldEditProc)(hWnd, message, wParam, lParam);
    SendMessage(pstData->ahEdit[pstData->lItemSel], MSG_GETTEXT, (WPARAM)3, (LPARAM) &Buf);
    lLen = strlen(Buf);
    
    if (3 == lLen)
    {
     lIpNum = atoi(Buf);
     if (lIpNum > 255)
     {
      SendMessage(pstData->ahEdit[pstData->lItemSel], MSG_SETTEXT, 0, (LPARAM)"255");
      if (pstData->lItemSel < 3)
      {
       /* 编辑框中插入符的使用比较混乱,在这边控制插入符的隐藏 */
       HideCaret(pstData->ahEdit[pstData->lItemSel]);
      }
     }
 
     pstData->lItemSel++;
     if (pstData->lItemSel <= 3)
     {
      SetFocusChild(pstData->ahEdit[pstData->lItemSel]);
      SendMessage(pstData->ahEdit[pstData->lItemSel], MSG_SETTEXT, 0, (LPARAM)"");
     }
    }
   }
   else if ((127 == wParam) || ('/b' == wParam))
   {
    (*OldEditProc)(hWnd, message, wParam, lParam);
    lLen = SendMessage(pstData->ahEdit[pstData->lItemSel], MSG_GETTEXTLENGTH, 0, 0);
    lCaretPos = SendMessage(pstData->ahEdit[pstData->lItemSel], EM_GETCARETPOS, 0, 0);

    if ((0 == lCaretPos) || (0 == lLen))
    {
     /* 编辑框中插入符的使用比较混乱,在这边控制插入符的隐藏 */
     HideCaret(pstData->ahEdit[pstData->lItemSel]);
     pstData->lItemSel--;
     if (pstData->lItemSel < 0)
     {
      pstData->lItemSel = 0;
     }
     SetFocusChild(pstData->ahEdit[pstData->lItemSel]);
    }
   }

   for (i = 0; i < TOTALITEM; i++)
   {
    lLen = SendMessage(pstData->ahEdit[i], MSG_GETTEXTLENGTH, 0, 0);
    SendMessage(pstData->ahEdit[i], EM_SETCARETPOS, 0, (LPARAM)lLen);
   }
   return 0;
  }

  case MSG_KEYDOWN:
  {
   if (pstData->lItemSel > 3)
   {
    pstData->lItemSel = 3;
   }
   
   lCaretPos = SendMessage(pstData->ahEdit[pstData->lItemSel], EM_GETCARETPOS, 0, 0);
   lLen = SendMessage(pstData->ahEdit[pstData->lItemSel], MSG_GETTEXTLENGTH, 0, 0);

   switch (LOWORD (wParam))
   {
    case SCANCODE_CURSORBLOCKLEFT:
    {
     if ((0 == lCaretPos) || (0 == lLen))
     {
      pstData->lItemSel--;
      if (pstData->lItemSel < 0)
      {
       pstData->lItemSel = 0;
      }
      SetFocusChild(pstData->ahEdit[pstData->lItemSel]);
      return 0;
     }
     else
     {
      break;
     }
    }
    
    case SCANCODE_CURSORBLOCKRIGHT:
    {
     if ((lCaretPos == lLen) || (3 == lCaretPos))
     {
      pstData->lItemSel++;
      if (pstData->lItemSel > 3)
      {
       pstData->lItemSel = 3;
      }
      SetFocusChild(pstData->ahEdit[pstData->lItemSel]);
      return 0;
     }
     else
     {
      break;
     }
    }

    default:
    {
     break;
    }
   }
  }

  default:
  {
   break;
  }
 }
 return (*OldEditProc)(hWnd, message, wParam, lParam);
}


static int IpEditCtrlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
{
 HDC       hdc = 0;
 PCONTROL  pstCtrl = NULL;
 int i = 0;
 DWORD ulStyle = GetWindowStyle(hwnd);
 pstCtrl = Control (hwnd);
 if (NULL == pstCtrl)
 {
  printf("pstCtrl NULL /n");
  return -1;
 }
 IPEDIT *pstData = (IPEDIT *) pstCtrl->dwAddData2;
 
 switch (message)
 {
  case MSG_CREATE:
  {
   RECT stRcClient;
   DWORD ulEditStyle = 0;
   
   memset(&stRcClient, 0, sizeof (stRcClient));
   GetClientRect(hwnd, &stRcClient);

   if (0 != ( WS_DISABLED &ulEditStyle))
   {
    ulEditStyle |= WS_DISABLED;
   }
   
   pstData = (PIPEDIT) malloc(sizeof(IPEDIT));
   if (NULL == pstData)
   {
    printf("ipedit control malloc failed/n");
    return -1;
   }
   pstData->lHeight = RECTH(stRcClient);
   pstData->lWidth = (RECTW(stRcClient) - 3 * ITEMDIS) /TOTALITEM;
   pstData->lItemSel = 0;

   for (i = 0; i < TOTALITEM; i++)
   {
    pstData->ahEdit[i] = CreateWindow (CTRL_SLEDIT,
          "",
                                 WS_CHILD | WS_VISIBLE | ES_CENTER | ulEditStyle,
                                 IDC_IPEDIT[i],
                                 i * (pstData->lWidth + ITEMDIS), 0, pstData->lWidth, pstData->lHeight,
                                 hwnd, 0);
    SendMessage(pstData->ahEdit[i], EM_LIMITTEXT, (WPARAM)3, 0);
    OldEditProc = SetWindowCallbackProc (pstData->ahEdit[i], OldEditBox);
   }
   
   pstCtrl->dwAddData2 = (DWORD)pstData;
   break;
  }
 
   
  case MSG_DESTROY:
  {
   if (NULL != pstData)
   {
    free (pstData);
    pstData = NULL;
   }
   break;
  }


  case MSG_PAINT:
  {   
   hdc = BeginPaint(hwnd);

   SetBrushColor(hdc, 0);
   
   for (i = 0; i < 3; i++)
   {
    FillBox(hdc, (ITEMDIS * i + (i +1) * pstData->lWidth + 2), (3 * pstData->lHeight / 5), POINTWIDTH, POINTHEIGHT);
   }
   
   EndPaint (hwnd, hdc);
   return 0;
  }

  case MSG_ERASEBKGND:
  {
   EditOnEraseBackground (hwnd, (HDC) wParam, (const RECT*) lParam);
          return 0;
  }

  case MSG_LBUTTONDOWN:
  {
   int lX = LOSWORD(lParam);
              int lY = HISWORD(lParam);

   GetSelItem(hwnd, pstData, lX, lY);

   break;
  }

  case MSG_GETIPADDRESS:
  {
   char Buffer[4];
   
   for (i = 0; i < TOTALITEM; i++)
   {
    SendMessage(pstData->ahEdit[i], MSG_GETTEXT, (WPARAM)3, (LPARAM)&Buffer);
    memcpy(pstData->IpBuffer[i], &Buffer, sizeof (Buffer));
   }
   sprintf(lParam, "%s.%s.%s.%s", pstData->IpBuffer[0],pstData->IpBuffer[1],
           pstData->IpBuffer[2], pstData->IpBuffer[3]);
   break;
  }

  case MSG_SETIPADDRESS:
  {
   int IpNum[4] = {0};
   int lLen = 0;
   
   sscanf(lParam, "%d.%d.%d.%d", &IpNum[0], &IpNum[1],&IpNum[2],&IpNum[3]);
   for (i = 0; i < TOTALITEM; i++)
   {
    if (IpNum[i] > 255)
    {
     sprintf(pstData->IpBuffer[i], "%d", 255);
    }
    else
    {
     sprintf(pstData->IpBuffer[i], "%d", IpNum[i]);
    }
    lLen = strlen(pstData->IpBuffer[i]);
    SendMessage(pstData->ahEdit[i], MSG_SETTEXT, 0, (LPARAM) pstData->IpBuffer[i]);
    SendMessage(pstData->ahEdit[i], EM_SETCARETPOS, 0, (LPARAM)lLen);
    
    HideCaret(pstData->ahEdit[i]);
   }

   break;
  }

  case MSG_ENABLE:
  {
   if (((0 == (ulStyle & WS_DISABLED)) && (FALSE == (BOOL) wParam))
                  || ((0 != (ulStyle & WS_DISABLED)) && (TRUE == wParam)))
   {
    if (TRUE == wParam)
    {
     ExcludeWindowStyle(hwnd,WS_DISABLED);
    }
    else
    {
     IncludeWindowStyle(hwnd,WS_DISABLED);
    }
    
    for (i = 0; i < TOTALITEM; i++)
    {
     SendMessage(pstData->ahEdit[i], MSG_ENABLE, wParam, 0);
    }

               InvalidateRect (hwnd, NULL, TRUE);
   }
   break;
  }

  case MSG_CLEARIPADDRESS:
  {
   for (i = 0; i < TOTALITEM; i++)
   {
    sprintf(pstData->IpBuffer[i], "%d", 0);
    SendMessage(pstData->ahEdit[i], MSG_SETTEXT, 0, (LPARAM) "");
    HideCaret(pstData->ahEdit[i]);
   }
   break;
  }
  
  default:
  {
   break;
  }

     
    }
   
    return DefaultControlProc (hwnd, message, wParam, lParam);
}

如有疑问加QQ:303340110 转载请通知

原创粉丝点击