我为我们新成立的社团制作了一个抽奖系统。这个抽奖系统也叫做抽签系统,它可以简单地对这些名字进行滚动。当按下了空格键和鼠标左键,名字就会停下来。我们社团打算利用这个软件对所有会员进行公正地抽签,然后让他们表演活动或者给予一些奖励。(本程序资源在这里:http://download.csdn.net/detail/jiangcaiyang123/3701969)
软件的截图如下所示:
这是一个很简单的系统。我把我的主函数的代码贴给大家:
#include <d3d9.h>
#include <d3dx9.h>
#include "Font.h"
#include "Input.h"
#pragma comment( lib, "d3d9.lib" )
#pragma comment( lib, "d3dx9.lib" )
// 定义的宏
#define NORMAL 0
#define MINIMIZED -1
#define LOSTFOCUS -2
// 全局常量
const int MEMBER_NUM = 86;
// 全局变量
LPDIRECT3D9 g_pD3D;// D3D对象
LPDIRECT3DDEVICE9 g_pDevice;// DirectX设备指针
D3DPRESENT_PARAMETERS g_D3dpp;// D3D当前参数结构体
short g_WindowState = NORMAL;
bool g_bFullScreen = false;
int clientWidth = 640;
int clientHeight = 480;
CFont info;// 字体
extern TCHAR szNameList[MEMBER_NUM][7];// 名单
CInput input;// 输入
HRESULT CALLBACK DefaultMsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 ); return 0;
case WM_SIZE:
if ( wParam == SIZE_MINIMIZED ) g_WindowState = MINIMIZED;
else g_WindowState = NORMAL;
break;
case WM_KILLFOCUS:
g_WindowState = LOSTFOCUS;
break;
case WM_SETFOCUS:
g_WindowState = NORMAL;
}
return (HRESULT)DefWindowProc( hWnd, msg, wParam, lParam );
}
inline void MainLoop( void )
{
// 静态变量
static unsigned int finalNum = 0;
static bool bRoll = true;
static TCHAR szPrompt[2][30] =
{
TEXT( " 文理CSDN高校俱乐部" ),
TEXT( "按空格或者鼠标左键继续" )
};
static int select = 0;
// 候选名单滚动
if ( bRoll )
{
finalNum++;
if ( finalNum == MEMBER_NUM )
{
finalNum = 0;
}
}
// 显示字体
info.Show( szPrompt[select], 20, 120 );
info.Show( szNameList[finalNum], 235, 300 );
input.UpdateKeyState( );
input.UpdateMouseState( );
// 输入判断
if ( input.KeyDown( DIK_SPACE ) || input.MouseButtonDown( 0 ) )
{
bRoll = !bRoll;
select = select == 1? 0: 1;
}
if ( input.KeyDown( DIK_ESCAPE ) ) exit( 0 );
// 全屏场景恢复
if ( FAILED( g_pDevice->TestCooperativeLevel() ) )
{
if ( g_pDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET )
{
info.Invalidate( );
g_pDevice->Reset( &g_D3dpp );
info.Validate( );
}
}
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int )
{
// 判断是否进行全屏显示
if ( MessageBox( NULL, TEXT( "是否进行全屏显示?" ), TEXT( "摇奖系统 - 蒋轶民制作" ), MB_ICONQUESTION | MB_YESNO ) == IDYES ) g_bFullScreen = true;
WNDCLASSEX wndClass;
// 设置成员wndClass的属性
wndClass.cbSize = sizeof( wndClass );
wndClass.style = CS_CLASSDC;
wndClass.lpfnWndProc = DefaultMsgProc;
wndClass.cbClsExtra = 0L;
wndClass.cbWndExtra = 0L;
wndClass.hInstance = hInst;
wndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndClass.hbrBackground = NULL;
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT( "摇奖系统" );
wndClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
RegisterClassEx( &wndClass );// 注册成员wndClass
/*---------------------------------*/
//创建D3D对象
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
if ( g_pD3D == NULL )
{
MessageBox( NULL, TEXT( "创建D3D对象失败。" ), TEXT( "摇奖系统 - 蒋轶民制作" ), MB_ICONSTOP );
return 1;
}
// 获取显示器的模式
D3DDISPLAYMODE displayMode;
if ( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode ) ) )
{
MessageBox( NULL, TEXT( "获取显示器的模式失败。" ), TEXT( "摇奖系统 - 蒋轶民制作" ), MB_ICONSTOP );
return 1;
}
// D3D显示的参数
ZeroMemory( &g_D3dpp, sizeof( g_D3dpp ) );
// 初始化D3DPRESENT_PARAMETERS
g_D3dpp.Windowed = !g_bFullScreen;
g_D3dpp.BackBufferWidth = clientWidth;
g_D3dpp.BackBufferHeight = clientHeight;
g_D3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
g_D3dpp.BackBufferFormat = displayMode.Format;
g_D3dpp.EnableAutoDepthStencil = TRUE;
g_D3dpp.AutoDepthStencilFormat = D3DFMT_D16;
/*---------------------------------*/
DWORD dwStyle;
if ( g_bFullScreen == true )
{
if ( clientWidth < 640 || clientHeight < 480 )
{
clientWidth = 640;
clientHeight = 480;
}
dwStyle = WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE;
}
else
{
clientWidth += 6;
clientHeight += 32;
dwStyle = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
}
// 创建窗口,并且将值传给窗口句柄
HWND hWnd = CreateWindow( TEXT( "摇奖系统" ), TEXT( "摇奖系统" ), dwStyle, 100, 40,
clientWidth, clientHeight, GetDesktopWindow(), NULL, hInst, NULL );
if ( hWnd == NULL )
{
MessageBox( NULL, TEXT( "创建窗口失败。" ), TEXT( "摇奖系统 - 蒋轶民制作" ), MB_ICONSTOP );
return 1;// 创建窗口失败
}
ShowWindow( hWnd, SW_SHOWDEFAULT );// 显示窗口
UpdateWindow( hWnd );// 更新窗口
// 创建设备
if ( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&g_D3dpp, &g_pDevice ) ) )
{
MessageBox( NULL, TEXT( "创建设备失败。" ), TEXT( "摇奖系统 - 蒋轶民制作" ), MB_ICONSTOP );
return 1;
}
// 创建字体
info.Initialize( g_pDevice, clientWidth, clientHeight );
// 创建输入
input.Initialize( BUFFERED, hInst, hWnd );
// 进入框架内部运行消息循环
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
// 处理消息
if ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
switch( g_WindowState )
{
case NORMAL:// 正常渲染
g_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 239, 211 ), 1.0f, 0);// 清除后台缓存
g_pDevice->BeginScene();// 开始渲染
MainLoop( );// 进入主循环
g_pDevice->EndScene();// 结束渲染
g_pDevice->Present( NULL, NULL, NULL, NULL );// 显示
break;
case MINIMIZED:// 最小化
WaitMessage();// 为了解决最小化时CPU占用高的问题,设立这条语句,即只等待消息
break;
case LOSTFOCUS:// 失去焦点
g_pDevice->Present( NULL, NULL, NULL, NULL );// 显示
}
}
}
return 0;
}
我的资源z在这里,大家可以下载: http://download.csdn.net/detail/jiangcaiyang123/3701969