john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

win kbps calc

/* john pfeiffer, 09nov07   based on earlier win tutorials: basic win with textout and edit box and dialog box from 01jan06 */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#define     IDC_ButtonPushed  40001
#define     IDC_ExitButton    40099  
#define     IDC_InputText     40003

#define     WIN_WIDTH   400
#define     WIN_HEIGHT   400

#define     BUTTON_QUIT_WIDTH   70
#define     BUTTON_QUIT_HEIGHT   38
#define     BUTTON_CALC_WIDTH   100
#define     BUTTON_CALC_HEIGHT   28



    int num;            //counter

    HWND InputText = NULL;
    int keypressedYloc=BUTTON_CALC_HEIGHT;  //yloc of text out (from button pushed)

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    char szTxt[64];       //used for strings    
    int i;

    switch(msg)
    {
        case WM_CREATE:
        break;
        case WM_PAINT:
        break;

        case WM_COMMAND:
            switch(LOWORD(wParam))    //find out what's been clicked
            {
                case IDC_ButtonPushed:
                    hdc = GetDC(hwnd);
                            //handwand handle, char array, # of chars
                    GetWindowText(InputText, szTxt, 64); 
                    TextOut(hdc, 0, keypressedYloc , szTxt,strlen(szTxt));

                    i = atoi(szTxt);
                    wsprintf(szTxt,"= %d KB per sec, %d Mb per minute, %d MB per minute",
                    (i/8), ((i*60)/1000), (((i/8)*60)/1000)  );
                    TextOut(hdc, 30, keypressedYloc , szTxt,strlen(szTxt));
                    keypressedYloc+=15;

                    ReleaseDC(hwnd, hdc);

                break;

                case IDC_ExitButton:
                    PostQuitMessage(0);
                break;

                default:
                break;
            }
        break;
        case WM_LBUTTONDOWN:
            hdc = GetDC(hwnd);
            TextOut( hdc, LOWORD(lParam),HIWORD(lParam), "Hello",5);
            num = num + 1;
            ReleaseDC(hwnd, hdc);
        break;
        case WM_CHAR:               //char input from keyboard
            hdc = GetDC(hwnd);
            SetTextColor(hdc, RGB(255,255,255)); //255 x3 = white, 0x3=black
            SetBkColor(hdc, RGB(190,190,190));
            TextOut(hdc, 100,0, "key pressed",11);
            ReleaseDC(hwnd, hdc);
        break;
        case WM_KEYDOWN:            //key on keyboard is down
            hdc = GetDC(hwnd);
            sprintf(szTxt,"clicked %d times", num);
            TextOut(hdc, 300,300, szTxt, strlen(szTxt));
            ReleaseDC(hwnd, hdc);
        break;


        case WM_DESTROY:
            PostQuitMessage(0);
        break;

           default: return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                                LPSTR lpszArgument, int nCmdLine)
{
    num=0;
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wcx;
    HWND ButtonPushed = NULL;
    HWND ExitButton = NULL;

    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.style = CS_DBLCLKS;
    wcx.lpfnWndProc = WinProc;
    wcx.cbClsExtra = 0;
    wcx.cbWndExtra = 0;
    wcx.hInstance = hThisInst;
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcx.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    wcx.lpszMenuName = NULL;
    wcx.lpszClassName = "ClassName";
    wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wcx)) return 0;

    hwnd = CreateWindowEx( 0, "ClassName", "buttons and editbox",
           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
           WIN_WIDTH, WIN_HEIGHT, HWND_DESKTOP, NULL, hThisInst, NULL);

                               //class, txt on button, -win child button, x,y,width,height
    ButtonPushed = CreateWindow( "BUTTON", "calculate", WS_CHILD | WS_VISIBLE |
        BS_PUSHBUTTON, 0, 0,
        BUTTON_CALC_WIDTH, BUTTON_CALC_HEIGHT,
        hwnd, (HMENU)IDC_ButtonPushed, hThisInst, NULL);

    ExitButton = CreateWindow( "BUTTON", "quit", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        WIN_WIDTH - BUTTON_QUIT_WIDTH, WIN_HEIGHT - (2*BUTTON_QUIT_HEIGHT),
        BUTTON_QUIT_WIDTH, BUTTON_QUIT_HEIGHT,

        hwnd, (HMENU)IDC_ExitButton, hThisInst, NULL);

    InputText = CreateWindow( "EDIT", NULL, WS_CHILD | WS_VISIBLE, 
        BUTTON_CALC_WIDTH + 10, 0,
        BUTTON_CALC_WIDTH, BUTTON_CALC_HEIGHT,
        hwnd, (HMENU)IDC_InputText, hThisInst, NULL);

    SetDlgItemText(hwnd, IDC_InputText, "kbps");

    ShowWindow(hwnd, nCmdLine);
    UpdateWindow(hwnd);

    while(TRUE)
    {
        if(PeekMessage(&msg, NULL, 0,0,PM_NOREMOVE))  //so prog doen't hang
        {
            if( !GetMessage(&msg, NULL, 0, 0) )
                break;
           TranslateMessage(&msg);  //figure out what the user did
           DispatchMessage(&msg);   //react to user input (click/text)
        }
    }
    return msg.wParam;
}

  • « Debian 5 install on windows xp machine dualboot cross browse
  • binary bits table »

Published

Jan 1, 2006

Category

c

~381 words

Tags

  • c 95
  • calc 1
  • kbps 1
  • win 19