⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prsprogresswnd.cpp

📁 采用文档类方法实现的一种串口通讯协议。可以借鉴参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////
// FileName:	PrsProgressWnd.cpp
// Version:
// Copyright (c) 2003 RAE Systems Inc		
// Purpose:		implementation of the CPrsProgressWnd class.
//				use for show progress bar and waiting animal picture
// Created by	Jianbin Kang on 07/8/2003
// Modification:
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "..\\prs.h"
#include "PrsProgressWnd.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define IDC_CANCEL   10
#define IDC_TEXT     11
#define IDC_PROGRESS 12

const LPCTSTR szSection = _T("Settings");   
const LPCTSTR szEntryX  = _T("X");
const LPCTSTR szEntryY  = _T("Y");


/////////////////////////////////////////////////////////////////////////////
// CPrsProgressWnd

CPrsProgressWnd::CPrsProgressWnd()
{
    CommonConstruct();
}

CPrsProgressWnd::CPrsProgressWnd(CWnd* I_pParent, LPCTSTR I_pszTitle, BOOL I_bSmooth /*= FALSE*/)
{
    CommonConstruct();
    m_strTitle = I_pszTitle;

    Create(I_pParent, I_pszTitle, I_bSmooth);
}

void CPrsProgressWnd::CommonConstruct()
{

    m_wRenenableWnd  = NULL;

    m_nNumTextLines  = 4;
    m_nPrevPos       = 0;
    m_nPrevPercent   = 0;
    m_nStep          = 1;
    m_nMinValue      = 0;
    m_nMaxValue      = 100;

    m_strTitle       = _T("Progress");
    m_strCancelLabel = _T(" Cancel ");
    m_bCancelled     = FALSE;
    m_bModal         = FALSE;

    m_bPersistantPosition = TRUE;   // saves and restores position automatically

	m_strGIFFileName = PRS_STR_EMPTY;
	m_bProgressBarShow = TRUE;

}

CPrsProgressWnd::~CPrsProgressWnd()
{
    DestroyWindow();
}

BOOL CPrsProgressWnd::Create(CWnd* I_pParent, LPCTSTR I_pszTitle, BOOL I_bSmooth /*= FALSE*/)
{
	BOOL bSuccess;

	// Register window class
	CString csClassName = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW,
											  ::LoadCursor(NULL, IDC_APPSTARTING),
											  CBrush(::GetSysColor(COLOR_BTNFACE)));

	// Get the system window message font for use in the cancel button and text area
	NONCLIENTMETRICS ncm;
	ncm.cbSize = sizeof(NONCLIENTMETRICS);
	VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
	m_font.CreateFontIndirect(&(ncm.lfMessageFont)); 

	// If no parent supplied then try and get a pointer to it anyway
	if (NULL != I_pParent)
	{
		I_pParent = AfxGetMainWnd();
	}

	// Create popup window
	//Modified by XiaoDong Zhou
	//For fixing UPMOST style 
	bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, //|WS_EX_CLIENTEDGE, // Extended style
						csClassName,                       // Classname
						I_pszTitle,                          // Title
						WS_POPUP|WS_BORDER|WS_CAPTION,     // style
						0,0,                               // position - updated soon.
						390,130,                           // Size - updated soon
						I_pParent->GetSafeHwnd(),            // handle to parent
						0,                                 // No menu
						NULL);    
	if (FALSE == bSuccess) 
	{
		return FALSE;
	}

	// Now create the controls
	CRect TempRect(0,0,10,10);

	bSuccess = m_lblPrompt.Create(PRS_STR_EMPTY, WS_CHILD|WS_VISIBLE|SS_NOPREFIX|SS_LEFTNOWORDWRAP,
							 TempRect, this, IDC_TEXT);
	if (FALSE == bSuccess) 
	{
		return FALSE;
	}


	bSuccess = m_CPictureEx.Create(PRS_STR_EMPTY, WS_CHILD|WS_VISIBLE|SS_NOPREFIX|SS_LEFTNOWORDWRAP,
							 TempRect, this, IDC_TEXT);
	if (FALSE == bSuccess) 
	{
		return FALSE;
	}

	//create the 
	DWORD dwProgressStyle = WS_CHILD|WS_VISIBLE;
#ifdef PBS_SMOOTH    
	if (TRUE == I_bSmooth)
	{
		dwProgressStyle |= PBS_SMOOTH;
	}
#endif
	bSuccess = m_wndProgress.Create(dwProgressStyle, TempRect, this, IDC_PROGRESS);
	if (FALSE == bSuccess) 
	{
		return FALSE;
	}

	bSuccess = m_CancelButton.Create(m_strCancelLabel, 
									 WS_CHILD|WS_VISIBLE|WS_TABSTOP| BS_PUSHBUTTON, 
									 TempRect, this, IDC_CANCEL);
	if (FALSE == bSuccess) 
	{
		return FALSE;
	}

	m_CancelButton.SetFont(&m_font, TRUE);
	m_lblPrompt.SetFont(&m_font, TRUE);

	// Resize the whole thing according to the number of text lines, desired window
	// width and current font.
	SetWindowSize(m_nNumTextLines, 390);

	// Center and show window
	//m_bPersistantPosition 
//	if (m_bPersistantPosition)
//	{
//		GetPreviousSettings();
//	}
//	else
//	{
		CenterWindow(I_pParent);
//	}

//	Show();

	return TRUE;
}

BOOL CPrsProgressWnd::GoModal(LPCTSTR I_pszTitle /*=_T("Progress")"*/, BOOL I_bSmooth /*= FALSE*/)
{
    CWnd *pMainWnd = AfxGetMainWnd();

    if (!::IsWindow(m_hWnd) && !Create(pMainWnd, I_pszTitle, I_bSmooth))
	{
		return FALSE;
	}

    // Walk up the window chain to find the main parent wnd and disable it. 
    CWnd * wnd = this;
    do 
	{
        CWnd * parent = wnd->GetParent();

        // if we have no parent (ie. the main window)
        // or if our parent is disabled, 
        // then this is the window that we will want to remember for reenabling
        if (!parent || !parent->IsWindowEnabled()) 
		{
            m_wRenenableWnd = wnd;
            m_wRenenableWnd->EnableWindow(false);
            break;
        }
        wnd = parent;
    } while (1);

    // Re-enable this window
    EnableWindow(TRUE);

    m_bModal = TRUE;

    return TRUE;
}
   
void CPrsProgressWnd::SetWindowTotal(CString I_strTotal /*= "ProRAE Studio"*/)
{
	m_strTitle = I_strTotal;
}

void CPrsProgressWnd::GetShowFlag()
{
	m_byShowFlag = 0;
	if(TRUE == m_bProgressBarShow)
	{
		m_byShowFlag |= 0x01;
	}

	if(0 != m_strCancelLabel.Compare(PRS_STR_EMPTY))
	{
		m_byShowFlag |= 0x02;
	}

	if(0 != m_strPrompt.Compare(PRS_STR_EMPTY))
	{
		m_byShowFlag |= 0x04;
	}

	if(0 != m_strGIFFileName.Compare(PRS_STR_EMPTY))
	{
		m_byShowFlag |= 0x08;
	}

}

void CPrsProgressWnd::GetGUISize(int I_nNumTextLines, int I_nWindowWidth)
{
	int nLeft = 0;

 	GetShowFlag();

	CSize EdgeSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));

	CRect TextRect, CancelRect, ProgressRect, PicRect;
    CSize CancelSize;

	m_sizePic = CSize(0, 0);
	m_sizeCancel = CSize(0, 0);
	m_sizeWindow = CSize(0, 0);
	m_RectPrompt.SetRect(PRS_WAIT_MARGIN, PRS_WAIT_MARGIN, 
		PRS_WAIT_MARGIN, PRS_WAIT_MARGIN);

	//picture show
	if(0 != (m_byShowFlag & 0x08))
	{
		m_sizePic = m_CPictureEx.GetSize();
		nLeft = m_sizePic.cx + PRS_WAIT_MARGIN;
		m_sizeWindow.cx = nLeft;
	}

	CDC* pDC = GetDC();
    if (NULL == pDC)
	{
		return;
	} 

	//prompt label show
	if(0 != (m_byShowFlag & 0x04))
	{
		m_RectPrompt.left = PRS_WAIT_MARGIN + nLeft;
		m_RectPrompt.top = PRS_WAIT_MARGIN;

        int nText = pDC->DrawText(m_strPrompt, m_RectPrompt, DT_CALCRECT|DT_NOCLIP|DT_NOPREFIX);
		
		m_sizeWindow.cx += m_RectPrompt.left + m_RectPrompt.Width();
		if(m_sizeWindow.cx < I_nWindowWidth)
		{
			m_sizeWindow.cx = I_nWindowWidth;
		}

        m_RectPrompt.right = m_RectPrompt.left + m_sizeWindow.cx;
	}
	
	int nHeightCancel = 0;
	CFont* pOldFont = pDC->SelectObject(&m_font);

	//cancel button show
	if(0 != (m_byShowFlag & 0x02))
	{
		m_sizeCancel = pDC->GetTextExtent(m_strCancelLabel + _T("  ")) +
                                         CSize(EdgeSize.cx*4, EdgeSize.cy*3);
		nHeightCancel += m_sizeCancel.cy + PRS_WAIT_MARGIN;

		if(m_sizeWindow.cx < m_sizeCancel.cx + PRS_WAIT_MARGIN)
		{
			m_sizeWindow.cx = m_sizeCancel.cx + PRS_WAIT_MARGIN;
		}
	}

	//progress bar show
	if((0 != (m_byShowFlag & 0x01)) && (0 == nHeightCancel))
	{
		m_sizeCancel.cy = PRS_WAIT_PROGRESS_MINHEIGHT;
		nHeightCancel += m_sizeCancel.cy + PRS_WAIT_MARGIN;
		if(m_sizeWindow.cx < m_sizeCancel.cx + PRS_WAIT_MARGIN + PRS_WAIT_PROGRESS_MINWIDTH)
		{
			m_sizeWindow.cx = m_sizeCancel.cx + PRS_WAIT_MARGIN + PRS_WAIT_PROGRESS_MINWIDTH;
		}
	}

	//set wondow size
	if(0 == m_sizeWindow.cx)
	{
		m_sizeWindow.cx = I_nWindowWidth;
	}

	m_sizeWindow.cx += PRS_WAIT_MARGIN;
	m_sizeWindow.cy = m_RectPrompt.Height() > m_sizePic.cy ? m_RectPrompt.Height() + PRS_WAIT_MARGIN : m_sizePic.cy + PRS_WAIT_MARGIN;
	m_sizeWindow.cy += nHeightCancel + PRS_WAIT_MARGIN;

    pDC->SelectObject(pOldFont);
	ReleaseDC(pDC);

}
 
void CPrsProgressWnd::SetWindowSize(int I_nNumTextLines, int I_nWindowWidth /*=390*/)
{
	//Get controller size info
	GetGUISize(I_nNumTextLines, I_nWindowWidth);

	//hide all controllers
	m_CancelButton.ShowWindow(SW_HIDE);
	m_wndProgress.ShowWindow(SW_HIDE);
	m_lblPrompt.ShowWindow(SW_HIDE);
	m_CPictureEx.ShowWindow(SW_HIDE);

    // Resize the main window to fit the controls
    CRect WndRect, ClientRect;
    GetWindowRect(WndRect); GetClientRect(ClientRect);
    WndRect.right = WndRect.left + WndRect.Width() - ClientRect.Width() + m_sizeWindow.cx;
    WndRect.bottom = WndRect.top + WndRect.Height() - ClientRect.Height() + m_sizeWindow.cy;
    
	
	MoveWindow(WndRect);
	CenterWindow();
    
	CRect CancelRect(0, 0, 0, 0);
	CRect ProgressRect(0, 0, 0, 0);
	CRect PicRect(0, 0, 0, 0);

	int nProgressRight = 0;
    // Work out how big (and where) the cancel button should be
	//cancel button show
	if(0 != (m_byShowFlag & 0x02))
	{
		CancelRect.SetRect( m_sizeWindow.cx  - PRS_WAIT_MARGIN - m_sizeCancel.cx, 
							m_sizeWindow.cy - PRS_WAIT_MARGIN - m_sizeCancel.cy, 
						    m_sizeWindow.cx  - PRS_WAIT_MARGIN, 
							m_sizeWindow.cy - PRS_WAIT_MARGIN);
		m_CancelButton.SetWindowText(m_strCancelLabel);
		m_CancelButton.MoveWindow(CancelRect);
		m_CancelButton.ShowWindow(SW_SHOW);

		nProgressRight = CancelRect.left - PRS_WAIT_MARGIN;
	}
	else
	{
		nProgressRight = m_sizeWindow.cx - PRS_WAIT_MARGIN;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -