C++实现新年贺卡程序

用c++应用程序编写的雪花贺卡,逢年过节送给你自己身边的亲友吧

snow.cpp

///////////////////////////////////////////////////////////////////////////////
// Snow.cpp
// Date: 2009-2-5 21:16
// A moving ball.
//
///////////////////////////////////////////////////////////////////////////////

#include <assert.h>
#include "Snow.h"

TCHAR strForWin1[] = "时间过得好快啊!";
TCHAR strForWin2[] = "开学已经十周了..." ;
TCHAR strForWin3[] = "你学的怎么样了?";
TCHAR strForWin4[] = "有问题一定要及时让我知道";
TCHAR strForWin5[] = "祝大家“小光棍节”快乐";
TCHAR strForWin6[] = "  CJ Wang 2011.11.1";

const int nMaxHeight = 450;

#define ID_TIMER 1

///////////////////////////////////////////////////////////////////////////////

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, char* cmdParam, int cmdShow )
{
 char className[] = "Snow";
 MovingBall MovingBallClass( WindowsProcedure, className, hInst );
 MovingBallClass.Register();

 WinMaker win( "-- Have a joy here!", className, hInst );
 win.Show( cmdShow );

 MSG msg;
 int status;

 while( ( status = ::GetMessage( & msg, NULL, 0, 0 ) ) != 0 )
 {
 if ( status == -1 )
 return -1;
 ::TranslateMessage( & msg );
 ::DispatchMessage( & msg );
 }

 return msg.wParam;
}

///////////////////////////////////////////////////////////////////////////////

MovingBall::MovingBall( WNDPROC wndProc, const char* className, HINSTANCE hInstance )
{
 _class.style = 0;
 _class.lpfnWndProc = wndProc; // Windows procedure: mandatory
 _class.cbClsExtra = 0;
 _class.cbWndExtra = 0;
 _class.hInstance = hInstance;
 _class.hIcon = 0; // Owner of class: mandatory
 _class.hCursor = ::LoadCursor( 0, IDC_ARROW );
 _class.hbrBackground = (HBRUSH) ( COLOR_WINDOW + 1 ); // Optional
 _class.lpszMenuName = 0;
 _class.lpszClassName = className; // Mandatory
}

WinMaker::WinMaker( const char* szCaption, const char* className, HINSTANCE hInstance )
{
 DWORD dwStyle = WS_OVERLAPPEDWINDOW;
 dwStyle &= ~WS_SIZEBOX;
 dwStyle &= ~WS_MAXIMIZEBOX;
 dwStyle &= ~WS_MINIMIZEBOX;

 _hWnd = ::CreateWindow(
 className, // Name of a registered window class
 szCaption, // Window caption
 dwStyle, // Window style
 CW_USEDEFAULT, // x position
 CW_USEDEFAULT, // y position
 787, // width
 590, // height
 0, // Handle to parent window
 0, // Handle to menu
 hInstance, // Application instance
 0 ); // Window creation data
}

/*:: -- 作用域标识符!如果是在MFC下编程的话,因为MFC封装了API函数,但是参数有的和API函数不一样,
比如MFC封装的函数一般都没有句柄这个参数,但是API函数都有,
所以在MFC编程中,如果你调用的是全局的API函数的话就要加::符号,
来通知编译器你调用的是全局的API函数,而不是MFC封装的API函数!
当然有的函数比如参数是个布尔型的,MFC封装的函数和全局的API函数的参数相同,
编译器默认的是调用MFC封装的函数,所以你加不加::作用域标识符都是一样的!!

控制台下编写的程序用的就是API函数所以没必要加::作用域标识符的。
*/

///////////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WindowsProcedure( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam )
{
 static HBITMAP hbmpBkground = NULL,
 hbmpSnow = NULL,
 hbmpMask = NULL;
 static Snow snowFlakes[ 80 ];
 static int countSnow = 0;
 static int cxClient, cyClient;

 static int nHeightY = nMaxHeight;

 COLORREF clrBk;
 PAINTSTRUCT ps;
 HDC hdc = NULL,
 hdcMem = NULL;
 HINSTANCE hInst = NULL;

 switch( uMessage )
 {
 case WM_CREATE:
 hInst = ( (LPCREATESTRUCT) lParam )->hInstance;
 assert( hInst );

 hbmpBkground = ::LoadBitmap( hInst, TEXT( "bground" ) );
 assert( hbmpBkground );
 hbmpSnow = ::LoadBitmap( hInst, TEXT( "snow" ) );
 assert( hbmpSnow );
 hbmpMask = ::LoadBitmap( hInst, TEXT( "mask" ) );
 assert( hbmpMask );
 ::SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

 //设置定时器
 ::SetTimer( hWnd, ID_TIMER, 160, NULL );
 return 0;

 case WM_SIZE:
 cxClient = LOWORD( lParam );
 cyClient = HIWORD( lParam );
 return 0;

 case WM_PAINT:
 hdc = ::BeginPaint( hWnd, & ps );
 assert( hdc );
 hdcMem = ::CreateCompatibleDC( hdc );
 assert( hdcMem );

 ::SelectObject( hdcMem, hbmpBkground );
 ::BitBlt(
 hdc,
 0, 0,
 cxClient, cyClient,
 hdcMem,
 0, 0,
 SRCCOPY );

 ::DeleteDC( hdcMem );
 ::EndPaint( hWnd, & ps );
 return 0;

 case WM_TIMER:
 ::FlashWindow( hWnd, TRUE );
 if ( countSnow < 80 )
 {
 snowFlakes[ countSnow ].xPos = rand() % cxClient;
 snowFlakes[ countSnow ].yPos = 0;
 snowFlakes[ countSnow ].bIsExist = TRUE;
 countSnow++;
 }

 if ( countSnow == 80 )
 countSnow = 0;

 hdc = ::GetDC( hWnd );
 assert( hdc );
 hdcMem = ::CreateCompatibleDC( hdc );
 assert( hdcMem );

 ::SelectObject( hdcMem, hbmpBkground );
 ::BitBlt(
 hdc,
 0, 0,
 cxClient, cyClient,
 hdcMem,
 0, 0,
 SRCCOPY );

 clrBk = ::GetBkColor( hdc );
 ::SetTextColor( hdc, RGB( 0, 11, 255 ) );
 ::SetBkColor( hdc, clrBk );
 ::TextOut( hdc, 100, nHeightY, strForWin1, lstrlen( strForWin1 ) );
 ::TextOut( hdc, 100, nHeightY + 18, strForWin2, lstrlen( strForWin2 ) );
 ::TextOut( hdc, 100, nHeightY + 36, strForWin3, lstrlen( strForWin3 ) );
 ::TextOut( hdc, 100, nHeightY + 54, strForWin4, lstrlen( strForWin4 ) );
 ::TextOut( hdc, 100, nHeightY + 70, strForWin5, lstrlen( strForWin5 ) );
 ::TextOut( hdc, 100, nHeightY + 88, strForWin6, lstrlen( strForWin6 ) );

 //
 // The redraw area for the text
 //
 nHeightY -= 10;

 if ( nHeightY <= -88 )
 {
 nHeightY = nMaxHeight;
 }

 int i;
 for ( i = 0; i < 80; i++ )
 {
 if ( snowFlakes[ i ].bIsExist )
 {
 ::SelectObject( hdcMem, hbmpMask );
 ::BitBlt(
 hdc,
 snowFlakes[ i ].xPos, snowFlakes[ i ].yPos,
 20, 20,
 hdcMem,
 0, 0,
 SRCAND );

 ::SelectObject( hdcMem, hbmpSnow );
 ::BitBlt(
 hdc,
 snowFlakes[ i ].xPos, snowFlakes[ i ].yPos,
 20, 20,
 hdcMem,
 0, 0,
 SRCPAINT );

 if ( rand() % 2 == 0 )
 snowFlakes[ i ].xPos += 3;
 else
 snowFlakes[ i ].xPos -= 3;

 snowFlakes[ i ].yPos += 10;

 if ( snowFlakes[ i ].yPos > cyClient )
 {
 snowFlakes[ i ].xPos = rand() % cxClient;
 snowFlakes[ i ].yPos = 0;
 }
 }
 }

 ::ReleaseDC( hWnd, hdc );
 ::DeleteDC( hdcMem );
 return 0;

 case WM_DESTROY:
 ::DeleteObject( hbmpBkground );
 ::DeleteObject( hbmpSnow );
 ::DeleteObject( hbmpMask );
 ::KillTimer( hWnd, ID_TIMER );
 ::PostQuitMessage( 0 );
 return 0;
 }

 return ::DefWindowProc( hWnd, uMessage, wParam, lParam );
}

源码下载:贺卡程序

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)

    遇到一个小需求, 快速搞定. 来看看用C/C++代码检测ip能否ping通: #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> using namespace std; string getCmdResult(const string &strCmd) // 这个是获取命令执行的结果, 类似于

  • C++11获取线程返回值的实现代码

    C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会忽略顶层函数的返回值. 那问题来了,我们要怎么获得线程的返回值呢? 我们通过一个例子来说明如何实现这个需求. 假设我们的app会创建一个线程来压缩一个文件夹,该线程在压缩完文件夹后会返回压缩文件 *.zip 和这个zip文件的大小,我们现在就想获得这个线程的返回值. 有两种方法可以实现这个需求: 1

  • C++中rapidjson将map转为json的方法

    rapidjson将map转为json------人生苦短,我用rapidjson 直接撸代码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #includ

  • C++获取MD5算法实现代码

    这个是网上扒下来的 作者已经无法知道是谁了 MD5.h #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type define */ typedef unsigned char byte; typedef unsigned int uint32; typedef unsigned int uint4; using std::string; using std::ifstream; /* MD5

  • c++文件监控之FileSystemWatcher

    具体代码如下: #using <System.dll> #include <iostream> using namespace std; using namespace System; using namespace System::IO; using namespace System::Security::Permissions; public ref class Watcher { private: // Define the event handlers. static vo

  • C++中rapidjson组装map和数组array的代码示例

    rapidjson组装map和数组array的代码示例 直接上码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjs

  • C++获取特定进程CPU使用率的实现代码

    近来发现笔记本在关闭屏幕后风扇转得特别快,打开屏幕后看任务管理器,风扇马上减速,也没有发现大量占用CPU的进程.于是想写一个小程序在后台记录每个进程的CPU使用情况,揪出锁屏后占用CPU的进程.于是自己写了一个C++类CPUusage,方便地监视不同进程的CPU占用情况.本人编程还只是个新手,如有问题请多多指教( •̀ ω •́ )! 计算原理为调用GetProcessTimes(),与上次调用得到的结果相减得到CPU占用时间,再除以两次调用的时间差,从而得到占用百分比.其中OpenProces

  • C++ 获取进程CPU占用率

    核心代码 // 时间转换 static __int64 file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } // 获得CPU的核数 static int get_processor_number() { SYSTEM_INFO info; Ge

  • C++基于消息队列的多线程实现示例代码

    前言 实现消息队列的关键因素是考量不同线程访问消息队列的同步问题.本实现涉及到几个知识点 std::lock_guard 介绍 std::lock_gurad 是 C++11 中定义的模板类.定义如下: template <class Mutex> class lock_guard; lock_guard 对象通常用于管理某个锁(Lock)对象,因此与 Mutex RAII 相关,方便线程对互斥量上锁,即在某个 lock_guard 对象的声明周期内,它所管理的锁对象会一直保持上锁状态:而 l

  • C++ 设置控制台(命令行)窗口 光标位置,及前背景颜色

    核心代码 #include "stdafx.h" #include <stdio.h> #include <windows.h> /* #define FOREGROUND_BLUE 0x0001 // text color contains blue. #define FOREGROUND_GREEN 0x0002 // text color contains green. #define FOREGROUND_RED 0x0004 // text color

随机推荐