Hello. I, the C++ noob is back again... This time requiring some help with some visual CPP.
This is the first time im working with this, and its only @ a basic level. Yes, this is somehting im doing at university, but i dont ask for help unless im stuck and have tried my best.
The problem :
Implement a simple program to display a message and which allows the user to:
1 - Exit
2 - Change and select customer colours
3 - Change and select custom fonts

From this. i've completed 1 and 2. I am stuck on selecting fonts. My code isnt compliling and more and i cant understand the error i am getting.!

please can someone help....

// WinApp1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WinApp1.h"
#include "commdlg.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

HFONT g_hfFont;

/* additional global variables
*/
COLORREF rgbText = RGB(0, 0, 0); // the colour to draw the text
COLORREF rgbBackground = RGB(255, 255, 255); // the colour of the text backgrnd
COLORREF rgbCustom[16] = {0}; // an array of 16 'custom' colours


// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WINAPP1, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPP1));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//


ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPP1));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_WINAPP1);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

/* This is the method to display the colour chooser dialog
It should show the current colour and 'remember custom colours
*/
void DoSelectColour(HWND hwnd) {
/* CHOOSECOLOR is a struct that controls the appearance
and behaviour of the ChooseColor dialog
*/
CHOOSECOLOR cc = {sizeof(CHOOSECOLOR)};
cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR;
cc.hwndOwner = hwnd;
cc.rgbResult = rgbText; // this shows the current colour
cc.lpCustColors = rgbCustom; // the array of 'custom' colours
if(ChooseColor(&cc)) // displays the dialog - returns
// true on OK, false on Cancel
{
rgbText = cc.rgbResult;
}
}



void DoSelectFont(HWND hwnd) {
CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
LOGFONT lf;
GetObject(g_hfFont, sizeof(LOGFONT), &lf);
cf.Flags = CF_EFFECTS | CF_INITTOLOGFONTSTRUCT |
CF_SCREENFONTS;
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.rgbColors = rgbText;
if(ChooseFont(&cf)) {
HFONT hf = CreateFontIndirect(&lf);
if(hf) {
g_hfFont = hf;
}
rgbText = cf.rgbColors; }
}

/* This is the method to draw the text on the window */

void DrawMessage(HDC hdc, RECT* prc){
	SetBkColor(hdc, rgbBackground);
SetTextColor(hdc, rgbText);
	char szMsg[] = "R Singh \nStudent ID: 123456";
/* we want to change the 'top' of the rectangle so that it
is half the original height.
*/
prc->top = (prc->bottom - prc->top) /2;
/* DrawText takes a number of parameters:
a handle to the display area to draw on
a pointer to the string to draw
the number of characters to draw (-1 if all)
a RECT struct to format the text into
format control
*/
DrawText(hdc, szMsg, -1, prc, DT_WORDBREAK | DT_CENTER);
/* We could arrange the vertical centering of the text
by using DT_SINGLELINE | DT_CENTER | DT_VCENTER
but this does not allow the word-wrap feature
*/

HFONT hfOld = SelectObject(hdc, g_hfFont);
SelectObject(hdc, hfOld);


}


//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
HFONT hf;
long lfHeight;
hdc = GetDC(NULL);
lfHeight = -MulDiv(12, GetDeviceCaps(hdc,
LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
hf = CreateFont(lfHeight,0,0,0,0,TRUE,0,0,
0,0,0,0,0,"Times New Roman");
if(hf) {
DeleteObject(g_hfFont);
g_hfFont = hf;
}
else
{ MessageBox(hWnd, "Font creation failed!",
"Error", MB_OK | MB_ICONEXCLAMATION);
}
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case ID_COLOUR_RED: // Colour Menu - Red
			rgbText = RGB(255, 0, 0); // set the current colour
			InvalidateRect(hWnd, NULL, TRUE); // force a repaint
			UpdateWindow(hWnd);
			break;
		case ID_COLOUR_GREEN: // Colour Menu - Green
			rgbText = RGB(0, 255, 0);
			InvalidateRect(hWnd, NULL, TRUE);
			UpdateWindow(hWnd);
			break;
		case ID_COLOUR_BLUE: // Colour Menu - Blue
			rgbText = RGB(0, 0, 255);
			InvalidateRect(hWnd, NULL, TRUE);
			UpdateWindow(hWnd);
			break;
		case ID_COLOUR_SELECT: // Colour Menu - Select...
			DoSelectColour(hWnd); // call our method
			InvalidateRect(hWnd, NULL, TRUE); // force a repaint
			UpdateWindow(hWnd);
			break;

		case ID_FONT_SELECT:
			DoSelectFont(hWnd);
			InvalidateRect(hWnd, NULL, TRUE); // force a repaint
			UpdateWindow(hWnd);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);

		// This is our code to call our DrawMessage method
		RECT rcClient; // the DrawText function needs a client rectangle
		GetClientRect(hWnd, &rcClient);
		DrawMessage(hdc, &rcClient);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

if i try to compile i get the error:
\winapp1.cpp(168) : error C2065: 'hf' : undeclared identifier

Any help / tips would be appreciated

You are getting that error because HFONT hf = CreateFontIndirect(&lf); is defined inside of the DoSelectFont function, trying to call it in DrawMessage returns an error because it doesn't exist in that scope.

umm so is it a problem with global definition?..

i've changed the code.... i get a different error now

winapp1.cpp(168) : error C2440: 'initializing' : cannot convert from 'HGDIOBJ' to 'HFONT'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.