Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,967 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,367 people online right now. Registration is fast and FREE... Join Now!




hw : create visual c++ program to edit words

 
Reply to this topicStart new topic

hw : create visual c++ program to edit words, need help changing fonts in program

arfington
5 Dec, 2007 - 12:23 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 4


My Contributions
hi have made my program in visual c++ it has words on a screen designed in resource editor. i cant get it to load the fonts so i added a error message to see if it was seeing my code and it does. now it creates lots of errors saying cant load fonts. the bit of code in question is in the wndproc bit. i dont know if i have to link the fonts to the program and if i do how any help much appreciated

CODE
// workshop 3.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "workshop 3.h"
#include "commdlg.h" // we need this for the Colour chooser dialog box
#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

int lfHeight;
HFONT g_hfFont;
LOGFONT lf;
COLORREF rgbText = RGB(0, 0, 0); // the colour to draw the text
COLORREF rgbBackground = RGB(255, 255, 255); // the colour of the text backgrnd
// Forward declarations of functions included in this code module:
COLORREF rgbCustom[16] = {0}; // an array of 16 'custom' colours
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

/* 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) {            // put where do select colour is

CHOOSEFONT cf = {sizeof(CHOOSEFONT)};

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; }

}

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_WORKSHOP3, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

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

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

    // 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_WORKSHOP3));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_WORKSHOP3);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   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
//
//
void DrawMessage(HDC hdc, RECT* prc)
    {
        SetBkColor(hdc, rgbBackground);
        SetTextColor(hdc, rgbText);
       char szMsg[] = "Hello, my name is steven jukes student number 0522403";
        prc->top = (prc->bottom - prc->top) /2;
        DrawText(hdc, szMsg, -1, prc, DT_WORDBREAK | DT_CENTER);
        SelectObject(hdc, g_hfFont);

    }

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HFONT arf;
    HDC hdc;
    //    lfHeight = -MulDiv(12, GetDeviceCaps(hdc,
//    LOGPIXELSY), 72);
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    arf = CreateFont(lfHeight,0,0,0,0,TRUE,0,0,0,0,0,0,0,"Times New Roman");
    if(g_hfFont) {
    g_hfFont = arf;
    DeleteObject(g_hfFont);
    }
    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_TIMESNEWROMAN:
            InvalidateRect(hWnd, NULL, TRUE);
            UpdateWindow(hWnd);
            break;
        case ID_FONT_SELECT:
            DoSelectFont(hWnd);
            InvalidateRect(hWnd, NULL, TRUE);
            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;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month