Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Win32 painting (http://www.programmingforums.org/showthread.php?t=9081)

nnxion Mar 28th, 2006 5:47 AM

Win32 painting
 
I have the following code, which works fine for printing out my coordinates, the only problem is that if I don't overwrite the old text (say the old text is "200, 200" and the new text is "20, 20" which is 2 characters less) the old text will still be visible (it would still display the "00" from the "200, 200"). How can I fix that? By the way, I already have a WM_PAINT with some other drawing in it.

I thought I could just use InvalidateRect(), but that doesn't work.
I hope I'm making sense.
:

case WM_LBUTTONDOWN:
        hdc = GetDC(hwnd);
        pts = MAKEPOINTS(lParam);
        text << pts.x << ", " << pts.y;
        TextOut(hdc, 180, 360, text.str().c_str(), text.str().length());
        ReleaseDC(hwnd, hdc);
break;


InfoGeek Mar 28th, 2006 7:48 AM

I think you can use the InvalidateRect() function to do this. Post the whole code and I'll try to experiment with that.

grumpy Mar 28th, 2006 8:06 AM

This is not actually a C++ problem. It is a problem with usage of the win32 library.

Try saving the last text that has been output, and output it again with it's colour set to that of the background. Then output your new text.
:

// untested, but illustrates the idea .....

case WM_LBUTTONDOWN:
        hdc = GetDC(hwnd);
        pts = MAKEPOINTS(lParam);
        text << pts.x << ", " << pts.y;
        if (previous_text.str.length() > 0)
        {
            COLORREF text_colour = GetTextColor(hdc);
            SetTextColor(hdc, GetBkColor(hdc));
            TextOut(hdc, 180, 360, previous_text.str().c_str(), previous_text.str().length());
            SetTextColor(hdc, text_color);
        }
        TextOut(hdc, 180, 360, text.str().c_str(), text.str().length());
        previous_text = text;
        ReleaseDC(hwnd, hdc);
break;


nnxion Mar 28th, 2006 8:27 AM

Thank you Robert, that worked, I would have thought there to be a more direct way of doing it. I could of course draw a rectangle with the color of the background, but I like this (your) way better. :)

@InfoGeek: I'll show you what I have anyway. I know, I might have mixed C and C++ here a lot, my framework application was C, but I decided doing it in C++ was nicer.
:

/* Includes */
#include <sstream>
#include <windows.h>

/* Global variables */
const char *clsName = "Elevator";
const char *wndName = "Elevator Threads Application";

/* Prototypes */
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
void DrawRectangle( HDC hdc, int, int, int, int, COLORREF = RGB(255, 255, 255));

/* Starting point */
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        MSG        msg;
        HWND      hwnd;
        WNDCLASSEX wcx;

        /* Create the application window */
        wcx.cbSize        = sizeof(WNDCLASSEX);
        wcx.style        = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc  = WndProcedure;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor      = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = clsName;
        wcx.hInstance    = hInstance;
        wcx.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);

        /* Register the application */
        if(!RegisterClassEx(&wcx))
        {
                MessageBox(NULL, "RegisterClassEx Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
                return 1;
          }

        /* Create the window */
        hwnd = CreateWindow(clsName,
                          wndName,
                          WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          425,
                          425,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

        /* If creating failed */
        if(!hwnd)
        {
              MessageBox(NULL, "CreateWindow Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
              return 1;
          }

        ShowWindow(hwnd, SW_SHOWNORMAL);
        UpdateWindow(hwnd);

        /* Message loop */
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        return (int)msg.wParam;
}

/* Window Procedure */
LRESULT CALLBACK WndProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        int numx = 4, numy = 6, startx = 75, starty = 75, x , y;
        std::stringstream text;
        static std::string previous_text;
        POINTS pts;
        PAINTSTRUCT ps;
        HDC hdc;

    switch(msg)
    {
                case WM_PAINT:
                        hdc = BeginPaint(hwnd, &ps);
                        /* building */
                        DrawRectangle(hdc, 50, 50, 300, 300, RGB(135, 206, 250));

                        /* x windows */
                        for(y = 0; y < numy; y++)
                        {
                                for(x = 0; x < numx; x++)
                                {
                                        DrawRectangle(hdc, startx, starty, 30, 30);
                                        startx += 75;
                                }
                                startx = 75;
                                starty += 40;
                        }
                        EndPaint(hwnd, &ps);
                        break;

                case WM_LBUTTONDOWN:
                        hdc = GetDC(hwnd);
                        pts = MAKEPOINTS(lParam);
                        text << pts.x << ", " << pts.y;

                        if (previous_text.length() > 0)
                        {
                                COLORREF text_color = GetTextColor(hdc);
                                SetTextColor(hdc, GetBkColor(hdc));
                                TextOut(hdc, 180, 360, previous_text.c_str(), previous_text.length());
                                SetTextColor(hdc, text_color);
                        }

                        TextOut(hdc, 180, 360, text.str().c_str(), text.str().length());
                                previous_text = text.str();
                        ReleaseDC(hwnd, hdc);
                        break;

                case WM_SIZE:
                        break;

                case WM_DESTROY:
                        PostQuitMessage(0);
                        break;

                default:
                        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

void DrawRectangle( HDC hdc, int startx, int starty, int x, int y , COLORREF cRef)
{
    HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;

    hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
    hbrush = CreateSolidBrush(cRef);

    hpenOld = (HPEN) SelectObject(hdc, hpen);
    hbrushOld = (HBRUSH) SelectObject(hdc, hbrush);

    Rectangle(hdc, startx, starty, startx + x, starty + y);

    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
}

/* This comment is for stupid program that would otherwise remove code */


InfoGeek Mar 28th, 2006 8:40 AM

I was thinking of calling just InvalidateRect() when you receive WM_LBUTTONDOWN message and outputting the coordinates in WM_PAINT along with the other code. But grumpy's solution is better.

nnxion Mar 28th, 2006 8:47 AM

Quote:

Originally Posted by InfoGeek
I was thinking of calling just InvalidateRect() when you receive WM_LBUTTONDOWN message and outputting the coordinates in WM_PAINT along with the other code. But grumpy's solution is better.

By the way, what do you think about the program? It's beautiful isn't it? Almost a house. :D

Sorry...just in a silly mood. :p

InfoGeek Mar 28th, 2006 8:52 AM

So you're making a house? :D

nnxion Mar 28th, 2006 9:17 AM

Quote:

Originally Posted by InfoGeek
So you're making a house? :D

Well I wanted to get some experience with win32 threads (have done some thread projects with Java in the past), and wanted to visualise them. So I made this application, which as you see is still far from finished.

I wanted to push a floor and make an elevator come, so that it will be act like a real elevator. I always hate waiting so long for elevators, so I thought I might be able to prove that I'm waiting too long by making this application. :p

Oh, I just looked on google and saw a couple of school assignments for the same, but they use pthread (*nix). A working solution.

And well the house is a building I can see accross from where I work (where I am at right now), in reality the building here is yellow, but I like the color blue better. ;)


All times are GMT -5. The time now is 5:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC