【VC开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了VC开源代码-IPADDR.CPP的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
#pragma title("IP Address Custom Control Implementation")
// Created by Joseph A. Dziedzic, September 1997
// Revised April 1998
// Thanks to Dan Anderson, Kenny Goers, Kevin Lussier, and Doug Miller for their suggestions
// and code enhancements.
// Mail comments to dziedzic@ma.ultranet.com
#include "stdafx.h"
#include "IPAddr.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Style bits for the individual edit controls
const int WS_EDIT = WS_CHILD | WS_VISIBLE | ES_CENTER | ES_MULTILINE;
const TCHAR szDialogClass[] = _T("#32770"); // Special window class for dialogs
BOOL CIPAddrCtl::m_bRegistered = Register(); // Register the control during class initialization
/////////////////////////////////////////////////////////////////////////////
// CIPAddrCtl
IMPLEMENT_DYNCREATE(CIPAddrCtl, CWnd)
CIPAddrCtl::CIPAddrCtl()
{
m_bEnabled = TRUE; // Window enabled flag (TRUE by default)
m_bReadOnly = FALSE; // Read only flag (FALSE by default)
m_bNoValidate = FALSE; // Don't do immediate field validation on input
}
CIPAddrCtl::~CIPAddrCtl()
{
}
BEGIN_MESSAGE_MAP(CIPAddrCtl, CWnd)
//{{AFX_MSG_MAP(CIPAddrCtl)
ON_WM_CREATE()
ON_WM_NCDESTROY()
ON_WM_SIZE()
ON_WM_SETFOCUS()
ON_WM_PAINT()
ON_WM_ENABLE()
ON_WM_ERASEBKGND()
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(IPAM_GETADDRESS, OnGetAddress)
ON_MESSAGE(IPAM_SETADDRESS, OnSetAddress)
ON_MESSAGE(IPAM_SETREADONLY, OnSetReadOnly)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIPAddrCtl message handlers
BOOL CIPAddrCtl::Register()
{
// Register the window class of the control
WNDCLASS wc;
wc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW; // Usual style bits
wc.lpfnWndProc = IPAddrWndProc; // Message processing code
wc.cbClsExtra = 0; // No extra bytes needed
wc.cbWndExtra = 0;
wc.hInstance = NULL; // No instance handle
wc.hIcon = NULL; // No icon
wc.hCursor = ::LoadCursor(NULL, IDC_IBEAM); // Use I-beam cursor (like edit control)
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); // Use default window color (overriden in OnEraseBkgnd)
wc.lpszMenuName = NULL; // No menus
wc.lpszClassName = _T("IPAddr"); // Class name
if (!::RegisterClass(&wc)) // If registration failed, subsequent dialogs will fail
{
ASSERT(FALSE);
return FALSE;
}
else
return TRUE;
}
BOOL CIPAddrCtl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwExStyle/*=0*/)
{
// Create a window class that has the properties we want
CString szWndClass = AfxRegisterWndClass(CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_IBEAM), (HBRUSH) COLOR_WINDOW+1);
// Create using the extended window style
#if _MSC_VER >= 1100
// Original VC 5.0 stuff
return CWnd::CreateEx(dwExStyle, szWndClass, NULL, dwStyle, rect, pParentWnd, nID);
#else
// Back ported to VC 4.2
return CWnd::CreateEx(dwExStyle, szWndClass, NULL, dwStyle,
rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
pParentWnd->GetSafeHwnd(), (HMENU) nID);
#endif
}
LRESULT CALLBACK IPAddrWndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch (uiMsg) // Dispatch on message type
{
case WM_NCCREATE: // On WM_NCCREATE we create a C++ object and attach it to the control
{
CIPAddrCtl* pCtl = new CIPAddrCtl; // Create an instance of the class
ASSERT(pCtl); // Better not fail!
BOOL b = pCtl->SubclassWindow(hWnd); // Attach the window handle to the new object
ASSERT(b); // Better not fail!
return b; // Return result to continue/abort window creation
break;
}
default: // All other messages go through default window processor
return ::DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
}
int CIPAddrCtl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Save the "no immediate validation on input" style setting
m_bNoValidate = (lpCreateStruct->style & IPAS_NOVALIDATE);
// Set the styles for the parent control
ModifyStyleEx(0, WS_EX_CLIENTEDGE | WS_EX_NOPARENTNOTIFY);
/