15,979
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include "afxwin.h"
#include "afxcmn.h"
#include "atltypes.h"
#define UM_TEST WM_USER + 1
class CTestWnd :
public CWnd
{
public:
CTestWnd(void);
~CTestWnd(void);
private:
CProgressCtrl m_progressCtrl;
public:
void Test(void);
private:
afx_msg LRESULT OnTest(WPARAM wParam, LPARAM lParam);
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
private:
CRect m_rtProgress;
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
};
.#include "StdAfx.h"
#include "TestWnd.h"
BEGIN_MESSAGE_MAP(CTestWnd, CWnd)
ON_WM_ACTIVATE()
ON_MESSAGE(UM_TEST, CTestWnd::OnTest)
ON_WM_SIZE()
END_MESSAGE_MAP()
UINT MyTestThread(LPVOID pParam)
{
while (1)
{
Sleep(2000);
SendMessage(static_cast<HWND>(pParam), UM_TEST, NULL, NULL);
}
return 0;
}
CTestWnd::CTestWnd(void)
{
}
CTestWnd::~CTestWnd(void)
{
}
void CTestWnd::Test(void)
{
AfxBeginThread(MyTestThread, static_cast<LPVOID>(m_hWnd));
m_progressCtrl.Create(NULL, m_rtProgress, this, 0);
}
void CTestWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState, pWndOther, bMinimized);
// TODO: 在此处添加消息处理程序代码
}
LRESULT CTestWnd::OnTest(WPARAM wParam, LPARAM lParam)
{
if (m_progressCtrl.IsWindowVisible())
{
m_progressCtrl.ShowWindow(SW_HIDE);
}
else
{
m_progressCtrl.ShowWindow(SW_SHOW);
}
return 0;
}
void CTestWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
m_rtProgress.SetRect(cy / 2, 0, cx, cy / 2 + 30);
}
#define WM_MYTSTMSG (WM_USER + 100)
class CMyWnd : public CWnd
{
public:
CMyWnd()
{
}
virtual ~CMyWnd()
{
}
CProgressCtrl m_PrsCtr;
public:
virtual BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID=0, LPVOID lpParam = NULL)
{
BOOL bRet = FALSE;
LPCTSTR lpszClassName = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS|CS_SAVEBITS,
::LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), ::LoadIcon(NULL, IDI_HAND));
bRet = CWnd::CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, lpParam);
if(bRet)
{
m_PrsCtr.Create(WS_CHILD|WS_VISIBLE, CRect(0,0, 100, 20), this, 1000);
}
SetTimer(10, 100, NULL);
return bRet;
}
virtual BOOL DestroyWindow()
{
KillTimer(10);
m_PrsCtr.DestroyWindow();
return CWnd::DestroyWindow();
}
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case(WM_TIMER):
{
if(wParam == 10)
{
if(m_PrsCtr.m_hWnd)
m_PrsCtr.StepIt();
}
break;
}
case(WM_MYTSTMSG):
{
if(m_PrsCtr)
{
int nCmd = m_PrsCtr.IsWindowVisible()? SW_HIDE:SW_SHOW;
m_PrsCtr.ShowWindow(nCmd);
}
break;
}
}
return CWnd::WindowProc(message, wParam, lParam);
}
};
//测试
BOOL CDlg6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_MyWnd.CreateEx(0, _T("Test"), WS_POPUP|WS_CAPTION|WS_VISIBLE, CRect(0, 0, 400, 400), this);
SetTimer(10, 2000, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlg6Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == 10)
{
m_MyWnd.PostMessage(WM_MYTSTMSG, 0, 0);
}
CDialog::OnTimer(nIDEvent);
}
BOOL CDlg6Dlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
m_MyWnd.DestroyWindow();
return CDialog::DestroyWindow();
}