![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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; |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]() 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 |
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
So you're making a house?
![]()
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|