- Start Borland C++BuilderX and, on the main menu, click File ->
New...
- In the Object Gallery dialog box, click New GUI Application and
click OK
- In the New GUI Application Project Wizard - Step 1 of 3, in the
Directory edit box of the Project Settings section, type the path you
want. Otherwise, type
C:\Programs\Win32 Programming
- In the Name edit box, type NetPrice
- Click Next
- In the New GUI Application Project Wizard - Step 2 of 3, accept the
defaults and click Next
- In the New GUI Application Project Wizard - Step 3 of 3, click the
check box under Create
- Select Untitled under the Name column header. Type Exercise
to replace the name and press Tab
- Click Finish
- To create a resource header file, on the main menu, click File ->
New File...
- In the Create New File dialog box, in the Name, type resource
- In the Type combo box, select h, and click OK
- In the file, type:
#define IDD_CONTROLSDLG 101
#define IDC_MARKED_PRICE 104
#define IDC_TAX_RATE 106
#define IDC_CALCULATE_BTN 107
#define IDC_TAX_AMOUNT 109
#define IDC_NET_PRICE 110
|
- To create a resource script, on the main menu, click File -> New
File...
- In the Create New File dialog box, in the Name, type NetPrice
- In the Type combo box, select rc, and click OK
- In the file, type:
#include "resource.h"
IDD_CONTROLSDLG DIALOG 260, 200, 188, 100
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Net Price Calculation"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Marked Price:",IDC_STATIC,10,11,45,8
EDITTEXT IDC_MARKED_PRICE,65,9,55,12,ES_RIGHT | ES_AUTOHSCROLL
LTEXT "Tax Rate:",IDC_STATIC,10,32,32,8
EDITTEXT IDC_TAX_RATE,65,30,45,12,ES_RIGHT | ES_AUTOHSCROLL
LTEXT "%",IDC_STATIC,112,32,10,8
PUSHBUTTON "Calculate",IDC_CALCULATE_BTN,130,30,50,14
PUSHBUTTON "Close",IDCANCEL,130,70,50,14
LTEXT "Tax Amount:",IDC_STATIC,10,52,41,8
EDITTEXT IDC_TAX_AMOUNT,65,50,55,12,ES_RIGHT | ES_AUTOHSCROLL
LTEXT "Net Price:",IDC_STATIC,10,73,32,8
EDITTEXT IDC_NET_PRICE,65,71,55,12,ES_RIGHT | ES_AUTOHSCROLL
END
|
- Display the Exercise.cpp file and change it as follows:
#include <windows.h>
#include <cstdio>
using namespace std;
#ifdef __BORLANDC__
#pragma argsused
#endif
#include "resource.h"
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLSDLG),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
hInst = hInstance;
return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
char strMarkedPrice[20], strTaxRate[20], strTaxAmount[20], strNetPrice[20];
HWND hWndMarkedPrice, hWndTaxRate, hWndTaxAmount, hWndNetPrice;
double MarkedPrice, TaxRate, TaxAmount, NetPrice;
hWndMarkedPrice = GetDlgItem(hWndDlg, IDC_MARKED_PRICE);
hWndTaxRate = GetDlgItem(hWndDlg, IDC_TAX_RATE);
hWndTaxAmount = GetDlgItem(hWndDlg, IDC_TAX_AMOUNT);
hWndNetPrice = GetDlgItem(hWndDlg, IDC_NET_PRICE);
switch(Msg)
{
case WM_INITDIALOG:
SetWindowText(hWndMarkedPrice, "0.00");
SetWindowText(hWndTaxRate, "5.75");
SetWindowText(hWndTaxAmount, "0.00");
SetWindowText(hWndNetPrice, "0.00");
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDC_CALCULATE_BTN:
GetWindowText(hWndMarkedPrice, strMarkedPrice, 20);
GetWindowText(hWndTaxRate, strTaxRate, 20);
MarkedPrice = atof(strMarkedPrice);
TaxRate = atof(strTaxRate) / 100;
TaxAmount = MarkedPrice * TaxRate;
NetPrice = MarkedPrice + TaxAmount;
sprintf(strTaxAmount, "%.2f", TaxAmount);
sprintf(strNetPrice, "%.2f", NetPrice);
SetWindowText(hWndTaxAmount, strTaxAmount);
SetWindowText(hWndNetPrice, strNetPrice);
return TRUE;
case IDCANCEL:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
|
- Test the application then close the dialog box
|