View Full Version: Unit converter

C++ Learning Community > C++ Creations > Unit converter


Title: Unit converter
Description: Little Win32 program


dr voodoo - July 3, 2003 07:19 PM (GMT)
//Length Unit converter
//by Dr.Voodoo

#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";


int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

wincl.hIcon = LoadIcon(hThisInstance, (LPCTSTR)IDI_WINLOGO);
wincl.hIconSm = LoadIcon(hThisInstance, (LPCTSTR)IDI_WINLOGO);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Converter", /* Title Text */
WS_SYSMENU , /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
376, /* The programs width */
68, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
//These definitions (#define...) could be anywhere in the code
//but I put them here because here they are first used
#define Button1 1
#define Static1 2
#define Edit1 3
#define Combo1 4
#define Combo2 5
CreateWindowEx(WS_EX_CLIENTEDGE, "BUTTON", "=",
WS_CHILD | WS_VISIBLE,
160, 10, 50, 24, hwnd, (HMENU)Button1, GetModuleHandle(NULL), NULL);
CreateWindowEx(WS_EX_CLIENTEDGE, "STATIC", "",
WS_CHILD | WS_VISIBLE,
220, 10, 80, 24, hwnd, (HMENU)Static1, GetModuleHandle(NULL), NULL);
CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | ES_NUMBER,
10, 10, 80, 24, hwnd, (HMENU)Edit1, GetModuleHandle(NULL), NULL);
//Note that with COMBOBOXs the height of the window is the height when
//the dropdown box is opened!
HWND hwnd_Combo1=CreateWindowEx(WS_EX_CLIENTEDGE, "COMBOBOX", "",
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
100, 10, 50, 1000, hwnd, (HMENU)Combo1, GetModuleHandle(NULL), NULL);
HWND hwnd_Combo2=CreateWindowEx(WS_EX_CLIENTEDGE, "COMBOBOX", "",
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
310, 10, 50, 1000, hwnd, (HMENU)Combo2, GetModuleHandle(NULL), NULL);
//Fill in
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"km");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"hm");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"dam");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"m");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"dm");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"cm");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"mm");

SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"yard");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"foot");
SendMessage(hwnd_Combo1, CB_ADDSTRING, 0, (LPARAM)"inch");

SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"km");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"hm");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"dam");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"m");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"dm");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"cm");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"mm");

SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"yard");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"foot");
SendMessage(hwnd_Combo2, CB_ADDSTRING, 0, (LPARAM)"inch");

};
break;
case WM_COMMAND:
{
if (LOWORD(wParam)==Button1)
{
//We check if everywhere a Unit is selected
int Combo1_item = SendMessage(GetDlgItem(hwnd, Combo1),CB_GETCURSEL,0,0);
int Combo2_item = SendMessage(GetDlgItem(hwnd, Combo2),CB_GETCURSEL,0,0);
if ((Combo1_item==CB_ERR)||(Combo2_item==CB_ERR))
{
MessageBox(hwnd,"You didn't select a Unit for one (or both) side(s)","", MB_OK|MB_ICONEXCLAMATION);
}
else
{
//We convert to double
double value=(double)GetDlgItemInt(hwnd,Edit1,NULL,false);
switch(Combo1_item)//First we convert everything to mm
{
case 0:value*=1000000;break;//km
case 1:value*=100000;break;//hm
case 2:value*=10000;break;//dam
case 3:value*=1000;break;//m
case 4:value*=100;break;//dm
case 5:value*=10;break;//cm
case 6:value*=1;break;//mm
case 7:value*=914.4;break;//yard
case 8:value*=304.8;break;//foot
case 9:value*=25.3995;break;//inch
};
switch(Combo2_item)//Now we convert to our output unit
{
case 0:value/=1000000;break;//km
case 1:value/=100000;break;//hm
case 2:value/=10000;break;//dam
case 3:value/=1000;break;//m
case 4:value/=100;break;//dm
case 5:value/=10;break;//cm
case 6:value/=1;break;//mm
case 7:value/=914.4;break;//yard
case 8:value/=304.8;break;//foot
case 9:value/=25.3995;break;//inch
};
//Now convert back to int unfortunatly we will
//loose everything behinde the comma :-(
SetDlgItemInt(hwnd,Static1,(int)value,false);
}
}
}
break;
case WM_KEYDOWN:
if (wParam==VK_RETURN)
{
//We simulate a button push
PostMessage(hwnd,WM_COMMAND,Button1,0);
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}

ih8censorship - July 3, 2003 11:31 PM (GMT)
pretty nice! good job!

Dragon - July 4, 2003 12:40 AM (GMT)
Nice job. I noticed an error, though, in converting inches to feet. I typed in 12 inches to be converted to inches, and it gave me 0! Overall, good job! :P

ih8censorship - July 4, 2003 01:42 AM (GMT)

QUOTE
I typed in 12 inches to be converted to inches, and it gave me 0! Overall, good job!
well ya who would ever need to convert inches to inches :P did u make a typo? :lol:

dr voodoo - July 4, 2003 02:44 PM (GMT)
QUOTE
Nice job. I noticed an error, though, in converting inches to feet. I typed in 12 inches to be converted to inches, and it gave me 0! Overall, good job!

Well that's a little problem. The SetDlgItemInt (Dragon it does exist!) takes an integer var as argument. While converting I use double so that I can maintaine the komma/dot but at that point I have to convert the double to an integer so everything behind the komma will be lost. And 12 Inches is less than 1 foot so actually it's correct.

BTW: Does somebody know if there's some function in some lib to convert a double to a char-array and backwards?(like real() and string() in GM) I would know how to write such a function but it's a bit of work so if there's already one...

Dragon - July 4, 2003 07:51 PM (GMT)
QUOTE
The SetDlgItemInt (Dragon it does exist!)


Oh, it does! How wonderfull! ;) hehe




* Hosted for free by InvisionFree