Thread: Whats going on with my code?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Whats going on with my code?

    Hi, i have been continuing to work through this Programming Windows Fifth Edition, and thus far 2 similar programs havent worked for me. They compile but then when i try to run them i get an error from windows. I ran a Debug on one and it said i got a Segmentation Fault, i think. Well here is the latest source code, it took me forever to type and of course when i finally finished and got it compiled it didnt work. I tried both C and C++ but it still wont work. Any help would be appreciated thanks!

    Code:
    /*----------------------------------
        Typer.cpp -- Typing Program
    ----------------------------------*/
    
    #include <windows.h>
    
    #define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT("Typer");
        HWND hwnd;
        MSG msg;
        WNDCLASS wndclass;
        
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc = WndProc;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName;
        
        if(!RegisterClass(&wndclass))
        {
            MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
            return 0;
        }
        
        hwnd = CreateWindow(szAppName, TEXT("Typing Program"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, NULL);
        ShowWindow(hwnd, iCmdShow);
        UpdateWindow(hwnd);
        
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static DWORD dwCharSet = DEFAULT_CHARSET;
        static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret;
        static TCHAR * pBuffer = NULL;
        HDC hdc;
        int x, y, i;
        PAINTSTRUCT ps;
        TEXTMETRIC tm;
        
        switch(message)
        {
            case WM_INPUTLANGCHANGE:
                dwCharSet = wParam;
                
            case WM_CREATE:
                hdc = GetDC(hwnd);
                SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
                             dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                GetTextMetrics(hdc, &tm);
                cyChar = tm.tmAveCharWidth;
                cyChar = tm.tmHeight;
                DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
                ReleaseDC(hwnd, hdc);
                //fall through
            
            case WM_SIZE:
                //obtain window size in pixels
                if(message == WM_SIZE)
                {
                    cxClient = LOWORD(lParam);
                    cyClient = HIWORD(lParam);
                }
                //calculate window size in characters
                cxBuffer = max(1, cxClient / cxChar);
                cyBuffer = max(1, cyClient / cyChar);
                //allocate memory for buffer and clear it
                if(pBuffer != NULL)
                    free(pBuffer);
                
                pBuffer = (TCHAR *) malloc(cxBuffer * cyBuffer * sizeof(TCHAR));
                
                for(y=0; y<cyBuffer; y++)
                {
                    for(x=0; x<cxBuffer; x++)
                    {
                        BUFFER(x,y) = ' ';
                    }
                }
                
                xCaret = 0;
                yCaret = 0;
                
                if(hwnd == GetFocus())
                    SetCaretPos(xCaret * cxChar, yCaret * cyChar);
                
                InvalidateRect(hwnd, NULL, TRUE);
                return 0;
                
            case WM_SETFOCUS:
                //create and show the caret
                CreateCaret(hwnd, NULL, cxChar, cyChar);
                SetCaretPos(xCaret * cxChar, yCaret * cyChar);
                ShowCaret(hwnd);
                return 0;
                
            case WM_KILLFOCUS:
                //hide and destroy the caret
                HideCaret(hwnd);
                DestroyCaret();
                return 0;
                
            case WM_KEYDOWN:
                switch(wParam)
                {
                    case VK_HOME:
                        xCaret = 0;
                        break;
                    case VK_END:
                        xCaret = cxBuffer - 1;
                        break;
                    case VK_PRIOR:
                        yCaret = 0;
                        break;
                    case VK_NEXT:
                        yCaret = cyBuffer - 1;
                        break;
                    case VK_LEFT:
                        xCaret = min(xCaret - 1, 0);
                        break;
                    case VK_RIGHT:
                        xCaret = min(xCaret + 1, cxBuffer - 1);
                        break;
                    case VK_UP:
                        yCaret = max(yCaret - 1, 0);
                        break;
                    case VK_DOWN:
                        yCaret = min(yCaret - 1, cyBuffer - 1);
                        break;
                    case VK_DELETE:
                        for(x = xCaret; x<cxBuffer - 1; x++)
                        {
                            BUFFER(x, yCaret) = BUFFER(x+1, yCaret);
                        }
                        BUFFER(cxBuffer - 1, yCaret) = ' ';
                        
                        HideCaret(hwnd);
                        hdc = GetDC(hwnd);
                        
                        SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
                                     dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                        TextOut(hdc, xCaret * cxChar, yCaret * cyChar,
                                & BUFFER(xCaret, yCaret),
                                cxBuffer - xCaret);
                        DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
                        ReleaseDC(hwnd, hdc);
                        ShowCaret(hwnd);
                        break;
                }
                SetCaretPos(xCaret * cxChar, yCaret * cyChar);
                return 0;
                
            case WM_CHAR:
                for(i=0; i<(int)LOWORD(lParam); i++)
                {
                    switch(wParam)
                    {
                        case '\b':
                            if(xCaret > 0)
                            {
                                xCaret--;
                                SendMessage(hwnd, WM_KEYDOWN, VK_DELETE, 1);
                            }
                            break;
                        case '\t':
                            do
                            {
                                SendMessage(hwnd, WM_CHAR, ' ', 1);
                            }
                            while(xCaret % 8 != 0);
                            break;
                        case '\n':
                            if(++yCaret == cyBuffer)
                            {
                                yCaret = 0;
                            }
                            break;
                        case '\r':
                            xCaret = 0;
                            if(++yCaret == cyBuffer)
                            {
                                yCaret = 0;
                            }
                            break;
                        case '\x1B':
                            for(y=0; y<cyBuffer; y++)
                            {
                                for(x=0; x<cxBuffer; x++)
                                {
                                    BUFFER(x,y) = ' ';
                                }
                            }
                            xCaret = 0;
                            yCaret = 0;
                            InvalidateRect(hwnd, NULL, FALSE);
                            break;
                        default:
                            BUFFER(xCaret, yCaret) = (TCHAR) wParam;
                            
                            HideCaret(hwnd);
                            hdc = GetDC(hwnd);
                            
                            SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
                                         dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                            TextOut(hdc, xCaret * cxChar, yCaret * cyChar,
                                    & BUFFER(xCaret, yCaret), 1);
                            DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
                            ReleaseDC(hwnd, hdc);
                            ShowCaret(hwnd);
                            
                            if(++xCaret == cxBuffer)
                            {
                                xCaret = 0;
                                if(++yCaret == cyBuffer)
                                    yCaret = 0;
                            }
                            break;
                    }
                }
                SetCaretPos(xCaret * cxChar, yCaret * cyChar);
                return 0;
                
            case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);
                
                SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,
                             dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                for(y=0; y<cyBuffer; y++)
                {
                    TextOut(hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer);
                }
                DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
                EndPaint(hwnd, &ps);
                return 0;
                
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Im starting to think getting this book wasnt a good idea cause its outdated (thats what i read), dunno. Thanks for any help!

    PS - What's MFC Windows programming?
    Last edited by Junior89; 01-15-2006 at 02:20 AM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I found no "segmentation fault", but this:

    Code:
                cyChar = tm.tmAveCharWidth;
                cyChar = tm.tmHeight;
    You reassign the y two times

    Code:
                cxChar = tm.tmAveCharWidth;
                cyChar = tm.tmHeight;
    Might work better. No divide by 0. Edit: that book is mighty fine, don't doubt it. Do a google on MFC if you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM