📄 demopanel.cpp
字号:
// DemoPanel.cpp : implementation file
//
#include "stdafx.h"
#include "elecdemo.h"
#include "DemoPanel.h"
#include "mmsystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDemoPanel
LPCTSTR CDemoPanel::m_lpszClassName = NULL;
CDemoPanel::CDemoPanel()
{
m_xStartPos=m_xStartPos=0;
m_nSpeed=80;
m_iCurPage=m_iOldPage=0;
}
CDemoPanel::~CDemoPanel()
{
for(int i=0;i<m_PageList.GetSize();i++)
{
PAGEINFO *pi=m_PageList[i];
if (m_PageList[i]->pPage && m_PageList[i]->bAutoDestroy)
{
m_PageList[i]->pPage->DestroyWindow();
delete m_PageList[i]->pPage;
}
delete m_PageList[i];
}
m_PageList.RemoveAll();
}
BEGIN_MESSAGE_MAP(CDemoPanel, CWnd)
//{{AFX_MSG_MAP(CDemoPanel)
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoPanel message handlers
BOOL CDemoPanel::Create(DWORD dwExStyle, DWORD dwStyle, UINT nPlaceholderID,
CWnd* pParentWnd, UINT nID)
{
RECT rect;
pParentWnd->GetDlgItem(nPlaceholderID)->GetWindowRect(&rect);
pParentWnd->ScreenToClient(&rect);
m_rcClient=rect;
return Create(dwExStyle, dwStyle, rect, pParentWnd, nID);
}
BOOL CDemoPanel::Create(DWORD dwExStyle, DWORD dwStyle, const RECT& rect,
CWnd* pParentWnd, UINT nID)
{
m_Parent=pParentWnd;
// register window class & create CWnd object
if (m_lpszClassName == NULL)
m_lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW);
BOOL bResult = CreateEx(dwExStyle, m_lpszClassName, _T(""), dwStyle,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
pParentWnd->GetSafeHwnd(), (HMENU)nID, NULL );
m_Style=PANEL_RIGHT;
return bResult;
}
void CDemoPanel::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Do not call CWnd::OnPaint() for painting messages
}
LRESULT CALLBACK CDemoPanel::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAGEINFO* pi = (PAGEINFO*)GetWindowLong(hWnd, GWL_USERDATA);
CDemoPanel* _this = (CDemoPanel*)GetWindowLong(hWnd, DWL_USER);
switch (uMsg)
{
case WM_LBUTTONDOWN:
{
::SetCapture(hWnd);
}
break;
case WM_LBUTTONUP:
::ReleaseCapture();
break;
case WM_MOUSEMOVE:
{
}
break;
}
return ::CallWindowProc(pi->pOldProc, hWnd, uMsg, wParam, lParam);
}
void CDemoPanel::AddPage(LPCTSTR caption,UINT nIDTemplate,CRuntimeClass*rtc,int order,BOOL bAutoDel)
{
if (order<0||order>m_PageList.GetSize())
order=m_PageList.GetSize();
//Create Template
ASSERT(rtc!=NULL);
CDialog* pwndTemplate = (CDialog*)rtc->CreateObject();
BOOL b = pwndTemplate->Create(nIDTemplate, this);
if (!b) { delete pwndTemplate; return ; }
pwndTemplate->MoveWindow(0,0,m_rcClient.Width(),m_rcClient.Height());
ASSERT(pwndTemplate!=NULL);
ASSERT(pwndTemplate->m_hWnd!=NULL);
//SubClass Template window proc
WNDPROC pOldProc = (WNDPROC)::SetWindowLong(pwndTemplate->m_hWnd, DWL_DLGPROC,
(LONG)CDemoPanel::WindowProc);
//Add page at pagelist
PAGEINFO* pi = new PAGEINFO;
pi->pPage = pwndTemplate;
pi->pOldProc = pOldProc;
pi->bAutoDestroy = bAutoDel;
m_PageList.Add(pi);
//Set Window datas
::SetWindowLong(pwndTemplate->m_hWnd, GWL_USERDATA, (LONG)pi);
::SetWindowLong(pwndTemplate->m_hWnd, DWL_USER, (LONG)this);
}
int CDemoPanel::ShowPage(int i)
{
if((i<0)||(i>=m_PageList.GetSize()))
return -1;
::PlaySound((LPTSTR)IDR_SHOW,AfxGetInstanceHandle(),
SND_RESOURCE|SND_ASYNC);
SetStyle(m_Style);
m_iOldPage=m_iCurPage;
m_iCurPage=i;
SetTimer(PANEL_TIMER,100,NULL);
return m_iOldPage;
}
void CDemoPanel::RecalLayout()
{
int xPos=m_xStartPos;
int yPos=m_yStartPos;
//Update layout
HDWP hdwp = BeginDeferWindowPos(m_PageList.GetSize()); //*3 for pwndButton+pwndTemplate+pwndGroupBox
for (int i=0; i<m_PageList.GetSize(); i++)
{
PAGEINFO* pi = m_PageList[i];
int x=0,y=0;
if(m_iCurPage==i)
{
DeferWindowPos(hdwp, pi->pPage->m_hWnd, 0, xPos, yPos, 0, 0,SWP_NOZORDER|SWP_SHOWWINDOW
|SWP_NOSIZE);
}
else if(i==m_iOldPage)
{
switch(m_Style)
{
case PANEL_UP:
{
x=0;
y=m_yStartPos+m_rcClient.Height();
}
break;
case PANEL_DOWN:
{
x=0;
y=m_yStartPos-m_rcClient.Height();
}
break;
case PANEL_LEFT:
{
x=m_xStartPos-m_rcClient.Width();
y=0;
}
break;
case PANEL_RIGHT:
{
x=m_xStartPos+m_rcClient.Width();
y=0;
}
break;
}
DeferWindowPos(hdwp, pi->pPage->m_hWnd, 0, x, y, 0, 0,SWP_NOZORDER|SWP_SHOWWINDOW
|SWP_NOSIZE);
}
else
{
x=-m_rcClient.Width();
DeferWindowPos(hdwp, pi->pPage->m_hWnd, 0, x, y, 0, 0,SWP_NOZORDER|SWP_HIDEWINDOW
|SWP_NOSIZE);
}
}
EndDeferWindowPos(hdwp);
//Update Scroll Bar
InvalidateRect(&m_rcClient, FALSE);
UpdateWindow();
}
void CDemoPanel::OnTimer(UINT nIDEvent)
{
if(nIDEvent==PANEL_TIMER)
{
switch(m_Style)
{
case PANEL_UP:
{
m_yStartPos-=m_nSpeed;
if(m_yStartPos<=0)
{
m_yStartPos=0;
KillTimer(PANEL_TIMER);
}
}
break;
case PANEL_DOWN:
{
m_yStartPos+=m_nSpeed;
if(m_yStartPos>=0)
{
m_yStartPos=0;
KillTimer(PANEL_TIMER);
}
}
break;
case PANEL_LEFT:
{
m_xStartPos-=m_nSpeed;
if(m_xStartPos<=0)
{
m_xStartPos=0;
KillTimer(PANEL_TIMER);
}
}
break;
case PANEL_RIGHT:
{
m_xStartPos+=m_nSpeed;
if(m_xStartPos>=0)
{
m_xStartPos=0;
KillTimer(PANEL_TIMER);
}
TRACE1("%d\n",m_xStartPos);
}
break;
}
RecalLayout();
}
CWnd::OnTimer(nIDEvent);
}
void CDemoPanel::SetSpeed(int nSpeed)
{
if(nSpeed<=0)
return;
m_nSpeed=nSpeed;
}
void CDemoPanel::SetStyle(int style)
{
if(style==PANEL_UP)
{
m_yStartPos=m_rcClient.Height();
m_xStartPos=0;
}
else if(style==PANEL_DOWN)
{
m_yStartPos=-m_rcClient.Height();
m_xStartPos=0;
}
else if(style==PANEL_LEFT)
{
m_xStartPos=m_rcClient.Width();
m_yStartPos=0;
}
else
{
style=PANEL_RIGHT;
m_xStartPos=-m_rcClient.Width();
m_yStartPos=0;
}
m_Style=style;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -