开始等待画面

本文详细介绍了如何使用C++实现显示启动画面的功能,包括类定义、成员变量、成员函数以及初始化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
// CG: This file was added by the Splash Screen component.

#ifndef _SPLASH_SCRN_
#define _SPLASH_SCRN_

// Splash.h : header file
//

/////////////////////////////////////////////////////////////////////////////
//   Splash Screen class

class CSplashWnd : public CWnd
{
// Construction
protected:
	CSplashWnd();

// Attributes:
public:
	CBitmap m_bitmap;

// Operations
public:
	static void EnableSplashScreen(BOOL bEnable = TRUE);
	static void ShowSplashScreen(CWnd* pParentWnd = NULL);
	static BOOL PreTranslateAppMessage(MSG* pMsg);

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSplashWnd)
	//}}AFX_VIRTUAL

// Implementation
public:
	~CSplashWnd();
	virtual void PostNcDestroy();

protected:
	BOOL Create(CWnd* pParentWnd = NULL);
	void HideSplashScreen();
	static BOOL c_bShowSplashWnd;
	static CSplashWnd* c_pSplashWnd;

// Generated message map functions
protected:
	//{{AFX_MSG(CSplashWnd)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnPaint();
	afx_msg void OnTimer(UINT nIDEvent);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


#endif


 

// CG: This file was added by the Splash Screen component.
// Splash.cpp : implementation file
//

#include "stdafx.h"  // e. g. stdafx.h
#include "resource.h"  // e.g. resource.h

#include "Splash.h"  // e.g. splash.h

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

/////////////////////////////////////////////////////////////////////////////
//   Splash Screen class

BOOL CSplashWnd::c_bShowSplashWnd;
CSplashWnd* CSplashWnd::c_pSplashWnd;
CSplashWnd::CSplashWnd()
{
}

CSplashWnd::~CSplashWnd()
{
	// Clear the static window pointer.
	ASSERT(c_pSplashWnd == this);
	c_pSplashWnd = NULL;
}

BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
	//{{AFX_MSG_MAP(CSplashWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSplashWnd::EnableSplashScreen(BOOL bEnable /*= TRUE*/)
{
	c_bShowSplashWnd = bEnable;
}

void CSplashWnd::ShowSplashScreen(CWnd* pParentWnd /*= NULL*/)
{
	if (!c_bShowSplashWnd || c_pSplashWnd != NULL)
		return;

	// Allocate a new splash screen, and create the window.
	c_pSplashWnd = new CSplashWnd;
	if (!c_pSplashWnd->Create(pParentWnd))
		delete c_pSplashWnd;
	else
		c_pSplashWnd->UpdateWindow();
}

BOOL CSplashWnd::PreTranslateAppMessage(MSG* pMsg)
{
	if (c_pSplashWnd == NULL)
		return FALSE;

	// If we get a keyboard or mouse message, hide the splash screen.
	if (pMsg->message == WM_KEYDOWN ||
	    pMsg->message == WM_SYSKEYDOWN ||
	    pMsg->message == WM_LBUTTONDOWN ||
	    pMsg->message == WM_RBUTTONDOWN ||
	    pMsg->message == WM_MBUTTONDOWN ||
	    pMsg->message == WM_NCLBUTTONDOWN ||
	    pMsg->message == WM_NCRBUTTONDOWN ||
	    pMsg->message == WM_NCMBUTTONDOWN)
	{
		c_pSplashWnd->HideSplashScreen();
		return TRUE;	// message handled here
	}

	return FALSE;	// message not handled
}

BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{
	if (!m_bitmap.LoadBitmap(IDB_BITMAP1))
		return FALSE;

	BITMAP bm;
	m_bitmap.GetBitmap(&bm);

	return CreateEx(0,
		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
		NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, pParentWnd->GetSafeHwnd(), NULL);
}

void CSplashWnd::HideSplashScreen()
{
	// Destroy the window, and update the mainframe.
	DestroyWindow();
	AfxGetMainWnd()->UpdateWindow();
}

void CSplashWnd::PostNcDestroy()
{
	// Free the C++ class.
	delete this;
}

int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// Center the window.
	CenterWindow();

	// Set a timer to destroy the splash screen.
	SetTimer(1, 750, NULL);

	return 0;
}

void CSplashWnd::OnPaint()
{
	CPaintDC dc(this);

	CDC dcImage;
	if (!dcImage.CreateCompatibleDC(&dc))
		return;

	BITMAP bm;
	m_bitmap.GetBitmap(&bm);

	// Paint the image.
	CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
	dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
	dcImage.SelectObject(pOldBitmap);
}

void CSplashWnd::OnTimer(UINT nIDEvent)
{
	// Destroy the splash screen window.
	HideSplashScreen();
}


导入之后

在InitInstance之中添加

\
	{
\
		CCommandLineInfo cmdInfo;
\
		ParseCommandLine(cmdInfo);
\

\
		CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
\
	}

 

在OnCreate(LPCREATESTRUCT lpCreateStruct)中

CSplashWnd::ShowSplashScreen(this);


 

另一种方法(另开线程)

#ifndef _SPLASH_H_
#define _SPLASH_H_

/************************************************************************ 
	类名:CSplashThread
	功能:显示启动画面

************************************************************************/ 

#include"SplashDlg.h"  //根据实际情况改变该路径

class CSplashThread : public CWinThread
{
	DECLARE_DYNCREATE(CSplashThread)  //动态创建

public:
	void HideSplash();		 

public:
	//{{AFXVIRTUAL(CSplashThread)  
	public:
	virtual BOOL InitInstance();
	virtual int ExitInstance();
	//}}AFX_VIRTUAL

protected:

	 CSplashDlg*  m_pSplashDlg;  //声明一个对话框指针

	//{{AFX_MSG(CSplashThread) 
	//}}AFX_MSG  

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
#endif
#include "stdafx.h"  
#include "resource.h" 
#include "Splash.h"  

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

IMPLEMENT_DYNCREATE(CSplashThread, CWinThread)  //动态创建

BOOL CSplashThread::InitInstance()
{

  //	说明:通常,系统内的每个线程都有自己的输入队列。本函数(既“连接线程输入函数”)允许线程和进程共享输入队列。连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态以及输入队列状态都会进入共享状态 

  ::AttachThreadInput(m_nThreadID,					  //欲连接线程的标识符(ID)
					  AfxGetApp()->m_nThreadID,		  //与idAttach线程连接的另一个线程的标识符
					  TRUE							  //TRUE(非零)连接,FALSE撤消连接
					  );         //这个函数可以不用!!

  m_pSplashDlg=new CSplashDlg;
  m_pSplashDlg->Create(IDD_DIALOG_SPLASH);
  m_pSplashDlg->ShowWindow(SW_SHOW); //在这个用户界面线程中创建对话框

  return true;
}


void CSplashThread::HideSplash()
{
	  m_pSplashDlg->SendMessage(WM_CLOSE);
}

int CSplashThread::ExitInstance()
{
	m_pSplashDlg->DestroyWindow();
	delete m_pSplashDlg;

	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CSplashThread, CWinThread)
	//{{AFX_MSG_MAP(CSplashThread)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


启动的就是IDD_DIALOG_SPLASH  具体操作在对话框中添加

 CSplashThread* pSplashThread;

在InitInstance() 中

    /**********************************启动画面*****************************************************/	
	 pSplashThread = (CSplashThread*)AfxBeginThread(RUNTIME_CLASS(CSplashThread), THREAD_PRIORITY_NORMAL, 0, 0);
    /**********************************启动画面*****************************************************/


 

 

### 回答1: QPainter是Qt框架中的绘图工具类,用于在绘图设备上绘制图形、图像和文本。当我们需要在绘图设备上进行复杂的绘制操作时,可能会遇到画面加载等待的情况。 在绘制过程中,如果绘制的内容较多或复杂,可能会导致画面加载速度变慢,进而引起画面加载等待的情况。这主要取决于绘制的对象数量、绘制的复杂度和绘图设备的性能等因素。 为了解决画面加载等待的问题,我们可以采取以下几种策略: 1. 使用异步绘制:异步绘制意味着在绘制过程中,程序可以继续执行其他任务,不需要等待绘制操作完成。可以使用Qt提供的多线程技术,将绘制任务放在一个独立的线程中进行,使得主线程不会因为绘制操作而被阻塞。 2. 使用局部刷新:当仅部分区域需要更新时,我们可以只对需要更新的区域进行绘制,而不是对整个画面进行重新绘制。这样可以减少绘制的工作量,提升绘制效率。 3. 使用双缓冲绘制:双缓冲绘制是指在内存中创建一个与绘图设备相对应的缓冲区,先将绘制操作在缓冲区中进行,等到绘制完成后再将缓冲区的内容一次性地复制到绘图设备上。这样可以避免绘制过程中的闪烁和加载等待。 除了以上策略,还可以通过优化绘制算法、减少不必要的绘制操作等方法来提高绘制效率,从而减少画面加载等待的时间。综上所述,通过采取适当的优化策略和技术手段,可以有效地解决qpainter画面加载等待的问题。 ### 回答2: QWidget 是 Qt 库中用于创建 GUI 程序的类之一,而 QPainter 是 QWidget 类中主要用于绘图的类之一。 当我们在 QWidget 的 paintEvent() 函数中使用 QPainter 进行绘图时,有时候绘图内容可能比较复杂,或者需要从远程服务器获取数据,这样就会导致绘图过程比较耗时。为了让用户知道程序正在加载或处理数据,我们可以使用 QPainter 来实现画面加载等待效果。 具体实现方法如下: 1. 在 QWidget 对象的成员变量中定义一个 QTimer 对象,用于控制定时器触发的时间间隔。 2. 在 paintEvent() 函数中,判断定时器是否处于活动状态,如果是,则绘制加载等待效果。 3. 在需要加载等待的地方,启动定时器,开始绘制加载等待效果。 4. 在数据处理完成或绘图完成时,停止定时器。 代码示例: ```cpp class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent) { QTimer* timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(500); // 控制定时器触发的时间间隔为 0.5 秒 } protected: void paintEvent(QPaintEvent *event) override { QPainter painter(this); if(m_isLoading) // 判断是否处于加载状态 { // 绘制加载等待效果 painter.drawText(rect(), Qt::AlignCenter, "正在加载,请稍候..."); } else { // 绘制其他内容 } } private slots: void startLoading() { m_isLoading = true; // 数据处理 m_isLoading = false; // 或者在处理完成后发送信号,由信号触发 paintEvent() 函数中的重绘 } private: bool m_isLoading = false; // 是否处于加载状态 }; ``` 这样,当需要加载等待时,我们就可以调用 startLoading() 函数,启动定时器,并在 paintEvent() 函数中绘制加载等待效果。数据处理完成后,设置 m_isLoading 为 false,停止定时器,等待界面会自动刷新,加载等待效果消失。 ### 回答3: QPainter是Qt框架中的一个功能强大的绘图类,可以用于绘制各种图形和图片。当我们在使用QPainter绘图时,有时可能会遇到画面加载等待的情况。 画面加载等待可能是由于以下几个原因导致的: 1. 绘图操作复杂:如果我们在绘图时需要进行复杂的计算和处理,比如绘制大量的图形、加载大图像或者进行复杂的图形变换等操作,那么绘图的速度就会受到影响,导致画面加载等待。 2. 数据加载耗时:如果我们在绘图时需要从外部数据源加载数据,比如从网络或者文件读取图片数据等,数据加载的速度可能会比较慢,从而导致画面加载等待。 3. 频繁绘制操作:如果我们在短时间内频繁地进行绘制操作,比如在一个循环中多次调用QPainter的绘制函数,那么可能会导致画面加载等待。 为了解决画面加载等待的问题,我们可以采取以下几个措施: 1. 使用异步加载数据:如果我们需要从外部数据源加载数据,可以考虑使用异步加载的方式,这样可以避免数据加载阻塞主线程,提高绘图的速度。 2. 合理使用缓存:对于一些计算比较复杂的图形或者图像,可以事先计算好并进行缓存,这样可以避免每次都进行复杂的计算,提高绘图的速度。 3. 优化绘图逻辑:在绘图时,可以考虑优化绘图逻辑,减少不必要的绘制操作,避免频繁绘制导致的加载等待。 通过以上措施的综合应用,我们可以提高QPainter的绘图效率,减少画面加载等待的时间,从而提升用户的使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值