Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 4:47 AM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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;
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 28th, 2006, 6:48 AM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
I think you can use the InvalidateRect() function to do this. Post the whole code and I'll try to experiment with that.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Mar 28th, 2006, 7:06 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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;
grumpy is offline   Reply With Quote
Old Mar 28th, 2006, 7:27 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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 */
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 28th, 2006, 7:40 AM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Mar 28th, 2006, 7:47 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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.

Sorry...just in a silly mood. :p
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 28th, 2006, 7:52 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
So you're making a house?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Mar 28th, 2006, 8:17 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by InfoGeek
So you're making a house?
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.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:02 PM.

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