zoukankan      html  css  js  c++  java
  • 《随机出题软件》&《随机分队软件》源码(Windows API)

    1 引言 


    1.1 编写目的:

    为了对院级活动《最强大脑》提供软件支持,同时为了练习使用windows API。

    1.2 项目背景:

    来自计算机学院学生会信息部指派的任务,规定时间完成软件的编写。

    1.3 参考资料: 

    ● 《windows程序设计》、网络资料
    ● 文档格式来自 http://blog.csdn.net/qjfpjie/article/details/7604834 由详细设计说明书改编

    2 总体设计 


    2.1 需求概述 

    《随机出题软件》:分程序和题库,题库为3、5、7分题库。程序界面需提供这三种题的按钮,需要具有从题库中随机抽取题目、显示题目、显示答案以及倒计时功能。

    《随机分队软件》:随机分好队伍。分别实现30人选5人,10人选5人。选出来的是编号。

    3 程序描述 


     《随机出题软件》:

    题库文件“1.txt、2.txt、3.txt”,答案文件分别对应“a1.txt、a2.txt、a3.txt”,放在程序根目录下。

    题库中存题目的格式应该是"#12 李白字什么?",其中题号和题目描述不能连在一起。

    题目描述中不能出现‘#’字符,否则误认为后面是题号。

    3.1 逐个模块给出以下说明: 

    ● 功能 

    《随机出题软件》:

    随机抽取题库中题目、显示题目、显示答案、倒计时、声效提示、自动更新题库大小。

    《随机分队软件》:

    30选5、10选5、显示分队结果。

    ● 性能 

    《随机出题软件》:

    题库如果过大,软件会卡死,显示的题目不能太长,所以要规范好题库文件。

    3.2 算法:

    《随机出题软件》:

    核心算法为调用rand()及srand()实现随机出题功能,另无高深算法。

    ● 存储分配 

    利用txt文件存储,而没使用数据库,存储以及管理数据较为麻烦。

    4 程序展示

    《随机出题软件》:

    文件结构:

    使用演示:

    《随机分队软件》:

    文件结构:

    使用演示:

    5 源代码


    目录: 

    《随机出题软件》myRandom.cpp
    《随机出题软件》win1.cpp
    《随机出题软件》myRandom.h
    《随机出题软件》varset.h
    《随机出题软件》win2.cpp

    源代码下载地址(包含题库):

    http://pan.baidu.com/s/1kTI81nx

    部分代码展示:

    《随机出题软件》:

     - 主要的cpp文件

    myRandom.cpp

      1 #include "myRandom.h"
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <windows.h>
      5 
      6 extern int QT_1,QT_2,QT_3;
      7 
      8 int random(int qt)    //传入题库号和该题库使用记录,然后随机输出该题库中的一个题的题号
      9 {
     10     int num;
     11     switch(qt){
     12         case 1:num = rand() % QT_1 + 1;break;   //产生指定题库的随机题号
     13         case 2:num = rand() % QT_2 + 1;break;
     14         case 3:num = rand() % QT_3 + 1;break;
     15         default:num=0;
     16     }
     17     return num;
     18 }
     19 
     20 
     21 int print_qt(int qt,int num,char question[])   //将指定题库(qt)中指定题号(num)的题的内容输出出来
     22 {
     23     int len = 0;
     24     //将“qt.txt”放到字符数组filename中,根据qt的不同可改变
     25     char filename[20];
     26     int t=qt,i=0;
     27     while(t/10){    //将题库号qt输入进filename中
     28         filename[i++]=t/10+'0';
     29         t/=10;
     30     }
     31     filename[i++]=t+'0';
     32     char* p = filename;
     33     strcpy(p+i,".txt"); //将.txt补上
     34     //cout<<filename<<endl;
     35 
     36     //用filename代表的文件作输入重定向
     37     if(freopen(filename,"r",stdin)==NULL){
     38         MessageBox(NULL,TEXT("抽题过程中产生错误! 错误号:1"),TEXT("ERROR"),MB_ICONERROR);
     39         //fprintf(stderr,"打开%d.txt文件错误!
    ",qt);
     40         fflush(stdin);
     41         if(freopen("CON","r",stdin)==NULL);
     42         return 0;
     43     }
     44     char c;
     45     int n=0;
     46     while((c=getchar())!=EOF){
     47         if(c=='#'){     //遇到'#'检测后面数字是否为指定题号
     48             scanf("%d",&n);
     49             if(n==num)  //找到该题
     50                 break;
     51         }
     52     }
     53     if(c==EOF){        //没有找到该题号
     54         MessageBox(NULL,TEXT("抽题过程中产生错误! 错误号:2"),TEXT("ERROR"),MB_ICONERROR);
     55         return 0;
     56     }
     57     if(!n){
     58         MessageBox(NULL,TEXT("抽题过程中产生错误! 错误号:3"),TEXT("ERROR"),MB_ICONERROR);
     59         //fprintf(stderr,"没有在题库中找到当前随机号码的题目,请debug程序找出错误。
    ");
     60         fflush(stdin);
     61         if(freopen("CON","r",stdin)==NULL);
     62         //    fprintf(stderr,"重定向控制台错误!
    ");
     63         return 0;
     64     }
     65     else{
     66         //找到该题,输出内容
     67         while((c=getchar())=='#' || c=='
    ');    //前面若有 # 或者 回车,直接忽略
     68         if(c==EOF)
     69             return len;
     70         do{
     71             question[len++] = c;
     72         }while((c=getchar())!='#' && c!=EOF);
     73         question[len] = '';
     74         fflush(stdin);
     75         if(freopen("CON","r",stdin)==NULL);
     76         //    fprintf(stderr,"重定向控制台错误!
    ");
     77         return len;
     78     }
     79     //return len;
     80 }
     81 
     82 int getAnswer(int TK,int QUESTION,char answer[1000])
     83 {
     84     int len = 0;    //answer长度
     85     //将“qt.txt”放到字符数组filename中,根据qt的不同可改变
     86     char filename[20];
     87     filename[0] = 'a';
     88     int t=TK,i=1;
     89     while(t/10){    //将题库号qt输入进filename中
     90         filename[i++]=t/10+'0';
     91         t/=10;
     92     }
     93     filename[i++]=t+'0';
     94     char* p = filename;
     95     strcpy(p+i,".txt"); //将.txt补上
     96     //cout<<filename<<endl;
     97 
     98     //用filename代表的文件作输入重定向
     99     if(freopen(filename,"r",stdin)==NULL){
    100         MessageBox(NULL,TEXT("提取答案过程中产生错误! 错误号:1"),TEXT("ERROR"),MB_ICONERROR);
    101         //fprintf(stderr,"打开%d.txt文件错误!
    ",qt);
    102         fflush(stdin);
    103         if(freopen("CON","r",stdin)==NULL);
    104             //fprintf(stderr,"重定向控制台错误!
    ");
    105         return 0;
    106     }
    107     char c;
    108     int n=0;
    109     while((c=getchar())!=EOF){
    110         if(c=='#'){     //遇到'#'检测后面数字是否为指定题号
    111             scanf("%d",&n);
    112             if(n==QUESTION)  //找到该题
    113                 break;
    114         }
    115     }
    116     if(c==EOF){        //没有找到该题号
    117         MessageBox(NULL,TEXT("提取答案过程中产生错误! 错误号:2"),TEXT("ERROR"),MB_ICONERROR);
    118         return 0;
    119     }
    120     if(!n){
    121         MessageBox(NULL,TEXT("提取答案过程中产生错误! 错误号:3"),TEXT("ERROR"),MB_ICONERROR);
    122         //fprintf(stderr,"没有在题库中找到当前随机号码的题目,请debug程序找出错误。
    ");
    123         fflush(stdin);
    124         if(freopen("CON","r",stdin)==NULL);
    125         //    fprintf(stderr,"重定向控制台错误!
    ");
    126         return 0;
    127     }
    128     else{
    129         //找到该题,输出内容
    130         while((c=getchar())=='#' || c=='
    ');    //前面若有 # 或者 回车,直接忽略
    131         if(c==EOF)
    132             return len;
    133         do{
    134             answer[len++] = c;
    135         }while((c=getchar())!='#' && c!=EOF);
    136         answer[len] = '';
    137         fflush(stdin);
    138         if(freopen("CON","r",stdin)==NULL);
    139         //    fprintf(stderr,"重定向控制台错误!
    ");
    140         return len;
    141     }
    142 }
    143 int FindQtNum(int qt)
    144 {
    145     int num=0;
    146     
    147     int len = 0;
    148     //将“qt.txt”放到字符数组filename中,根据qt的不同可改变
    149     char filename[20];
    150     int t=qt,i=0;
    151     while(t/10){    //将题库号qt输入进filename中
    152         filename[i++]=t/10+'0';
    153         t/=10;
    154     }
    155     filename[i++]=t+'0';
    156     char* p = filename;
    157     strcpy(p+i,".txt"); //将.txt补上
    158 
    159     //用filename代表的文件作输入重定向
    160     if(freopen(filename,"r",stdin)==NULL){
    161         MessageBox(NULL,TEXT("查找题库大小过程中产生错误! 错误号:1"),TEXT("ERROR"),MB_ICONERROR);
    162         //fprintf(stderr,"打开%d.txt文件错误!
    ",qt);
    163         fflush(stdin);
    164         if(freopen("CON","r",stdin)==NULL);
    165             //fprintf(stderr,"重定向控制台错误!
    ");
    166         return 0;
    167     }
    168     char c;
    169     int n=0;
    170     while((c=getchar())!=EOF){
    171         if(c=='#'){     //遇到'#'检测后面数字是否为指定题号
    172             num++;
    173         }
    174     }
    175 
    176     return num;
    177 }

     win1.cpp

      1 // win1.cpp : Defines the entry point for the application.
      2 //
      3 #include <windows.h>
      4 #include <mmsystem.h>
      5 #include <ctime>
      6 #include "resource.h"
      7 #include "myRandom.h"
      8 #include "varset.h"        //各类相关变量设置
      9 #include "stdafx.h"
     10 #define LASTTIME    30
     11 #pragma comment(lib, "WINMM.LIB")
     12 
     13 extern int QT_1 = 0,QT_2 = 0,QT_3 = 0;    //每个题库中题目,初始化为0
     14 
     15 bool isuse1[1001] = {0};        //题目使用记录    //抽到的题赋true,初始化为false
     16 bool isuse2[1001] = {0};        //题目使用记录
     17 bool isuse3[1001] = {0};        //题目使用记录
     18 
     19 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
     20 BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
     21 BOOL CALLBACK AboutProc4(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
     22 
     23 int APIENTRY WinMain(HINSTANCE hInstance,
     24                      HINSTANCE hPrevInstance,
     25                      LPSTR     lpCmdLine,
     26                      int       nCmdShow)
     27 {
     28      // TODO: Place code here.
     29     static TCHAR szmywndName[] = TEXT("Win1");
     30     HWND hwnd;
     31     MSG msg;
     32 
     33     //给全局变量赋值
     34     hInst = hInstance;    
     35     //设置父窗口位置和尺寸
     36     int width = GetSystemMetrics ( SM_CXSCREEN ); 
     37     int height= GetSystemMetrics ( SM_CYSCREEN ); 
     38     CCW_X = width / 10 * 3;
     39     CCW_Y = height / 3;
     40     CCW_WIDTH = width / 10 * 4;
     41     CCW_HEIGHT = height / 10 * 4;
     42     //设置按钮尺寸
     43     CBN_WIDTH = int(CCW_WIDTH / 10.0 * 2);
     44     CBN_HEIGHT = int(CCW_HEIGHT / 6.0);
     45     //设置静态文本框尺寸
     46     CSTA_WIDTH = int(CCW_WIDTH /10.0 * 8);
     47     CSTA_HEIGHT = int(CCW_HEIGHT /100.0 * 60);
     48 
     49     WNDCLASS mywnd;
     50     mywnd.cbClsExtra = 0;
     51     mywnd.cbWndExtra = 0;
     52     mywnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    //背景
     53     mywnd.hCursor = LoadCursor(NULL,IDC_ARROW);        //光标
     54     //mywnd.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1));    //图标
     55     mywnd.hIcon = (HICON)LoadImage(NULL,"icon2.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
     56     mywnd.hInstance = hInstance;                    //设置句柄
     57     mywnd.lpfnWndProc = WndProc;                    //设置窗口过程
     58     mywnd.lpszClassName = szmywndName;                //设置窗口类名字
     59     mywnd.lpszMenuName = NULL;                        //设置菜单名字
     60     mywnd.style = CS_HREDRAW | CS_VREDRAW;            //设置窗口风格
     61     //LoadImage(NULL,"D:\win1\icon.png",IMAGE_ICON,0,0,LR_LOADFROMFILE);
     62 
     63     if(!RegisterClass(&mywnd)){        //注册窗口类
     64         MessageBox(NULL,TEXT("This program requires Windows NT!"),szmywndName,MB_ICONERROR);
     65         return 0;
     66     }
     67 
     68     //创建窗口
     69     hwnd = CreateWindow(szmywndName,
     70                         TEXT("自动抽题程序 --- freecode"),
     71                         WS_OVERLAPPEDWINDOW^WS_THICKFRAME ^ WS_MAXIMIZEBOX ,
     72                         CCW_X,
     73                         CCW_Y,
     74                         CCW_WIDTH,
     75                         CCW_HEIGHT,
     76                         NULL,
     77                         NULL,
     78                         hInstance,
     79                         NULL);
     80     mainhwnd = hwnd;    //给全局变量赋值
     81 
     82     ShowWindow(hwnd,nCmdShow);        //显示窗口
     83     UpdateWindow(hwnd);                //升级窗口
     84 
     85     while(GetMessage(&msg,NULL,0,0)){    //消息循环
     86         TranslateMessage(&msg);
     87         DispatchMessage(&msg);
     88     }
     89 
     90     return msg.wParam;
     91 }
     92 
     93 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
     94 {
     95     HWND        hwndButton1,hwndButton2,hwndButton3;
     96     HDC            hdc;
     97     PAINTSTRUCT ps;
     98     RECT        rect;
     99     int            wmId,wmEvent;
    100     HFONT        hFont;
    101 
    102     switch(message){
    103     case WM_CREATE:
    104         //hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
    105 
    106         srand((unsigned)time(0));   //设置产生随机数的种子
    107         //memset(isuse1,0,sizeof(isuse1));    //不知道为什么不能使用
    108         //memset(isuse2,0,sizeof(isuse2));
    109         //memset(isuse3,0,sizeof(isuse3));
    110 
    111         //查找每个题库中题目数量
    112         QT_1 = FindQtNum(1);
    113         QT_2 = FindQtNum(2);
    114         QT_3 = FindQtNum(3);
    115 
    116         //创建 按钮(3分题)
    117         hwndButton1   =  CreateWindow(TEXT("button"),
    118                                     TEXT("3分题"),
    119                                     WS_TABSTOP | WS_CHILD | WS_VISIBLE ,
    120                                     int(CCW_WIDTH / 10.0 * 1),int(CCW_HEIGHT / 6.0 * 3),CBN_WIDTH,CBN_HEIGHT,
    121                                     hwnd,
    122                                     (HMENU)1,
    123                                     hInst,
    124                                     NULL);
    125         //设置字体
    126         LOGFONT LogFont;
    127         ::memset(&LogFont, 0, sizeof(LOGFONT));
    128         lstrcpy(LogFont.lfFaceName, "隶书");
    129           LogFont.lfWeight = 100;
    130         LogFont.lfHeight = 30;//-44; // 字体大小
    131         LogFont.lfCharSet = 134;
    132         LogFont.lfOutPrecision = 3;
    133         LogFont.lfClipPrecision = 2;
    134         LogFont.lfOrientation = 45;
    135         LogFont.lfQuality = 1;
    136         LogFont.lfPitchAndFamily = 2;
    137 
    138         // 创建字体
    139         hFont = CreateFontIndirect(&LogFont);
    140         // 设置控件字体
    141         SendMessage(hwndButton1, WM_SETFONT, (WPARAM)hFont, 0); 
    142 
    143         //创建 按钮(5分题)
    144         hwndButton2   =  CreateWindow(TEXT("button"),
    145                                     TEXT("5分题"),
    146                                     WS_TABSTOP | WS_CHILD | WS_VISIBLE ,
    147                                     int(CCW_WIDTH / 10.0 * 3.8),int(CCW_HEIGHT / 6.0 * 3),CBN_WIDTH,CBN_HEIGHT,
    148                                     hwnd,
    149                                     (HMENU)2,
    150                                     hInst,
    151                                     NULL);
    152         // 设置控件字体
    153         SendMessage(hwndButton2, WM_SETFONT, (WPARAM)hFont, 0); 
    154 
    155 
    156         //创建 按钮(7分题)
    157         hwndButton3   =  CreateWindow(TEXT("button"),
    158                                     TEXT("7分题"),
    159                                     WS_TABSTOP | WS_CHILD | WS_VISIBLE ,
    160                                     int(CCW_WIDTH / 10.0 * 6.6),int(CCW_HEIGHT / 6.0 * 3),CBN_WIDTH,CBN_HEIGHT,
    161                                     hwnd,
    162                                     (HMENU)3,
    163                                     hInst,
    164                                     NULL);
    165         // 设置控件字体
    166         SendMessage(hwndButton3, WM_SETFONT, (WPARAM)hFont, 0); 
    167 
    168 
    169         return 0;
    170     case WM_COMMAND:
    171         wmId    = LOWORD(wParam); 
    172         wmEvent = HIWORD(wParam);
    173         switch(wmId){
    174             case 1:        //单击3分题按钮,开始在3分题库里,抽题
    175                 /* 非模态
    176                 hdlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,AboutProc);
    177                 if(hdlg)  
    178                 {  
    179                     //显示对话框  
    180                     ShowWindow(hdlg, SW_NORMAL);  
    181                 } 
    182                 */
    183 
    184                 TK = 1;
    185                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd,AboutProc);
    186 
    187                 //hdlg = FindWindow(MAKEINTRESOURCE(IDD_DIALOG1),NULL);    //获得对话框句柄
    188                 break;
    189 
    190 
    191             case 2:        //单击5分题按钮,开始在3分题库里,抽题
    192                 TK = 2;
    193                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd,AboutProc);    
    194                 break;
    195 
    196 
    197             case 3:        //单击7分题按钮,开始在3分题库里,抽题
    198                 TK = 3;
    199                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd,AboutProc);    
    200                 break;
    201             default:
    202                 break;
    203         }
    204         return 0;
    205     case WM_PAINT:
    206         hdc = BeginPaint(hwnd,&ps);    //获得句柄
    207         
    208         GetClientRect(hwnd,&rect);    //获得客户区尺寸
    209         rect.bottom -= CBN_HEIGHT * 2;
    210         //显示文字
    211         hFont = CreateFont(
    212                         70,0,    //高度20, 宽取0表示由系统选择最佳值
    213                         0, 0,    //文本倾斜,与字体倾斜都为0
    214                         FW_HEAVY,    //粗体
    215                         0,0,0,        //非斜体,无下划线,无中划线
    216                         GB2312_CHARSET,    //字符集
    217                         OUT_DEFAULT_PRECIS,        
    218                         CLIP_DEFAULT_PRECIS,        
    219                         DEFAULT_QUALITY,        //一系列的默认值
    220                         DEFAULT_PITCH | FF_DONTCARE,    
    221                         "楷体"    //字体名称
    222                     );
    223         SetTextColor(hdc, RGB(0, 255, 0));    //设置文本为绿色
    224         SelectObject(hdc, hFont);
    225 
    226         DrawText(hdc,TEXT("抽 题 环 节"),-1,&rect,
    227             DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    228         
    229         EndPaint(hwnd,&ps);
    230         ReleaseDC(hwnd,hdc);
    231         return 0;
    232     case WM_DESTROY:
    233         PostQuitMessage(0);
    234         return 0;
    235     }
    236     return DefWindowProc(hwnd,message,wParam,lParam);
    237 }
    238 BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)    //题目显示窗口
    239 {
    240     int            t;
    241     int            qtsum;        //题目总数
    242     bool*        isu;        //存储访问数组
    243     char        question[1010];        //题目描述
    244     int            qtnum;        //随机产生的题号
    245     HWND        hwndStatic,hwndStatic3;    //存储静态文本框句柄
    246     HFONT        hFont;
    247 
    248     switch (message){
    249         case WM_INITDIALOG :
    250             //PlaySound("d:\win1\last2.wav", NULL, SND_FILENAME | SND_ASYNC);
    251             PlaySound(".\素材\last2.wav", NULL, SND_FILENAME | SND_ASYNC);
    252             SetTimer(hDlg, 1, 1000, NULL);    //设置一个1s定时器
    253             num = LASTTIME;
    254             SetDlgItemInt(hDlg, IDC_STATIC3, num--, FALSE);
    255 
    256             //设置 IDC_STATIC3 倒计时字体
    257             LOGFONT LogFont;
    258             ::memset(&LogFont, 0, sizeof(LOGFONT));
    259             lstrcpy(LogFont.lfFaceName, "华文新魏");
    260               LogFont.lfWeight = 600;
    261             LogFont.lfHeight = 80;//-44; // 字体大小
    262             LogFont.lfCharSet = 134;
    263             LogFont.lfOutPrecision = 3;
    264             LogFont.lfClipPrecision = 2;
    265             LogFont.lfOrientation = 45;
    266             LogFont.lfQuality = 1;
    267             LogFont.lfPitchAndFamily = 2;
    268 
    269             // 创建字体
    270             hFont = CreateFontIndirect(&LogFont);
    271 
    272             // 取得控件句柄
    273             hwndStatic3 = GetDlgItem(hDlg, IDC_STATIC3);
    274             // 设置控件字体
    275             SendMessage(hwndStatic3, WM_SETFONT, (WPARAM)hFont, 0); 
    276             /*    不知道为什么不能用
    277             hdc3 = GetDC(hwndStatic3);
    278             SetTextColor(hdc3,RGB(0,128,128));//设置字体颜色
    279             */
    280 
    281             //选择题库
    282             switch(TK){
    283             case 1:
    284                 qtsum = QT_1;
    285                 isu = isuse1;
    286                 break;
    287             case 2:
    288                 qtsum = QT_2;
    289                 isu = isuse2;
    290                 break;
    291             case 3:
    292                 qtsum = QT_3;
    293                 isu = isuse3;
    294                 break;
    295             default:
    296                 break;
    297             }
    298             for(t=1;t<=qtsum;t++)    //检测题目中有没有没使用过的题
    299                 if(!isu[t])
    300                     break;
    301             if(t>qtsum){
    302                 EndDialog (hDlg, 0) ;
    303                 MessageBox(NULL,TEXT("该题库中的题已全部抽完!"),TEXT("ERROR"),MB_ICONERROR);
    304                 break;
    305             }
    306             while(1){
    307                 qtnum = random(TK);    //在TK题库里抽题。
    308                 if(!isu[qtnum]){    //出到没被使用过的题
    309                     isu[qtnum] = true;
    310                     break;
    311                 }
    312             }
    313 
    314             QUESTION = qtnum;
    315             
    316             hwndStatic = GetDlgItem(hDlg,IDC_STATIC1);
    317 
    318             if(print_qt(TK,qtnum,question)){    //抽题成功,题目放在question字符数组里(抽题失败的话函数中有警示代码,并返回0,无需理会)
    319                 SetWindowText(hwndStatic, question);
    320             }
    321             else return false;
    322             
    323             return TRUE ;
    324         case WM_COMMAND :
    325             switch (LOWORD (wParam)){
    326                 case IDOK : //答案
    327                     EndDialog (hDlg, 0) ;
    328                     PlaySound(NULL, NULL, SND_FILENAME | SND_ASYNC);
    329                     DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), mainhwnd,AboutProc4);
    330                     break;
    331                 case IDCANCEL :
    332                     PlaySound(NULL, NULL, SND_FILENAME | SND_ASYNC);
    333                     EndDialog (hDlg, 0) ;
    334                     return TRUE ;
    335             }
    336             break;
    337         case WM_TIMER:
    338             if(num>5){
    339                 SetDlgItemInt(hDlg, IDC_STATIC3, num--, FALSE);
    340             }
    341             else if(num==5){
    342                 PlaySound(".\素材\last1.wav", NULL, SND_FILENAME | SND_ASYNC);
    343                 SetDlgItemInt(hDlg, IDC_STATIC3, num--, FALSE);
    344             }
    345             else if(0<=num && num<5){
    346                 PlaySound(".\素材\last1.wav", NULL, SND_FILENAME | SND_ASYNC);
    347                 SetDlgItemInt(hDlg, IDC_STATIC3, num--, FALSE);
    348             }
    349             else{
    350                 PlaySound(".\素材\ao.wav", NULL, SND_FILENAME | SND_ASYNC);
    351                 hwndStatic3 = GetDlgItem(hDlg, IDC_STATIC3);
    352                 SetWindowText(hwndStatic3, "");
    353                 KillTimer(hDlg, 1);
    354                 MessageBox(hDlg,TEXT("  对不起,您没有时间了!^_^"),TEXT(""),MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    355             }
    356         default:break;
    357     }
    358     return FALSE ;
    359 }
    360 
    361 BOOL CALLBACK AboutProc4(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)    //答案显示窗口
    362 {
    363     HWND        hwndStatic;    //存储静态文本框句柄
    364     char        answer[1000];    //存储答案
    365 
    366     switch (message){
    367         case WM_INITDIALOG :
    368             hwndStatic = GetDlgItem(hDlg,IDC_STATIC2);
    369             //根据题库和题号获取答案
    370             if(getAnswer(TK,QUESTION,answer)){    //获取答案成功,答案放在answer字符数组里(抽题失败的话函数中有警示代码,并返回0,无需理会)
    371                 SetWindowText(hwndStatic, answer);
    372             }
    373             return TRUE ;
    374         case WM_COMMAND :
    375             switch (LOWORD (wParam)){
    376                 case IDOK :    
    377                     break;
    378                 case IDCANCEL :
    379                     EndDialog (hDlg, 0) ;
    380                     return TRUE ;
    381             }
    382             break;
    383         default:break;
    384     }
    385     return FALSE ;
    386 }

     - 主要的header头文件

    myRandom.h

     1 #include <ctime>
     2 #include <stdlib.h>
     3 #include <stdio.h>
     4 #include <string.h>
     5 
     6 //#define QT_1 275    //题库1的大小(题数)
     7 //#define QT_2 170    //题库2的大小
     8 //#define QT_3 190    //题库3的大小
     9 
    10 int random(int qt);  //传入题库号,然后随机输出该题库中的一个题的题号
    11 int print_qt(int qt,int num,char question[]);  //将指定题库(qt)中指定题号(num)的题的内容输出出来。
    12 int getAnswer(int TK,int QUESTION,char answer[1000]);    //在制定答案库中获取答案
    13 int FindQtNum(int qt);    //获得指定题库的题目数目

     varset.h

     1 #ifndef __MYHEADER__
     2 #define __MYHEADER__
     3 
     4 //存储窗口句柄的全局变量
     5 HINSTANCE hInst;
     6 HWND mainhwnd;
     7 //设置父窗口位置和尺寸
     8 int CCW_X;            
     9 int CCW_Y;
    10 int CCW_WIDTH;
    11 int CCW_HEIGHT;
    12 //设置按钮尺寸
    13 int CBN_WIDTH;
    14 int CBN_HEIGHT;
    15 //设置静态文本框尺寸
    16 int CSTA_WIDTH;
    17 int CSTA_HEIGHT;
    18 //题目
    19 int TK;        //题库
    20 int QUESTION;    //随机产生的题号
    21 //倒计时
    22 int num;
    23 
    24 #endif __MYHEADER__

     《随机分队软件》:

     - 主要的cpp文件

    win2.cpp

      1 // win1.cpp : Defines the entry point for the application.
      2 //
      3 #include "stdafx.h"
      4 #include <windows.h>
      5 #include <ctime>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include "resource.h"
      9 
     10 #define SUM 30        //全部的人数    必须 SUM%TEAMNUM==0
     11 #define TEAMNUM 5    //每队人数
     12 #define CCW_X 600
     13 #define CCW_Y 300
     14 #define CCW_WIDTH  260
     15 #define CCW_HEIGHT 120
     16 #define CBN_WIDTH  260
     17 #define CBN_HEIGHT 92
     18 
     19 HINSTANCE    hInst;
     20 bool        allnum[SUM+1] = {0};    //全部编号
     21 char        team[1000];    //抽中的编号
     22 int            teamnum;
     23 
     24 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
     25 BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
     26 
     27 int APIENTRY WinMain(HINSTANCE hInstance,
     28                      HINSTANCE hPrevInstance,
     29                      LPSTR     lpCmdLine,
     30                      int       nCmdShow)
     31 {
     32      // TODO: Place code here.
     33     static TCHAR szmywndName[] = TEXT("Win2");
     34     HWND hwnd;
     35     MSG msg;
     36 
     37     hInst = hInstance;    //全局变量赋值
     38 
     39     WNDCLASS mywnd;
     40     mywnd.cbClsExtra = 0;
     41     mywnd.cbWndExtra = 0;
     42     mywnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    //背景
     43     mywnd.hCursor = LoadCursor(NULL,IDC_ARROW);        //光标
     44     //mywnd.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1));    //图标
     45     mywnd.hIcon = (HICON)LoadImage(NULL,"icon2.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
     46     mywnd.hInstance = hInstance;                    //设置句柄
     47     mywnd.lpfnWndProc = WndProc;                    //设置窗口过程
     48     mywnd.lpszClassName = szmywndName;                //设置窗口类名字
     49     mywnd.lpszMenuName = NULL;                        //设置菜单名字
     50     mywnd.style = CS_HREDRAW | CS_VREDRAW;            //设置窗口风格
     51 
     52     if(!RegisterClass(&mywnd)){        //注册窗口类
     53         MessageBox(NULL,TEXT("This program requires Windows NT!"),szmywndName,MB_ICONERROR);
     54         return 0;
     55     }
     56 
     57     //创建窗口
     58     hwnd = CreateWindow(szmywndName,
     59                         TEXT("自动分队程序"),
     60                         //WS_OVERLAPPEDWINDOW ,
     61                         WS_OVERLAPPEDWINDOW^WS_CAPTION^WS_THICKFRAME^WS_MAXIMIZEBOX,
     62                         CCW_X,
     63                         CCW_Y,
     64                         CCW_WIDTH,
     65                         CCW_HEIGHT,
     66                         NULL,
     67                         NULL,
     68                         hInstance,
     69                         NULL);
     70 
     71     ShowWindow(hwnd,nCmdShow);        //显示窗口
     72     UpdateWindow(hwnd);                //升级窗口
     73 
     74     while(GetMessage(&msg,NULL,0,0)){    //消息循环
     75         TranslateMessage(&msg);
     76         DispatchMessage(&msg);
     77     }
     78 
     79     return msg.wParam;
     80 }
     81 
     82 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
     83 {
     84     HWND        hwndButton1;
     85     HDC            hdc;
     86     PAINTSTRUCT ps;
     87     int            wmId,wmEvent;
     88     int            num;
     89     int            sel[TEAMNUM+1] = {0};
     90     int            i,j,t,p,length;
     91     char*        str;
     92     HFONT        hFont;
     93 
     94     switch(message){
     95     case WM_CREATE:
     96         //创建按钮
     97         teamnum=0;
     98         srand((unsigned)time(0));
     99         hwndButton1   =  CreateWindow(TEXT("button"),
    100                                     TEXT("点我开始分队"),
    101                                     WS_TABSTOP | WS_CHILD | WS_VISIBLE ,
    102                                     //int( (CCW_WIDTH-CBN_WIDTH-10)/2.0 ),int(CCW_HEIGHT / 6.0 *1.5),CBN_WIDTH,CBN_HEIGHT,
    103                                     0,0,CBN_WIDTH,CBN_HEIGHT,
    104                                     hwnd,
    105                                     (HMENU)1,
    106                                     hInst,
    107                                     NULL);
    108         //设置字体
    109         LOGFONT LogFont;
    110         ::memset(&LogFont, 0, sizeof(LOGFONT));
    111         lstrcpy(LogFont.lfFaceName, "隶书");
    112           LogFont.lfWeight = 100;
    113         LogFont.lfHeight = 35;//-44; // 字体大小
    114         LogFont.lfCharSet = 134;
    115         LogFont.lfOutPrecision = 3;
    116         LogFont.lfClipPrecision = 2;
    117         LogFont.lfOrientation = 45;
    118         LogFont.lfQuality = 1;
    119         LogFont.lfPitchAndFamily = 2;
    120 
    121         // 创建字体
    122         hFont = CreateFontIndirect(&LogFont);
    123         // 设置控件字体
    124         SendMessage(hwndButton1, WM_SETFONT, (WPARAM)hFont, 0); 
    125         return 0;
    126     case WM_COMMAND:
    127         wmId    = LOWORD(wParam); 
    128         wmEvent = HIWORD(wParam);
    129         switch(wmId){
    130             case 1:    
    131                 teamnum++;
    132 
    133                 for(i=1;i<=SUM;i++)    //判断有没有编号可以分配了
    134                     if(!allnum[i])
    135                         break;
    136                 if(i>SUM){
    137                     MessageBox(NULL,TEXT("已没有编号可分配,请重新开始"),TEXT("ERROR"),MB_ICONERROR);
    138                     break;
    139                 }
    140 
    141                 for(i=1;i<=TEAMNUM;i++){    //从剩下的编号中随机抽取5个编号
    142                     while(1){
    143                         num = rand()%SUM + 1;    
    144                         if(!allnum[num])    //没被使用
    145                             break;
    146                     }
    147                     allnum[num]=true;
    148                     sel[i]=num;
    149                 }
    150 
    151                 p=0;    //team[]字符数组的伪指针
    152                 //先将确定的是第几队放入数组中
    153                 
    154                 switch(teamnum){
    155                     case 1:
    156                         str = "第一队:";break;
    157                     case 2:
    158                         str = "第二队:";break;
    159                     case 3:
    160                         str = "第三队:";break;
    161                     case 4:
    162                         str = "第四队:";break;
    163                     case 5:
    164                         str = "第五队:";break;
    165                     case 6:
    166                         str = "第六队:";break;
    167                     default:
    168                         MessageBox(NULL,TEXT("对不起,没有那么多队伍分配"),TEXT("ERROR"),MB_ICONERROR);
    169                         break;
    170                 }
    171                 strcpy(team,str);
    172                 p+=7;
    173 
    174                 for(i=1;i<=TEAMNUM;i++){    //将选出来的5个编号放到字符串数组中
    175                     if(sel[i]==0)
    176                         break;
    177                     if(i!=1)    //数字之间放置逗号
    178                         team[p++]=',';
    179                     t = sel[i];
    180                     length=0;
    181                     while(t){
    182                         length++;
    183                         t/=10;
    184                     }
    185                     t = sel[i];
    186                     for(j=p+length-1;j>=p;j--){    //将每一位数字放入
    187                         team[j]=t%10+'0';
    188                         t/=10;
    189                     }
    190                     p+=length;
    191                 }
    192                 team[p]='';
    193                 //MessageBox(NULL,TEXT(team),TEXT("ERROR"),MB_ICONERROR);
    194                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd,AboutProc);
    195                 break;
    196             default:break;
    197         }
    198         return 0;
    199     case WM_PAINT:
    200         hdc = BeginPaint(hwnd,&ps);    //获得句柄
    201         EndPaint(hwnd,&ps);
    202         ReleaseDC(hwnd,hdc);
    203         return 0;
    204     case WM_DESTROY:
    205         PostQuitMessage(0);
    206         return 0;
    207     }
    208     return DefWindowProc(hwnd,message,wParam,lParam);
    209 }
    210 
    211 BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)    //三分题的题目显示窗口
    212 {
    213     HWND hwndStatic;
    214 
    215     switch (message){
    216         case WM_INITDIALOG :
    217             hwndStatic = GetDlgItem(hDlg,IDC_STATIC1);
    218             SetWindowText(hwndStatic, team);
    219             return TRUE ;
    220         case WM_COMMAND :
    221             switch (LOWORD (wParam)){
    222                 case IDOK : 
    223                     break;
    224                 case IDCANCEL :
    225                     EndDialog (hDlg, 0) ;
    226                     return TRUE ;
    227             }
    228             break;
    229         default:break;
    230     }
    231     return FALSE ;
    232 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    如何选择大数据应用程序
    Python字符和字符值(ASCII或Unicode码值)转换方法
    Python字符和字符值(ASCII或Unicode码值)转换方法
    论炒币者的自我修养
    论炒币者的自我修养
    区块链是什么,如何评价区块链
    C#封装C++DLL(特别是char*对应的string)
    C#文件夹和文件操作
    VS工程目标文件名设置
    double最大最小值宏定义
  • 原文地址:https://www.cnblogs.com/yym2013/p/3656490.html
Copyright © 2011-2022 走看看