Our members are dedicated to PASSION and PURPOSE without drama!

Random double-street/lines generator (1 to 6)

Started by VLS, October 18, 2012, 04:57:03 AM

Previous topic - Next topic

0 Members and 7 Guests are viewing this topic.

VLS

Generate a random line of double-streets with ease:

[attachimg=1]

Download: [attachmini=2]




Program's source code:

#include <windows.h>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iterator>
#include <ctime>


// Resolve namespace
using namespace std;


// Set class name
const char g_szClassName[] = "vWindowCLS";


// Text to print
ostringstream g_oss;


// Unique number struct
struct c_unique {
  int current;
  c_unique() {current=0;}
  int operator()() {return ++current;}
} UniqueNumber;


// Rectangle structure
RECT g_rect;


/*
* Generates a random double-street line,
* prints it to main form and places it on the keyboard
*/
void gen_line(HWND hwnd)
{
   // Create a vector holding numbers 1 to 6
   vector<int> vec (6);
   
   // Fill it with numbers 1 to 6
   generate( vec.begin(), vec.end(), UniqueNumber );   
   
   // Seed rand
   srand(rand() % time(0));
   
   // Shuffle the vector
   random_shuffle( vec.begin(), vec.end() );
   
   // Reset g_oss
   g_oss.str("");
   
   // Convert all but the last element to avoid a trailing ","
   copy(vec.begin(), vec.end()-1, ostream_iterator<int>(g_oss, ","));


   // Now add the last element with no delimiter
   g_oss << vec.back();
   
   /* Set clipboard to generated line */
   if(OpenClipboard(hwnd))
   {
      // Declare memory block
      HGLOBAL glob;
     
      // Allocate memory
      glob = GlobalAlloc(GMEM_FIXED,32);
     
      // Copy text
      memcpy(glob,g_oss.str().c_str(),11);
     
      // Flush clipboard
      EmptyClipboard();
     
      // Set new data
      SetClipboardData(CF_TEXT,glob);
     
      // Close clipboard
      CloseClipboard();
     
      // Unlock memory
      GlobalUnlock(glob);
   }
   
   /* Invalidate rectangle */
   
   // Get rect
   GetClientRect(hwnd, &g_rect);
   
   // Invalidate rectangle
   InvalidateRect(hwnd, &g_rect, true);
}




LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE: // Program load
        case WM_LBUTTONUP: // Left-click
        case WM_RBUTTONUP: // Right-click
       
         // Call gen_line
         gen_line(hwnd);
         
      break;
     
      // Close window
        case WM_CLOSE:
       
            DestroyWindow(hwnd);
           
        break;
       
        case WM_PAINT:
         
         /* Paint the text*/
         HDC hDC;
         PAINTSTRUCT ps;
         hDC = BeginPaint(hwnd, &ps);
         
         GetClientRect(hwnd, &g_rect);   
         
         // Top, regular font
         DrawText(hDC, "Last random line:", -1, &g_rect,
         DT_SINGLELINE | DT_CENTER | DT_TOP);
         
         // Bottom, regular font
         DrawText(hDC, "Victor/VLS @ www.HobbyCode.tk", -1, &g_rect,
         DT_SINGLELINE | DT_CENTER | DT_BOTTOM);
         
         // Center, big font         
         HFONT hf;
         long lfHeight;


         lfHeight = -MulDiv(28, GetDeviceCaps(hDC, LOGPIXELSY), 72);


         hf = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");


         SelectObject(hDC, hf);
   
         DrawText(hDC, g_oss.str().c_str(), -1, &g_rect,
         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
         
         // Finish paint
         EndPaint(hwnd, &ps);
         
      break;
       
        // Destroy window
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
       
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;


    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);


    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Click to generate",
        WS_OVERLAPPEDWINDOW  &~ WS_MAXIMIZEBOX,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);


    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);


    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Email/Paypal: betselectiongmail.com
-- Victor

Face

Hi VLS!
wanted to pack the "RandomLine * (1-6). Zip" file, but you wrote it.

VLS


Email/Paypal: betselectiongmail.com
-- Victor