Code:
#include <windows.h>
#define BUTTON 100
#define LABEL1 101
#define LABEL2 102
#define TXTBOX1 501
#define TXTBOX2 502
#define MAXIN 10
#define MAXOUT 25
//FUNCTION PROTOTYPES
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
HWND SetUpWindow(char* cClass, char* cTitle, int nWidth, int nHeight, HINSTANCE hInstance);
//GLOBAL PARAMETERS
HWND hWndMe, hlb1, hlb2, htb1, htb2, hBtn;
char cBuf[MAXOUT]; // char buffer
//SET FONT
inline void SetDefaultFont(int identifier, HWND hwnd)
{
SendDlgItemMessage (hwnd, identifier, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(true, 0));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int iCmdShow)
{
char* cClass = "Convert"; // Name of class
char* cTitle = "Convert"; // Text in window title bar
const int nWidth = 275; // Width of window
const int nHeight = 100; // Height of window
MSG msg;
// CREATE WINDOWS
hWndMe = SetUpWindow(cClass, cTitle, nWidth, nHeight, hInstance);
hlb1 = CreateWindow("STATIC", "Enter temparature in Fahrenheit :", WS_CHILD | WS_VISIBLE | SS_LEFT, 0, 5, 160, 16, hWndMe, (HMENU)LABEL1, hInstance, NULL);
hlb1 = CreateWindow("STATIC", "The temparature in Celcius :", WS_CHILD | WS_VISIBLE | SS_LEFT, 0, 28, 132, 16, hWndMe, (HMENU)LABEL2, hInstance, NULL);
htb1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_RIGHT , 185, 3, 80, 20, hWndMe, (HMENU)TXTBOX1, hInstance, NULL);
htb2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_RIGHT , 132, 25, 133, 20, hWndMe, (HMENU)TXTBOX2, hInstance, NULL);
hBtn = CreateWindow("BUTTON", "Convert", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 185, 50, 50, 20, hWndMe, (HMENU)BUTTON, hInstance, NULL);
ShowWindow(hWndMe,iCmdShow);
UpdateWindow(hWndMe);
cBuf[0] = 0;
SetDefaultFont(LABEL1, hWndMe);
SetDefaultFont(LABEL2, hWndMe);
SetDefaultFont(TXTBOX1, hWndMe);
SetDefaultFont(TXTBOX2, hWndMe);
SetDefaultFont(BUTTON, hWndMe);
SetWindowText(htb1, "0");
//SetWindowText(htb2, "0");
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
break;
}
case WM_COMMAND:
{
if (lParam && LOWORD (wParam) == TXTBOX2)
{
switch (HIWORD (wParam))
{
case EN_SETFOCUS:
{
SetFocus (hWndMe);
break;
}
}
break ;
}
switch(LOWORD(wParam))
{
{
case BUTTON: // BUTTON clicked
{
GetWindowText(htb1, cBuf, MAXOUT);
_gcvt((5*atof(cBuf)-160)/9, MAXOUT, cBuf);
SetWindowText(htb2, cBuf);
cBuf[0] = 0;
}
}
}
}
}
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
HWND SetUpWindow(char* cClass, char* cTitle, int nWidth, int nHeight, HINSTANCE hInstance)
{
WNDCLASS wClass;
HWND hWnd;
wClass.style = CS_HREDRAW | CS_VREDRAW;
wClass.lpfnWndProc = WndProc;
wClass.cbClsExtra = 0;
wClass.cbWndExtra = 0;
wClass.hInstance = hInstance;
wClass.hIcon = LoadIcon(NULL,IDI_ASTERISK);
wClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1) ;
wClass.lpszMenuName = NULL;
wClass.lpszClassName = cClass;
RegisterClass(&wClass);
hWnd = CreateWindow(cClass, cTitle, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, nWidth, nHeight, NULL, NULL, hInstance, NULL);
return hWnd;
}