C++實現(xiàn)新年賀卡程序
更新時間:2019年04月07日 09:24:31 作者:陽光小少年
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)賀卡程序,C++應(yīng)用程序編寫的雪花賀卡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
用c++應(yīng)用程序編寫的雪花賀卡,逢年過節(jié)送給你自己身邊的親友吧
snow.cpp
/////////////////////////////////////////////////////////////////////////////// // Snow.cpp // Date: 2009-2-5 21:16 // A moving ball. // /////////////////////////////////////////////////////////////////////////////// #include <assert.h> #include "Snow.h" TCHAR strForWin1[] = "時間過得好快啊!"; TCHAR strForWin2[] = "開學(xué)已經(jīng)十周了..." ; TCHAR strForWin3[] = "你學(xué)的怎么樣了?"; TCHAR strForWin4[] = "有問題一定要及時讓我知道"; TCHAR strForWin5[] = "祝大家“小光棍節(jié)”快樂"; 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 } /*:: -- 作用域標(biāo)識符!如果是在MFC下編程的話,因為MFC封裝了API函數(shù),但是參數(shù)有的和API函數(shù)不一樣, 比如MFC封裝的函數(shù)一般都沒有句柄這個參數(shù),但是API函數(shù)都有, 所以在MFC編程中,如果你調(diào)用的是全局的API函數(shù)的話就要加::符號, 來通知編譯器你調(diào)用的是全局的API函數(shù),而不是MFC封裝的API函數(shù)! 當(dāng)然有的函數(shù)比如參數(shù)是個布爾型的,MFC封裝的函數(shù)和全局的API函數(shù)的參數(shù)相同, 編譯器默認(rèn)的是調(diào)用MFC封裝的函數(shù),所以你加不加::作用域標(biāo)識符都是一樣的!! 控制臺下編寫的程序用的就是API函數(shù)所以沒必要加::作用域標(biāo)識符的。 */ /////////////////////////////////////////////////////////////////////////////// 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 ); //設(shè)置定時器 ::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 ); }
源碼下載:賀卡程序
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c++如何控制對象的創(chuàng)建方式(禁止創(chuàng)建棧對象or堆對象)和創(chuàng)建的數(shù)量
這篇文章主要介紹了c++如何控制對象的創(chuàng)建方式和創(chuàng)建的數(shù)量,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08C++實現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找
這篇文章主要介紹了C++實現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找方法,涉及數(shù)組的操作,有值得借鑒的技巧,需要的朋友可以參考下2014-09-09