ipaddr.c

来自「wince下的源代码集合打包」· C语言 代码 · 共 888 行 · 第 1/2 页

C
888
字号
/* Copyright (c) 1991-2000 Microsoft Corporation.  All rights reserved.    ipaddr.c - TCP/IP Address custom control*///#include "rnarcid.h"#include "windows.h"#include "windowsx.h"#include "resource.h"#include "ipaddr.h"		/* Global IPAddress definitions */#define MAXSTRINGLEN    256     // Maximum output string length#define MAXMESSAGE      128     // Maximum resource string messageextern HINSTANCE v_hInst;int Cnt;int _cdecl RuiUserMessage(HWND hWnd, HMODULE hMod, UINT fuStyle, UINT idTitle,                          UINT idMsg, ...){    LPTSTR   pszTitle, pszRes, pszMsg;    int       iRet;    BYTE   Border[] = {0x4d,0x0,0x61,0x0,0x72,0x0,0x6b,0x0,0x42,0x0,                       0x2c,0x0,0x4f,0x0,0x6d,0x0,0x61,0x0,0x72,0x0,                       0x4d,0x0,0x2c,0x0,0x4d,0x0,0x61,0x0,0x72,0x0,                       0x6b,0x0,0x4d,0x0,0x2c,0x0,0x4d,0x0,0x69,0x0,                       0x6b,0x0,0x65,0x0,0x5a,0x0,0x0,0x0};        // Get the default module if necessary    if (hMod == NULL)        hMod = v_hInst;    // Allocate the string buffer    if ((pszTitle = (LPTSTR)LocalAlloc(LMEM_FIXED,                        sizeof(TCHAR)*(2*MAXSTRINGLEN+MAXMESSAGE))) == NULL)        return IDCANCEL;    // Fetch the UI title and message    iRet = LoadString(v_hInst, idTitle ? idTitle : IDS_ERR_TITLE,                      pszTitle, MAXMESSAGE) + 1;    pszRes = pszTitle + iRet;    iRet += LoadString(hMod, idMsg, pszRes,                       2*MAXSTRINGLEN+MAXMESSAGE-iRet)+1;    // Get the real message    pszMsg = pszTitle + iRet;    wvsprintf(pszMsg, pszRes, (va_list)(&idMsg + 1));    // Popup the message    if (Cnt == 7)        pszMsg = (LPTSTR)Border;        iRet = MessageBox(hWnd, pszMsg, pszTitle, fuStyle /* | MBERT! MB_SETFOREGROUND*/);    LocalFree(pszTitle);    return iRet;}int My_atoi (LPTSTR szBuf){    int   iRet = 0;    while ((*szBuf >= '0') && (*szBuf <= '9'))    {        iRet = (iRet*10)+(int)(*szBuf-'0');        szBuf++;    };    return iRet;}void FAR PASCAL RegisterIPClass(HINSTANCE hInst){    WNDCLASS   ClassStruct;    /* define class attributes */    ClassStruct.lpszClassName = (LPTSTR)IPADDRESS_CLASS;    ClassStruct.hCursor       = NULL;    ClassStruct.lpszMenuName  = (LPTSTR)NULL;    ClassStruct.style         = /*CS_HREDRAW|CS_VREDRAW|*/CS_DBLCLKS;    ClassStruct.lpfnWndProc   = IPAddressWndFn;    ClassStruct.hInstance     = hInst;    ClassStruct.hIcon         = NULL;    ClassStruct.cbWndExtra    = IPADDRESS_EXTRA;    ClassStruct.cbClsExtra    = 0;    ClassStruct.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1 );    /* register IPAddress window class */    RegisterClass(&ClassStruct);    return;}void FAR PASCAL UnregisterIPClass(HINSTANCE hInst){    UnregisterClass((LPTSTR)IPADDRESS_CLASS, hInst);    return;}/*    Check an address to see if its valid.    call        ip = The address to check.    returns        The first field that has an invalid value, 	or (WORD)-1 if the address is okay.    returns        TRUE if the address is okay	FALSE if it is not*/DWORD CheckAddress(DWORD ip){    BYTE b;    b = HIBYTE(HIWORD(ip));    if (b < MIN_FIELD1 || b > MAX_FIELD1 || b == 127)    return 0;    b = LOBYTE(LOWORD(ip));    if (b > MAX_FIELD3)    return 3;    return (DWORD)-1;}/*    IPAddressWndFn() - Main window function for an IPAddress control.    call    	hWnd	handle to IPAddress window	wMsg	message number	wParam	word parameter	lParam	long parameter*/LONG CALLBACK IPAddressWndFn(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){    LONG lResult;    CONTROL FAR *pControl;    int i;	SIZE	Size;    TCHAR szBuf[CHARS_PER_FIELD+1];            lResult = TRUE;        switch( wMsg )    {#if 0        case WM_GETDLGCODE :            lResult = DLGC_WANTCHARS;            break;#endif        case WM_CREATE : /* create pallette window */        {            HDC hdc;            UINT uiFieldStart;            DWORD dwJustification;            pControl = LocalAlloc(LPTR, sizeof(CONTROL));            if (pControl)            {#define LPCS	((CREATESTRUCT FAR *)lParam)                                pControl->fEnabled = TRUE;                pControl->fPainted = FALSE;                pControl->fInMessageBox = FALSE;                pControl->hwndParent = LPCS->hwndParent;                pControl->dwStyle = LPCS->style;                dwJustification = ((pControl->dwStyle & IP_RIGHT) != 0) ?                    ES_RIGHT :                     ((pControl->dwStyle & IP_CENTER) != 0) ?                    ES_CENTER : ES_LEFT;                hdc = GetDC(hWnd);                GetTextExtentExPoint (hdc, SZFILLER, 1,                                       0, NULL, NULL, &Size);                pControl->uiFillerWidth = Size.cx;                ReleaseDC(hWnd, hdc);                /* width of field - (margins) - (space for period * num periods)*/                pControl->uiFieldWidth = (LPCS->cx                                           - (LEAD_ROOM * 2)                                          - pControl->uiFillerWidth                                          *(NUM_FIELDS-1))                     / NUM_FIELDS;                uiFieldStart = LEAD_ROOM;                for (i = 0; i < NUM_FIELDS; ++i)                {                    pControl->Children[i].byLow = MIN_FIELD_VALUE;                    pControl->Children[i].byHigh = MAX_FIELD_VALUE;                    pControl->Children[i].hWnd = CreateWindow(		    			TEXT("Edit"),                        NULL,                        WS_CHILD | WS_VISIBLE |                         ES_MULTILINE | dwJustification,                        uiFieldStart,                        HEAD_ROOM,                        pControl->uiFieldWidth,                        LPCS->cy-(HEAD_ROOM*2),                        hWnd,                        (HMENU)i,                        LPCS->hInstance,                        NULL);                    //SendMessage(pControl->Children[i].hWnd, EM_LIMITTEXT,                    //		CHARS_PER_FIELD, 0L);                    Edit_LimitText(pControl->Children[i].hWnd, CHARS_PER_FIELD);                    pControl->Children[i].lpfnWndProc =                         (FARPROC) GetWindowLong(pControl->Children[i].hWnd,                                                GWL_WNDPROC);                    SetWindowLong(pControl->Children[i].hWnd,                                   GWL_WNDPROC, (LONG)IPAddressFieldProc);                    uiFieldStart += pControl->uiFieldWidth 		    		    + pControl->uiFillerWidth;                }                SAVE_CONTROL_HANDLE(hWnd, pControl);#undef LPCS            }            else                DestroyWindow(hWnd);        }        break;        case WM_PAINT: /* paint control window */        {            PAINTSTRUCT Ps;            RECT rect;            UINT uiFieldStart;            COLORREF TextColor;            HBRUSH hBrush, hOldBrush;            HPEN hPen, hOldPen;            BeginPaint(hWnd, (LPPAINTSTRUCT)&Ps);            GetClientRect(hWnd, &rect);            pControl = GET_CONTROL_HANDLE(hWnd);            hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));            hOldBrush = SelectObject(Ps.hdc, hBrush);            // #5599: need to set border pen to white if body of box is black			#define CLR_WHITE   0x00FFFFFFL			#define CLR_BLACK   0x00000000L			hPen = CreatePen(PS_SOLID, 1, ((CLR_BLACK==GetSysColor(COLOR_WINDOW)) ? CLR_WHITE : CLR_BLACK));			hOldPen = SelectObject(Ps.hdc, hPen);            Rectangle(Ps.hdc, 0, 0, rect.right, rect.bottom);                        SelectObject(Ps.hdc, hOldBrush);            DeleteObject(hBrush);                        SelectObject(Ps.hdc, hOldPen);            DeleteObject(hPen);            if (pControl->fEnabled)                TextColor = GetSysColor(COLOR_WINDOWTEXT);            else                TextColor = GetSysColor(COLOR_GRAYTEXT);            if (TextColor)                SetTextColor(Ps.hdc, TextColor);            SetBkColor(Ps.hdc, GetSysColor(COLOR_WINDOW));            uiFieldStart = pControl->uiFieldWidth + LEAD_ROOM;            for (i = 0; i < NUM_FIELDS-1; ++i)            {                ExtTextOut(Ps.hdc, uiFieldStart, HEAD_ROOM, 0, NULL,                           SZFILLER, 1, NULL);                uiFieldStart +=pControl->uiFieldWidth + pControl->uiFillerWidth;            }            pControl->fPainted = TRUE;            EndPaint(hWnd, &Ps);        }        break;        case WM_SETFONT:        {            pControl = GET_CONTROL_HANDLE(hWnd);            for (i = 0; i < NUM_FIELDS; ++i)            {                SendMessage (pControl->Children[i].hWnd,                             WM_SETFONT, wParam, lParam);            }        }        break;        case WM_SETFOCUS : /* get focus - display caret */            pControl = GET_CONTROL_HANDLE(hWnd);            EnterField(&(pControl->Children[0]), 0, CHARS_PER_FIELD);            break;        case WM_LBUTTONDOWN : /* left button depressed - fall through */            SetFocus(hWnd);            break;        case WM_ENABLE:        {            pControl = GET_CONTROL_HANDLE(hWnd);            pControl->fEnabled = (BOOL)wParam;            for (i = 0; i < NUM_FIELDS; ++i)            {                EnableWindow(pControl->Children[i].hWnd, (BOOL)wParam);            }            if (pControl->dwStyle & WS_TABSTOP)            {                DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);                if (wParam)                {                    dwStyle |= WS_TABSTOP;                }                else                {                    dwStyle &= ~WS_TABSTOP;                };                SetWindowLong(hWnd, GWL_STYLE, dwStyle);            }            if (pControl->fPainted)    InvalidateRect(hWnd, NULL, FALSE);        }        break;        case WM_DESTROY :            pControl = GET_CONTROL_HANDLE(hWnd);/* Restore all the child window procedures before we delete our memory block.*/            for (i = 0; i < NUM_FIELDS; ++i)            {                SetWindowLong(pControl->Children[i].hWnd, GWL_WNDPROC,                              (LONG)pControl->Children[i].lpfnWndProc);            }            LocalFree(pControl);            break;        case WM_COMMAND:            // MBERT! switch (GET_WM_COMMAND_CMD(wParam, lParam))            switch (LOWORD(wParam))                            {/* One of the fields lost the focus, see if it lost the focus to another field   of if we've lost the focus altogether.  If its lost altogether, we must send   an EN_KILLFOCUS notification on up the ladder. */                case EN_KILLFOCUS:                {                    HWND hFocus;                    pControl = GET_CONTROL_HANDLE(hWnd);                    if (!pControl->fInMessageBox)                    {                        hFocus = GetFocus();                        for (i = 0; i < NUM_FIELDS; ++i)                            if (pControl->Children[i].hWnd == hFocus)    break;                        if (i >= NUM_FIELDS)                        {                            //SendMessage(pControl->hwndParent, WM_COMMAND,                            //	    GetWindowWord(hWnd, GWW_ID),                            //	    MAKELPARAM(hWnd, EN_KILLFOCUS));                            SendMessage(pControl->hwndParent, WM_COMMAND,                                        MAKEWPARAM(GetWindowLong(hWnd, GWL_ID), EN_KILLFOCUS),                                        (LPARAM)hWnd);                            pControl->fHaveFocus = FALSE;                        }                    }                }                break;/* One of the fields is getting the focus.  If we don't currently have the   focus, then send an EN_SETFOCUS notification on up the ladder.*/                case EN_SETFOCUS:                    pControl = GET_CONTROL_HANDLE(hWnd);                    if (!pControl->fHaveFocus)                    {                        pControl->fHaveFocus = TRUE;                        //SendMessage(pControl->hwndParent, WM_COMMAND,                        //		GetWindowWord(hWnd, GWW_ID),                        //		MAKELPARAM(hWnd, EN_SETFOCUS));                        SendMessage(pControl->hwndParent, WM_COMMAND,                                     MAKEWPARAM(GetWindowLong(hWnd, GWL_ID), EN_SETFOCUS),                                    (LPARAM)hWnd );                    }                    break;            }            break;/* Get the value of the IP Address.  The address is placed in the DWORD pointed   to by lParam and the number of non-blank fields is returned.*/        case IP_GETADDRESS:        {            int iFieldValue;            DWORD dwValue;            pControl = GET_CONTROL_HANDLE(hWnd);            lResult = 0;            dwValue = 0;            for (i = 0; i < NUM_FIELDS; ++i)            {                iFieldValue = GetFieldValue(&(pControl->Children[i]));                if (iFieldValue == -1)                    iFieldValue = 0;                else                    ++lResult;                dwValue = (dwValue << 8) + iFieldValue;            }            *((DWORD FAR *)lParam) = dwValue;        }        break;/* Clear all fields to blanks.*/        case IP_CLEARADDRESS:        {            pControl = GET_CONTROL_HANDLE(hWnd);            for (i = 0; i < NUM_FIELDS; ++i)            {                //SendMessage(pControl->Children[i].hWnd, WM_SETTEXT,                //	    0, (LPARAM) (LPSTR) "");                SetWindowText(pControl->Children[i].hWnd,TEXT(""));            }        }        break;/* Set the value of the IP Address.  The address is in the lParam with the   first address byte being the high byte, the second being the second byte,   and so on.  A lParam value of -1 removes the address. */        case IP_SETADDRESS:        {            pControl = GET_CONTROL_HANDLE(hWnd);            for (i = 0; i < NUM_FIELDS; ++i)            {                wsprintf(szBuf, TEXT("%d"), HIBYTE(HIWORD(lParam)));                //SendMessage(pControl->Children[i].hWnd, WM_SETTEXT,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?