DragWnd.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:10k
源码类别:
P2P编程
开发平台:
Visual C++
- // DragWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testBT.h"
- #include "DragWnd.h"
- #include "FileBase.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WS_EX_LAYERED 0x00080000
- #define LWA_ALPHA 0x00000002
- #define DISPLAY_TIMEOUT_ID 2
- const CString CDragWnd::m_strSection = "dragwnd";
- /////////////////////////////////////////////////////////////////////////////
- // CDragWnd
- CDragWnd::CDragWnd() : m_defSize(150, 50)
- {
- m_pMainFrame = 0;
- m_pCategoryDownload = 0;
- m_bShowText = false;
- m_iEdge = 5;
- m_nOpacity = 150;
- m_bTransparent = false;
- SetLayeredWindowAttributes = 0;
- HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
- if (hUser32)
- {
- SetLayeredWindowAttributes = (pSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
- }
- }
- CDragWnd::~CDragWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CDragWnd, CWnd)
- //{{AFX_MSG_MAP(CDragWnd)
- ON_WM_PAINT()
- ON_WM_CONTEXTMENU()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_LBUTTONDOWN()
- ON_WM_NCPAINT()
- ON_WM_CREATE()
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- ON_WM_GETMINMAXINFO()
- ON_WM_NCHITTEST()
- ON_WM_NCCALCSIZE()
- ON_WM_ERASEBKGND()
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDragWnd message handlers
- BOOL CDragWnd::CreateExtend(CWnd* pMainFrame, CCategoryDownload* pCategoryDownload)
- {
- ASSERT(pMainFrame && pCategoryDownload);
- m_pMainFrame = pMainFrame;
- m_pCategoryDownload = pCategoryDownload;
- CMonitorSpeedbase::CreateEx(pCategoryDownload, 12);
- const char* myWndClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS, ::LoadCursor(0, IDC_ARROW));
- BOOL bRet = CWnd::CreateEx(WS_EX_TOPMOST|WS_EX_TOOLWINDOW, myWndClass, "",
- WS_OVERLAPPED|WS_POPUP|WS_THICKFRAME, CRect(0, 0, 100, 100), 0, NULL);
- LoadState();
- return bRet;
- }
- void CDragWnd::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
- {
- lpMMI->ptMinTrackSize.x = 80;
- lpMMI->ptMinTrackSize.y = 50;
- lpMMI->ptMaxTrackSize.x = 250;
- lpMMI->ptMaxTrackSize.y = 100;
- }
- void CDragWnd::OnLButtonDown(UINT nFlags, CPoint point)
- {
- SendMessage(WM_LBUTTONUP);
- SendMessage(WM_SYSCOMMAND, 0xF012);
- }
- BOOL CDragWnd::OnEraseBkgnd(CDC* pDC)
- {
- CBrush backBrush(RGB(225, 245, 255));
- CBrush* pOldBrush = pDC->SelectObject(&backBrush);
- CRect rect;
- pDC->GetClipBox(&rect);
- pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
- PATCOPY);
- pDC->SelectObject(pOldBrush);
- return TRUE;
- }
- void CDragWnd::OnNcPaint()
- {
- CWindowDC dc(this);
- CRect rcClient, rc;
- GetClientRect(rcClient);
- ClientToScreen(rcClient);
- GetWindowRect(rc);
- rcClient.OffsetRect(-rc.TopLeft());
- rc.OffsetRect(-rc.TopLeft());
- int l = 230, r = 100;
- for (int i=0;i<m_iEdge - 1; i++)
- {
- l -= 10;
- r += 10;
- dc.Draw3dRect(rc, RGB(l, l+10, l + 10), RGB(r, r+10, r+10));
- rc.DeflateRect(1, 1);
- }
- dc.Draw3dRect(rc, RGB(0, 0, 0), RGB(0, 0, 0));
- }
- void CDragWnd::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
- {
- CRect rcClient = lpncsp->rgrc[0];
- rcClient.DeflateRect(m_iEdge, m_iEdge);
- lpncsp->rgrc[0] = rcClient;
- }
- void CDragWnd::OnSize(UINT nType, int cx, int cy)
- {
- if (cx < 20 || cy < 20)
- {
- cx = 20;
- cy = 20;
- }
- CWnd::OnSize(nType, cx, cy);
- }
- UINT CDragWnd::OnNcHitTest(CPoint point)
- {
- CRect rc, rcBar, rcEdge;
- GetWindowRect(rcBar);
- for (int i = 0; i < 8; i++)
- if (GetEdgeRect(rcBar, GetEdgeHTCode(i), rcEdge))
- if (rcEdge.PtInRect(point))
- return GetEdgeHTCode(i);
- UINT iRet = CWnd::OnNcHitTest(point);
- return iRet;
- }
- UINT CDragWnd::GetEdgeHTCode(int nEdge)
- {
- if (nEdge == 0) return HTLEFT;
- if (nEdge == 1) return HTTOP;
- if (nEdge == 2) return HTRIGHT;
- if (nEdge == 3) return HTBOTTOM;
- if (nEdge == 4) return HTTOPLEFT;
- if (nEdge == 5) return HTTOPRIGHT;
- if (nEdge == 6) return HTBOTTOMLEFT;
- if (nEdge == 7) return HTBOTTOMRIGHT;
- ASSERT(FALSE); // invalid edge no
- return HTNOWHERE;
- }
- BOOL CDragWnd::GetEdgeRect(CRect rcWnd, UINT nHitTest,
- CRect& rcEdge)
- {
- rcEdge = rcWnd;
- switch (nHitTest)
- {
- case HTLEFT:
- rcEdge.right = rcEdge.left + m_iEdge;
- rcEdge.DeflateRect(0, m_iEdge);
- break;
- case HTTOP:
- rcEdge.bottom = rcEdge.top + m_iEdge;
- rcEdge.DeflateRect(m_iEdge, 0);
- break;
- case HTRIGHT:
- rcEdge.left = rcEdge.right - m_iEdge;
- rcEdge.DeflateRect(0, m_iEdge);
- break;
- case HTBOTTOM:
- rcEdge.top = rcEdge.bottom - m_iEdge;
- rcEdge.DeflateRect(m_iEdge, 0);
- break;
- case HTTOPLEFT:
- rcEdge.bottom = rcEdge.top + m_iEdge;
- rcEdge.right = rcEdge.left + m_iEdge;
- break;
- case HTTOPRIGHT:
- rcEdge.bottom = rcEdge.top + m_iEdge;
- rcEdge.left = rcEdge.right - m_iEdge;
- break;
- case HTBOTTOMLEFT:
- rcEdge.top = rcEdge.bottom - m_iEdge;
- rcEdge.right = rcEdge.left + m_iEdge;
- break;
- case HTBOTTOMRIGHT:
- rcEdge.top = rcEdge.bottom - m_iEdge;
- rcEdge.left = rcEdge.right - m_iEdge;
- break;
- default:
- ASSERT(FALSE); // invalid hit test code
- }
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // normal message.
- int CDragWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- m_font.CreateFont(-12, 0, 0, 0, 400, 0, 0, 0, 134, 0, 0, 0, 2, "宋体");
- BOOL bRet = SetTimer(DISPLAY_TIMEOUT_ID, 1000, 0);
- ASSERT(bRet);
- return 0;
- }
- BOOL CDragWnd::DestroyWindow()
- {
- SaveState();
- return CWnd::DestroyWindow();
- }
- void CDragWnd::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- m_pMainFrame->PostMessage(WM_DRAGWND_NOTIFY , WM_CONTEXTMENU, 0);
- }
- void CDragWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- m_pMainFrame->PostMessage(WM_DRAGWND_NOTIFY , WM_LBUTTONDBLCLK, 0);
- CWnd::OnLButtonDblClk(nFlags, point);
- }
- void CDragWnd::LoadState()
- {
- int cx = GetSystemMetrics(SM_CXSCREEN);
- int cy = GetSystemMetrics(SM_CYSCREEN);
- CRect rcDefault(0, 0, m_defSize.x, m_defSize.y);
- rcDefault.OffsetRect(cx-200, cy-200);
- CRect rc(rcDefault);
- CString strKey = "l";
- rc.left = AfxGetApp()->GetProfileInt(m_strSection, strKey, rc.left);
- strKey = "t";
- rc.top = AfxGetApp()->GetProfileInt(m_strSection, strKey, rc.top);
- strKey = "r";
- rc.right = AfxGetApp()->GetProfileInt(m_strSection, strKey, rc.right);
- strKey = "b";
- rc.bottom = AfxGetApp()->GetProfileInt(m_strSection, strKey, rc.bottom);
- strKey = "ShowText";
- m_bShowText = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_bShowText) != 0;
- //
- // check window size parameter.
- //
- rc.NormalizeRect();
- if (rc.right > cx || rc.left < 0 || rc.bottom > cy || rc.top < 0)
- rc = rcDefault;
- if (rc.Width() < 10)rc.right = rc.left + 20;
- if (rc.Height() < 10) rc.bottom += rc.top + 20;
- strKey = "visible";
- bool bVisible = AfxGetApp()->GetProfileInt(m_strSection, strKey, true) != 0;
- SetWindowPos(NULL, rc.left, rc.top , rc.Width(), rc.Height(), bVisible ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
- strKey = "opacity";
- m_nOpacity = AfxGetApp()->GetProfileInt(m_strSection, strKey, m_nOpacity);
- if (m_nOpacity> 255 || m_nOpacity < 0) m_nOpacity = 150;
- SetTransparentEx(TRUE, m_nOpacity);
- }
- void CDragWnd::SaveState()
- {
- CRect rc;
- GetWindowRect(rc);
- CString strKey = "l";
- BOOL bRet = AfxGetApp()->WriteProfileInt(m_strSection, strKey, rc.left);
- ASSERT(bRet);
- strKey = "t";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, rc.top);
- strKey = "r";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, rc.right);
- strKey = "b";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, rc.bottom);
- strKey = "visible";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, IsWindowVisible());
- strKey = "opacity";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_nOpacity);
- strKey = "ShowText";
- AfxGetApp()->WriteProfileInt(m_strSection, strKey, m_bShowText);
- }
- void CDragWnd::ShowText(bool bShowText)
- {
- m_bShowText = bShowText;
- Invalidate();
- }
- bool CDragWnd::IsShowText()
- {
- return m_bShowText;
- }
- int CDragWnd::GetOpacity()
- {
- return m_nOpacity;
- }
- BOOL CDragWnd::SetOpacity(int nOpacity)
- {
- if (!SetLayeredWindowAttributes)
- return FALSE;
- // if (nOpacity >= 0 && nOpacity <= 255)
- {
- m_nOpacity = nOpacity;
- if (m_bTransparent)
- {
- // update the transparency
- ASSERT(::IsWindow(m_hWnd));
- SetLayeredWindowAttributes(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
- }
- return true;
- }
- return false;
- }
- void CDragWnd::SetTransparent(BOOL bTransparent)
- {
- if (!SetLayeredWindowAttributes)
- return ;
- if (bTransparent)
- {
- // make sure they have set it up properly
- ASSERT(m_nOpacity >= 0 && m_nOpacity <= 255);
- ASSERT(m_hWnd && ::IsWindow(m_hWnd));
- // make it transparent
- long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
- l |= WS_EX_LAYERED;
- SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
- SetLayeredWindowAttributes(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
- m_bTransparent = true;
- }
- else
- {
- long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
- l ^= WS_EX_LAYERED;
- SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
- CRect r;
- ::GetWindowRect(m_hWnd, r);
- ::InvalidateRect(m_hWnd, r, true);
- m_bTransparent = false;
- }
- }
- BOOL CDragWnd::SetTransparentEx(BOOL bTransparent, int nOpacity)
- {
- // set members
- if (!SetOpacity(nOpacity)) return false;
- SetTransparent(bTransparent);
- return true;
- }
- bool CDragWnd::CanSetTransparent()
- {
- return SetLayeredWindowAttributes != 0;
- }
- void CDragWnd::SetStandardSize()
- {
- CRect rc;
- GetWindowRect(rc);
- rc.right = rc.left + m_defSize.x;
- rc.bottom = rc.top + m_defSize.y;
- MoveWindow(rc);
- }
- void CDragWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- CRect rc;
- GetClientRect(rc);
- CMonitorSpeedbase::OnPaint(dc, rc, m_bShowText);
- }
- void CDragWnd::OnTimer(UINT nIDEvent)
- {
- if (DISPLAY_TIMEOUT_ID != nIDEvent)
- return;
- CMonitorSpeedbase::OnTimer();
- Invalidate();
- }