// HorizontalLine.cpp: implementation of the CHorizontalLine class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "HorizontalLine.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CHorizontalLine::CHorizontalLine() { m_hWnd = NULL; m_crBkgnd = RGB(255, 255, 255); m_crLineColor = RGB(222, 222, 222); } CHorizontalLine::~CHorizontalLine() { } BOOL CHorizontalLine::RegisterWndClass() { WNDCLASS wc; wc.lpszClassName = BYHORIZONTALLINE_CLASS; wc.hInstance = AfxGetInstanceHandle(); wc.lpfnWndProc = WindowProc; wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); wc.hIcon = 0; wc.lpszMenuName = NULL; wc.hbrBackground = NULL; wc.style = CS_GLOBALCLASS|CS_DBLCLKS; wc.cbClsExtra = 0; wc.cbWndExtra = 0; // ×¢²á×Ô¶¨ÒåÀà return (::RegisterClass(&wc) != 0); } CHorizontalLine* CHorizontalLine::Hook(HWND hWnd) { CHorizontalLine* pHorizontalLine = (CHorizontalLine*)GetProp(hWnd, BYSTAG_HORIZONTALLINE); if(pHorizontalLine == NULL) { pHorizontalLine = new CHorizontalLine; pHorizontalLine->m_hWnd = hWnd; SetProp(hWnd, BYSTAG_HORIZONTALLINE, (HANDLE)pHorizontalLine); } return pHorizontalLine; } void CHorizontalLine::Release() { // delete delete this; } void CHorizontalLine::Notify(int nCode, int dwData, int dwData1/* = 0*/, int dwData2/* = 0*/) { HWND hParent; hParent = GetParent(m_hWnd); if(hParent != NULL) { BYHORIZONTALLINE_NMHDR iii_nmhdr; iii_nmhdr.nmhdr.hwndFrom = m_hWnd; iii_nmhdr.nmhdr.idFrom = GetWindowLong(m_hWnd, GWL_ID); iii_nmhdr.nmhdr.code = nCode; iii_nmhdr.dwData = dwData; iii_nmhdr.dwData1 = dwData1; iii_nmhdr.dwData2 = dwData2; SendMessage(hParent, WM_NOTIFY, (WPARAM)iii_nmhdr.nmhdr.idFrom, (LPARAM)&iii_nmhdr); } } //////////////////////////////// // À¹½Ø´°¿ÚÏûÏ¢º¯Êý LRESULT CALLBACK CHorizontalLine::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { CHorizontalLine* pHorizontalLine = (CHorizontalLine *)GetProp(hWnd, BYSTAG_HORIZONTALLINE); if(pHorizontalLine == NULL && uMsg != WM_NCCREATE) { return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } // Èç¹ûHookÔòÏìÓ¦ÏûÏ¢ ASSERT(hWnd); switch(uMsg) { case WM_NCCREATE: return OnNcCreate(hWnd, wParam, lParam); case WM_DESTROY: return pHorizontalLine->OnDestroy(wParam, lParam); case WM_PAINT: return pHorizontalLine->OnPaint(wParam, lParam); case WM_TIMER: return pHorizontalLine->OnTimer(wParam, lParam); case WM_GETDLGCODE: return DLGC_WANTALLKEYS; default: break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } /////////////////////////////// // WM_NCCREATE // ´°¿Ú´´½¨Ç°µÄ³õʼ»¯¹¤×÷ LRESULT CHorizontalLine::OnNcCreate(HWND hWnd, WPARAM wParam, LPARAM lParam) { CHorizontalLine* pHorizontalLine = (CHorizontalLine *)GetProp(hWnd, BYSTAG_HORIZONTALLINE); ASSERT(pHorizontalLine == NULL); Hook(hWnd); return ::DefWindowProc(hWnd, WM_NCCREATE, wParam, lParam); } /////////////////////////////// // WM_DESTROY LRESULT CHorizontalLine::OnDestroy(WPARAM wParam, LPARAM lParam) { Release(); return ::DefWindowProc(m_hWnd, WM_DESTROY, wParam, lParam); } /////////////////////////////// // WM_TIMER LRESULT CHorizontalLine::OnTimer(WPARAM wParam, LPARAM lParam) { return ::DefWindowProc(m_hWnd, WM_TIMER, wParam, lParam); } /////////////////////////////// // WM_PAINT LRESULT CHorizontalLine::OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC, hMemDC; HBITMAP hBitmap; RECT rcClient; CString strText; HFONT hFont; HBRUSH hBrushBK; // BeginPaint PAINTSTRUCT ps; hDC = BeginPaint(m_hWnd, &ps); GetClientRect(m_hWnd, &rcClient); hMemDC = ::CreateCompatibleDC(hDC); hBitmap = ::CreateCompatibleBitmap(hDC, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top); ::SelectObject(hMemDC, hBitmap); hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT); ::SelectObject(hMemDC, hFont); // ±³¾°ÑÕÉ« hBrushBK = CreateSolidBrush( m_crBkgnd ); ::FillRect(hMemDC, &rcClient, hBrushBK); DeleteObject(hBrushBK); // »­Ïß HPEN hPen = CreatePen(PS_SOLID, 1, m_crLineColor); HPEN hOldPen = (HPEN)::SelectObject(hMemDC, hPen); ::MoveToEx(hMemDC, 0, 0, NULL); LineTo(hMemDC, rcClient.right, 0); ::SelectObject(hMemDC, hOldPen); // EndPaint ::BitBlt(hDC, 0, 0, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top, hMemDC, 0, 0, SRCCOPY); EndPaint(m_hWnd, &ps); ::DeleteObject(hBitmap); ::DeleteDC(hMemDC); return 1; } void CHorizontalLine::SetBkgndColor(COLORREF cr) { m_crBkgnd = cr; }