VC屏保示例

參考文章:http://hi.baidu.com/daigongrei/item/529f0118c9827c426826bb48

開發工具:VS2012

系統環境:Win7


步驟:

1、新建一個MFC對話框程序。

2、導入bmp圖片,作爲屏保顯示圖。

3、豐富代碼,設置定時器,移動圖片。

4、修改exe後綴爲.scr。

5、複製該scr文件到C:\Windows\System32。

6、設置屏保爲該文件即可。


代碼如下。

QMProtectDlg.h

// QMProtectDlg.h : 頭文件
//

#pragma once


// CQMProtectDlg 對話框
class CQMProtectDlg : public CDialogEx
{
// 構造
public:
	CQMProtectDlg(CWnd* pParent = NULL);	// 標準構造函數

// 對話框數據
	enum { IDD = IDD_QMPROTECT_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持


// 實現
protected:
	HICON m_hIcon;

	// 生成的消息映射函數
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

private:
	UINT s_showH;//這是顯示屏的高度
	UINT s_showW;//這是顯示屏的寬度度
	UINT w_showX;//圖像要顯示的位置(從左到右的座標)
	CDC* w_pdcmem;//位圖內存,也就是和清明上河圖關聯的內存,
	CBitmap w_bitmap;//清明上河圖位圖
	CPoint w_point;       //記錄鼠標的位置
	void DrawBitmap();//畫圖的函數,這裏手動添加了。這樣方便快捷
public:
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnDestroy();
	afx_msg void OnActivateApp(BOOL bActive, DWORD dwThreadID);
};

QMProtectDlg.cpp

// QMProtectDlg.cpp : 實現文件
//

#include "stdafx.h"
#include "QMProtect.h"
#include "QMProtectDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define BITMAP_HEIGHT 469 //原圖像高度
#define BITMAP_WEIGHT 1024 //原圖像寬度

// CQMProtectDlg 對話框



CQMProtectDlg::CQMProtectDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CQMProtectDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQMProtectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CQMProtectDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	ON_WM_MBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_WM_SYSKEYDOWN()
	ON_WM_DESTROY()
	ON_WM_ACTIVATEAPP()
END_MESSAGE_MAP()


// CQMProtectDlg 消息處理程序

BOOL CQMProtectDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
	//  執行此操作
	SetIcon(m_hIcon, TRUE);			// 設置大圖標
	SetIcon(m_hIcon, FALSE);		// 設置小圖標

	// TODO: 在此添加額外的初始化代碼
	//設置全屏窗口
	CRect m_rcMain;
	GetWindowRect(&m_rcMain);//restore        the        src        screen's        size;   
			 //刪除窗口的標題欄
	LONG        style        =        GetWindowLong(m_hWnd,GWL_STYLE);   
	style &= ~WS_CAPTION;   
	SetWindowLong(m_hWnd,GWL_STYLE,style); //設置顯示窗口狀態  
	//獲得顯示器屏幕的寬度  
	s_showW        =        GetSystemMetrics(SM_CXSCREEN);   
	s_showH        =        GetSystemMetrics(SM_CYSCREEN);   
	//show        window   
	SetWindowPos(NULL,-23,-3,s_showW+27,s_showH+6,SWP_NOZORDER);
	//獲得鼠標的位置
	::GetCursorPos(&w_point);
	//隱藏鼠標
	ShowCursor(FALSE);

	return TRUE;  // 除非將焦點設置到控件,否則返回 TRUE
}

// 如果向對話框添加最小化按鈕,則需要下面的代碼
//  來繪製該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
//  這將由框架自動完成。

void CQMProtectDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用於繪製的設備上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使圖標在工作區矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 繪製圖標
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPaintDC dc(this); 
       w_pdcmem = new CDC;
       w_bitmap.LoadBitmap(IDB_BITMAP1);
       w_pdcmem->CreateCompatibleDC(&dc);       //建立與dc兼容的
       w_pdcmem->SelectObject(&w_bitmap);
       //sndPlaySound( "清明上河圖屏保音樂.wav" , SND_ASYNC | SND_LOOP );
       w_showX = 0;
       DrawBitmap();
       SetTimer(1,1,NULL);

		CDialogEx::OnPaint();
	}
}

void CQMProtectDlg::DrawBitmap()
{ 
	CClientDC dc(this);
	CDC dcmem;
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(&dc,s_showW,s_showH);
	dcmem.CreateCompatibleDC(&dc);
	dcmem.SelectObject(&bitmap);
	dcmem.SetBkMode(0);
	dcmem.FillRect(&CRect(0,0,s_showW,s_showH),
			  &CBrush(RGB(0,0,0))
		   );
	UINT y = (s_showH-BITMAP_HEIGHT)/2;
	dcmem.StretchBlt(
		   0,y,s_showW, BITMAP_HEIGHT, 
		   w_pdcmem,
		   w_showX,0,s_showW,BITMAP_HEIGHT-12,
		   SRCCOPY);
	dc.BitBlt(20,0,s_showW,s_showH,&dcmem,0,0,SRCCOPY);//顯示在屏幕上
	bitmap.DeleteObject();
}

//當用戶拖動最小化窗口時系統調用此函數取得光標
//顯示。
HCURSOR CQMProtectDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CQMProtectDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	w_showX += 1;
	if(w_showX>=BITMAP_WEIGHT){
		   w_showX = 0;
	} 
	DrawBitmap();

	CDialogEx::OnTimer(nIDEvent);
}


void CQMProtectDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	PostMessage(WM_CLOSE);

	CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CQMProtectDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	PostMessage(WM_CLOSE);

	CDialogEx::OnLButtonDown(nFlags, point);
}


void CQMProtectDlg::OnMButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	PostMessage(WM_CLOSE);

	CDialogEx::OnMButtonDown(nFlags, point);
}


void CQMProtectDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	// TODO: Add your message handler code here and/or call default
	if(	abs(point.x-w_point.x)>=200 ||
		abs(point.y-w_point.y)>=200)
	{
		PostMessage(WM_CLOSE);
	}

	CDialogEx::OnMouseMove(nFlags, point);
}


void CQMProtectDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	PostMessage(WM_CLOSE);

	CDialogEx::OnRButtonDown(nFlags, point);
}


void CQMProtectDlg::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息處理程序代碼和/或調用默認值
	PostMessage(WM_CLOSE);

	CDialogEx::OnSysKeyDown(nChar, nRepCnt, nFlags);
}


void CQMProtectDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此處添加消息處理程序代碼
	// TODO: Add your message handler code here
	delete w_pdcmem;
	KillTimer(1);//程序中用到了計時器。這裏關掉計時器
}


void CQMProtectDlg::OnActivateApp(BOOL bActive, DWORD dwThreadID)
{
	CDialogEx::OnActivateApp(bActive, dwThreadID);

	// TODO: 在此處添加消息處理程序代碼
	// TODO: Add your message handler code here
	if (!bActive) //is being deactivated
		   PostMessage(WM_CLOSE); 
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章