📄 netdisplay.cpp
字号:
// NetDisplay.cpp : implementation file
//
#include "stdafx.h"
#include "netfinder.h"
#include "NetDisplay.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BITMAPVIEWER_CLASSNAME _T("MFCNetDisplayCtl") // Window class name
/////////////////////////////////////////////////////////////////////////////
// CNetDisplay
CNetDisplay::CNetDisplay()
{
RegisterWindowClass();
ZeroMemory(m_ptr_array, sizeof(m_ptr_array));
m_numcells = 0;
m_selectedcell = -1;
m_ev1_visible = 1;
m_ev2_visible = 1;
m_mac_visible = 1;
}
CNetDisplay::~CNetDisplay()
{
}
// Register the window class if it has not already been registered.
BOOL CNetDisplay::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, BITMAPVIEWER_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = BITMAPVIEWER_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
BEGIN_MESSAGE_MAP(CNetDisplay, CWnd)
//{{AFX_MSG_MAP(CNetDisplay)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
ON_WM_VSCROLL()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNetDisplay message handlers
BOOL CNetDisplay::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CWnd::OnEraseBkgnd(pDC);
}
#define TOP_SCROLLOFFSET 25
#define TOP_OFFSET 0
#define CELL_HEIGHT 65
// Absolute Location Of Text Boxes 文本框的绝对位置
#define TEXT1_TOP 10
#define TEXT1_LEFT 10
#define TEXT2_TOP 10
#define TEXT2_LEFT 300
#define TEXT3_TOP 27
#define TEXT3_LEFT 10
#define TEXT4_TOP 28
#define TEXT4_LEFT 300
#define TEXT5_TOP 43
#define TEXT5_LEFT 10
#define TEXT6_TOP 43
#define TEXT6_LEFT 300
void CNetDisplay::OnPaint()
{
// Create a device context for painting
CPaintDC dc(this);
// Obtain a rectangle containing the canvas coordinates
CRect canvas_rect;
GetClientRect(&canvas_rect);
//------------------------------------------------------
// Draw each of the cells
//------------------------------------------------------
// Create a rectangle containing the first cell coordinates
CRect cell_rect;
cell_rect.left = (canvas_rect.left + 2);
cell_rect.right = (canvas_rect.right - 1);
cell_rect.top = (canvas_rect.top + TOP_OFFSET + 2 - (CELL_HEIGHT*GetScrollPos(SB_VERT)));
cell_rect.bottom = (cell_rect.top + CELL_HEIGHT );
// Paint each of the individual cells from first to last
for(unsigned int i = 0; i < m_numcells; i++){
if(m_ptr_array[i]){
// Paint first cell + remaining cells
PaintCell(&dc, &cell_rect, m_ptr_array[i], i);
// Add an offset to the next cell's coordinates
cell_rect.top += CELL_HEIGHT;
cell_rect.bottom += CELL_HEIGHT;
} else {
AfxMessageBox("null pointer");
}
}
//------------------------------------------------------
// Draw a border around the canvas
//------------------------------------------------------
dc.DrawEdge(canvas_rect, BDR_SUNKENINNER|BDR_SUNKENOUTER , BF_RECT);
// Do not call CWnd::OnPaint() for painting messages
}
void CNetDisplay::PaintCell(CPaintDC *pDC, CRect *pCell_Rect, NETDISPLAY_ENTRY *pEntry, int entry_number)
{
// Make a copy of the passed cell rectangle
CRect cell_rect(*pCell_Rect);
// Draw the cell border on top and bottom
pDC->DrawEdge(cell_rect, EDGE_ETCHED, BF_TOP|BF_BOTTOM);
//-----------------------
// Set Background Color
//-----------------------
bool white_text = 1;
int r1 = 0xff, g1 = 0xFF, b1 = 0xFF; // Initialize to white
int r2 = 0xff, g2 = 0xff, b2 = 0xff; // Initial values should never be seen
// Specify the Start and Stop color
switch(pEntry->alert_level){
case 0x01: // Select Gold
r1=0xff; g1=0xff; b1=0x00; // Start color
r2=0xff; g2=0xcc; b2=0x00; // Stop color
white_text = 0;
break;
case 0xFF: // Select Red
//r1=0xff; g1=0x83; b1=0x6; // Start color
r1=0xfc; g1=0xc6; b1=0x0a; // Start color
//r2=0xc8; g2=0x00; b2=0x00; // Stop color
r2=0xff; g2=0x00; b2=0x00; // Stop color
white_text = 0;
break;
case 0x00: // Select Green
default:
r1=0x66; g1=0xff; b1=0x66; // Start color
r2=0x00; g2=0xcc; b2=0x00; // Stop color
white_text = 0;
break;
}
// Change Color if selected
if(entry_number == m_selectedcell){
// Select Brown
//r1=0xD7; g1=0xD0; b1=0xC8; // Start color
//r2=0xAC; g2=0x9E; b2=0x95; // Stop color
//white_text = 1;
}
// Color the cell
for(int i = 0; i < cell_rect.Height(); i++)
{
int r,g,b;
r = r1 + (i * (r2-r1) / cell_rect.Height());
g = g1 + (i * (g2-g1) / cell_rect.Height());
b = b1 + (i * (b2-b1) / cell_rect.Height());
pDC->FillSolidRect( cell_rect.left, // x - upperleft
cell_rect.top + i, // y - upperleft
cell_rect.Width(), // width
1, // height
RGB(r,g,b) // color
);
}
//-----------------------
// Create Fonts + Colors
//-----------------------
CFont large_bold_font;
CFont mid_font;
CFont mid_bold_font;
large_bold_font.CreateFont(0, 0, 0, 0,
FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, "Arial");
mid_font.CreateFont(14, 5, 0, 0,
FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, "Arial");
mid_bold_font.CreateFont(14, 5, 0, 0,
FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, "Arial");
#define BLUE RGB( 0, 0, 255)
#define WHITE RGB(255, 255, 255)
#define BLACK RGB( 0, 0, 0)
#define YELLOW RGB(255, 255, 0)
//-----------------------
// Write the text
//-----------------------
COLORREF org_color; // Temporary Variable
CString temp_str, temp_str2; // Temporary Strings
// Do not overwrite the background -- FILL OFF
pDC->SetBkMode(TRANSPARENT);
// Set the default color
if(white_text) pDC->SetTextColor(BLACK);
else pDC->SetTextColor(BLACK);
//-------------------------------------------------------
// Write the first label <TEXT1> -- Device Name/Type
//-------------------------------------------------------
pDC->SelectObject(&large_bold_font);
pDC->TextOut( cell_rect.left + TEXT1_LEFT,
cell_rect.top + TEXT1_TOP,
pEntry->str_type
);
//-------------------------------------------------------
// Write the second label <TEXT2> -- IP Address
//-------------------------------------------------------
// If sorted by this method
if(m_sortmethod == SORT_IP){
// Set Color
if(white_text) {
org_color = pDC->SetTextColor(YELLOW);
} else {
org_color = pDC->SetTextColor(BLUE); //BLUE
}
} else {
// normal color
}
// Select Font
pDC->SelectObject(&large_bold_font);
// Write String
temp_str.Format("IP 地址: %u.%u.%u.%u", (unsigned short) pEntry->ip[0], (unsigned short) pEntry->ip[1],(unsigned short) pEntry->ip[2],(unsigned short) pEntry->ip[3]);
pDC->TextOut( cell_rect.left + TEXT2_LEFT,
cell_rect.top + TEXT2_TOP,
temp_str
);
// If color changed, restore color
if(m_sortmethod == SORT_IP) pDC->SetTextColor(org_color);
//-------------------------------------------------------
// Write the third label <TEXT3> - Additional Description
//-------------------------------------------------------
pDC->SelectObject(&mid_font);
temp_str.Format("%s",pEntry->str_description);
pDC->TextOut( cell_rect.left + TEXT3_LEFT,
cell_rect.top + TEXT3_TOP,
pEntry->str_description
);
//-------------------------------------------------------
// Write the fourth label <TEXT4> -- Mac Address
//-------------------------------------------------------
if(m_mac_visible){
pDC->SelectObject(&mid_font);
temp_str.Format("MAC 地址: %02x-%02x-%02x-%02x-%02x-%02x", (unsigned short) pEntry->mac[0], (unsigned short) pEntry->mac[1],(unsigned short) pEntry->mac[2],(unsigned short) pEntry->mac[3],(unsigned short) pEntry->mac[4],(unsigned short) pEntry->mac[5]);
pDC->TextOut( cell_rect.left + TEXT4_LEFT,
cell_rect.top + TEXT4_TOP,
temp_str
);
}
//-------------------------------------------------------
// Write the fifth label <TEXT5> -- Event 1 Time
//-------------------------------------------------------
if(m_ev1_visible){
// If sorted by this method
if(m_sortmethod == SORT_EV1){
// Set Color
if(white_text) {
org_color = pDC->SetTextColor(YELLOW);
} else {
org_color = pDC->SetTextColor(BLUE);
}
// Set Font
pDC->SelectObject(&mid_bold_font);
} else {
// Set Font
pDC->SelectObject(&mid_font);
}
// Set Event1 String
temp_str = pEntry->str_ev1 + " ";
if(pEntry->event1_days) temp_str2.Format("%i 天, %i 小时, %i 分, %i 秒", pEntry->event1_days,pEntry->event1_hours,pEntry->event1_minutes, pEntry->event1_seconds );
else
if(pEntry->event1_hours) temp_str2.Format("%i 小时, %i 分, %i 秒", pEntry->event1_hours,pEntry->event1_minutes, pEntry->event1_seconds );
else
if(pEntry->event1_minutes) temp_str2.Format("%i 分, %i 秒", pEntry->event1_minutes, pEntry->event1_seconds );
else
temp_str2.Format("%i 秒", pEntry->event1_seconds );
// Write Event 1 Text Field
pDC->TextOut( cell_rect.left + TEXT5_LEFT,
cell_rect.top + TEXT5_TOP,
temp_str + temp_str2
);
// If color changed, restore color
if(m_sortmethod == SORT_EV1) pDC->SetTextColor(org_color);
}
//-------------------------------------------------------
// Write the sixth label <TEXT6> -- Event 2 Time
//-------------------------------------------------------
if(m_ev2_visible){
// If sorted by this method
if(m_sortmethod == SORT_EV2){
// Set Font
pDC->SelectObject(&mid_bold_font);
// Set Color
if(white_text) {
org_color = pDC->SetTextColor(YELLOW);
} else {
org_color = pDC->SetTextColor(BLUE);
}
} else {
pDC->SelectObject(&mid_font);
}
// Set Event 2 String
temp_str = pEntry->str_ev2 + " ";
if(pEntry->event2_days) temp_str2.Format("%i 天, %i 小时, %i 分, %i 秒", pEntry->event2_days,pEntry->event2_hours,pEntry->event2_minutes, pEntry->event2_seconds );
else
if(pEntry->event2_hours) temp_str2.Format("%i 小时, %i 分, %i 秒", pEntry->event2_hours,pEntry->event2_minutes, pEntry->event2_seconds );
else
if(pEntry->event2_minutes) temp_str2.Format("%i 分, %i 秒", pEntry->event2_minutes, pEntry->event2_seconds );
else
temp_str2.Format("%i 秒", pEntry->event2_seconds );
// Write Event 2 Field
pDC->TextOut( cell_rect.left + TEXT6_LEFT,
cell_rect.top + TEXT6_TOP,
temp_str + temp_str2
);
// If color changed, restore color
if(m_sortmethod == SORT_EV2) pDC->SetTextColor(org_color);
}
// Raised edge if selected
if(entry_number == m_selectedcell){
CBrush brush1; // Must initialize!
brush1.CreateSolidBrush(RGB(0,0,0)); // Black brush.
int i;
CRect sel;
sel.CopyRect(&cell_rect);
sel.right -= 1;
pDC->FrameRect(sel, &brush1);
// Thicken Border
for (i = 0; i < 6; i++){
sel.left += 1;
sel.bottom -= 1;
sel.right -= 1;
sel.top += 1;
pDC->FrameRect(sel, &brush1);
}
//pDC->DrawEdge(cell_rect, EDGE_ETCHED, BF_TOP|BF_BOTTOM);
}
//-----------------------
// Cleanup
//-----------------------
// Restore Default Font
pDC->SelectStockObject(SYSTEM_FONT);
// Delete Fonts
large_bold_font.DeleteObject();
mid_font.DeleteObject();
mid_bold_font.DeleteObject();
}
int CNetDisplay::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
void CNetDisplay::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
// its horizontal scroll bar
int nCurPos = GetScrollPos(SB_VERT) ;
int nPrevPos = nCurPos;
// decide what to do for each diffrent scroll event
// SB_BOTTOM Scroll to bottom.
// SB_ENDSCROLL End scroll.
// SB_LINEDOWN Scroll one line down.
// SB_LINEUP Scroll one line up.
// SB_PAGEDOWN Scroll one page down.
// SB_PAGEUP Scroll one page up.
// SB_THUMBPOSITION Scroll to the absolute position. The current position is provided in nPos.
// SB_THUMBTRACK Drag scroll box to specified position. The current position is provided in nPos.
// SB_TOP Scroll to top.
switch(nSBCode)
{
case SB_ENDSCROLL: this->RedrawWindow();
break;
case SB_BOTTOM: nCurPos = GetScrollLimit(SB_VERT);
break;
case SB_LINEDOWN:
case SB_PAGEDOWN: if(nPrevPos < GetScrollLimit(SB_VERT))
{
nCurPos++;
}
break;
case SB_LINEUP:
case SB_PAGEUP: if(nPrevPos > 0)
{
nCurPos--;
}
break;
case SB_THUMBTRACK:
break;
case SB_THUMBPOSITION: nCurPos = nPos;
break;
case SB_TOP: nCurPos = 0;
break;
default:
AfxMessageBox("Scrolling Error", MB_OK, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -