SDK编写简单的随机数生成器

上几天帮同学搞了一个随机数生成器,首先在网上搜索了一下,发现网上有的做的也比较的简单,但是功能还是比较全的。


而且发现自己学的东西刚好可以写一个随机数生成器。当然还有些预备知识没有学,所以现学现用了。


计时器的有关知识我已经了解了,首先要用到创建字体,然后是创建父窗口控件,然后就是利用RGB的颜色的变化了。


看了几天的书,终于写出来了。下面是代码:


#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define ID_TIMER    1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("Beeper1") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("随机数生成器1.00版本"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
          
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
          
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static BOOL fFlipFlop = FALSE ;
     static DWORD color_green = 255;
     static DWORD color_blue = 255;
     static DWORD color_red = 255;
     LOGFONT lf;
     HFONT   hFont ;
     TEXTMETRIC tm ;
     TCHAR szBuffer [40];
     static int cxChar, cyChar, sum;
     DWORD a, iLength;
     DWORD ownrand[40] = {31,24,17,11,5,22,4,7,1,32,15,39,29,3,27,21,38,25,12,33,9,23,36,2,13,18,30,16,26,34,37,10,8,28,19,6,14,20,35};
     TCHAR str[256], type_height[10], type_width[10];
     HBRUSH      hBrush ;
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rc ;
     HWND hwndButton;
     switch (message)
     {
     case WM_CREATE:
          SetTimer (hwnd, ID_TIMER, 50, NULL) ;
         cxChar = LOWORD (GetDialogBaseUnits ()) ;
         cyChar = HIWORD (GetDialogBaseUnits ()) ;
               hwndButton = CreateWindow ( TEXT("button"),
                                   TEXT("开始"),
                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                   cxChar + 420, cyChar * (1 + 2 * 1)+300,
                                   20 * cxChar, 7 * cyChar / 4,
                                   hwnd, (HMENU) 1,
                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;

               hwndButton = CreateWindow ( TEXT("button"),
                                   TEXT("暂停"),
                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                   cxChar + 420 , cyChar * (1 + 2 * 1)+350,
                                   20 * cxChar, 7 * cyChar / 4,
                                   hwnd, (HMENU) 2,
                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
          return 0 ;

     case WM_TIMER :
          sum++;  
          InvalidateRect (hwnd, NULL, FALSE) ;
          return 0 ;
          
     case WM_PAINT:
        
          hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rc) ;
          hBrush = CreateSolidBrush (RGB(color_red,color_green,color_blue));
            
          if (sum<=39)
                _itow(ownrand[sum],str,10);
          else{
                sum=0;
                _itow(ownrand[sum],str,10);
          }

          color_red -= 10;                              //颜色变化的实现
          color_green -= 5;
          color_blue -= 1;
          sum++;

          FillRect (hdc, &rc, hBrush) ;
          hdc = GetDC(hwnd);
          GetTextMetrics(hdc,&tm);
          cxChar = tm.tmAveCharWidth;
          cyChar = tm.tmHeight + tm.tmExternalLeading;
          iLength = wcslen(str);
          SetTextColor(hdc,RGB(255-color_red,255-color_green,255-color_blue));
          //SetTextColor(hdc,RGB(250,0,0));

           //设置字体
          lf.lfHeight = 200;
          lf.lfWeight = 170;
          lf.lfWeight = 700;
          lf.lfUnderline = 0;
          lf.lfStrikeOut = 0;
          hFont = CreateFontIndirect(&lf);
          SelectObject(hdc, hFont);
          //GetTextMetrics (hdc, &tm);
          //_itow(tm.tmHeight,type_height,10);
          //_itow(tm.tmAscent,type_width,10);
          //TextOut(hdc, 0, 0, type_height, wcslen(type_height));
          //TextOut(hdc, 50, 0, type_width, wcslen(type_width));
          DeleteObject (hFont);
          SetBkColor (hdc, GetSysColor(COLOR_WINDOW));
          TextOut(hdc, cxChar+400, cyChar+100, str, iLength);

        

          ReleaseDC(hwnd, hdc);
          EndPaint (hwnd, &ps) ;
          DeleteObject (hBrush) ;
          return 0 ;
     case WM_COMMAND:
         if (2 == wParam){
             KillTimer (hwnd, ID_TIMER) ;
             //MessageBox(hwnd, TEXT("恭喜您"),TEXT("congratulations"),MB_OK);
         }

         if (1 == wParam){
            SetTimer (hwnd, ID_TIMER, 50, NULL) ;
         }
         break;
     case WM_DESTROY :
          KillTimer (hwnd, ID_TIMER) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

关于随机数产生的问题,本来想用rand(time(0))来实现的,但是通过测试发现精度不够高,当timer调用每100毫秒更新一次的时候就已经显现出伪随机特性了。

本来想自己写一个随机数生成算法的,但是由于滚动的总数量不是很多,所以请教了华哥,自己开了一个数组,在里面随机写了30个数字。


关于颜色的变化的实现,关键代码其实也比较简单的,每次调用计时器的时候改变RGB的值,但是改变的值不一样,这样就加长了循环周期。


整个代码只是基本实现了效果,没有过多的功能,自己如果以后学了·鼠标键盘等一些其他东西的话还能把这些东西也加进去,做一个改进版本。


posted @ 2011-12-31 15:50  magic_box  阅读(477)  评论(0编辑  收藏  举报