Click to See Complete Forum and Search --> : Transparant TextOut in ChildWindow


jo-the-ripper
February 9th, 2007, 03:36 PM
Hi. I have some problems with textout in a progressbar(childwindow). there are some error by the painting. if i deactive the transparant there are no problems. could somebody help me?

#include <Windows.h>
#include <StdIO.h>
#include <commctrl.h>
#pragma comment(lib,"Comctl32.lib")

static PrevWndProcEdit;
int i = 50;
LRESULT CALLBACK EditWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd, hText, hwndEditAlt, g_hText;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
PSTR szCmdLine, int iCmdShow)
{
InitCommonControls();
static TCHAR szAppName[] = TEXT("Progress Bar");
MSG msg;

WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = 0;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;

RegisterClass(&wndclass);

wndclass.lpfnWndProc = EditWndProc;

RegisterClass(&wndclass);

hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,szAppName, TEXT("LeWi-"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
350, 300,
NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.lParam;
}

// Hauptnachrichtenschleife
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//static HWND hwndEditAlt;

switch (message)
{
case WM_CREATE:
// Editfeld erzeugen
hwndEditAlt = CreateWindowEx(0, PROGRESS_CLASS,
"", WS_CHILD | PBS_SMOOTH | WS_VISIBLE,
10, 20,
300, 20,
hwnd, NULL,
((LPCREATESTRUCT) lParam)->hInstance, NULL);
// Den ursprünglichen WND-PROC Pointer sichern und durch den eigenen ersetzen.
PrevWndProcEdit = SetWindowLong (hwndEditAlt, GWL_WNDPROC, (LONG)EditWndProc);

SendMessage(hwndEditAlt, PBM_SETRANGE, 0, MAKELPARAM(0, 1000));
SendMessage(hwndEditAlt, PBM_SETPOS, 0, 0);

return 0;


case WM_DESTROY:
// Den ursprünglichen Pointer wieder einsetzen
SetWindowLong (hwndEditAlt, GWL_WNDPROC, PrevWndProcEdit);
PostQuitMessage (0);
return 0;
}

return DefWindowProc (hwnd, message, wParam, lParam);
}

// Die neue WND-PROC für das Editfeld
LRESULT CALLBACK EditWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
return 0;
}

case WM_LBUTTONDOWN:
case WM_MOUSEMOVE:
{

if (message == WM_MOUSEMOVE && wParam != MK_LBUTTON)
{
break;
}

InvalidateRect(hwnd, NULL, FALSE);
POINT Cursor = { LOWORD(lParam), HIWORD(lParam) };
RECT Size;
GetClientRect(hwnd, &Size);
SendMessage(hwndEditAlt, PBM_SETPOS, (int)(((float)(Cursor.x+1)) / 300.0f * 1000.0f), 0);
//InvalidateRect(hwnd,NULL,TRUE);

char buff[15];
sprintf(buff, "%.0f", ((float)(Cursor.x+1)) / 300.0f * 100.0f);
HDC hdc = GetDC(hwnd);
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc, 100, 0, buff, strlen(buff));
ReleaseDC(hwnd, hdc);

return 0;
}
}

return CallWindowProc ((WNDPROC) PrevWndProcEdit, hwnd, message, wParam, lParam);
}

Notsosuperhero
February 9th, 2007, 10:02 PM
What error(s) are you getting? Compile time or run time.

Your PrevWndProcEdit is missing a type specifier, all you have is static PrevWndProcEdit; Looks like you meant static LONG PrevWndProcEdit;

And also, use the code tags for code [ code]your code here [/ code] without spaces, it keeps the formatting of the text so it is easier to read.

toto1919
February 10th, 2007, 01:47 AM
Hi. I compiled and ran your code.

I changed this and got a good result...



InvalidateRect(hwnd, NULL, TRUE); //TRUE instead of false



Hope it is what you wanted...

/Thomas

jo-the-ripper
February 10th, 2007, 07:15 AM
Hi toto!

thx for your help. your're right that was the problem. it works great but there is still one problem. by the second click to the same point in the progressbar the text disappears. and after changing the formsize it's the same problem.

is there another good idea?

toto1919
February 12th, 2007, 05:08 PM
Maybe add an integer that checks if the Mouse.x value is the same as the last time the mousebutton was clicked (or mouse was dragged). Maybe:



static PrevWndProcEdit;
int i = 50;
int oldMouseX; //new line



... and further down in the WM_LBUTTONDOWN / WM_MOUSEMOVE case...



POINT Cursor = { LOWORD(lParam), HIWORD(lParam) };
if (Cursor.x != oldMouseX) { //new line
InvalidateRect(hwnd, NULL, TRUE); //just moved inside if()
RECT Size;
GetClientRect(hwnd, &Size);
SendMessage(hwndEditAlt, PBM_SETPOS, (int)(((float)(Cursor.x+1)) / 300.0f * 1000.0f), 0);
//InvalidateRect(hwnd,NULL,TRUE);

char buff[15];
sprintf(buff, "%.0f", ((float)(Cursor.x+1)) / 300.0f * 100.0f);
HDC hdc = GetDC(hwnd);
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc, 100, 0, buff, strlen(buff));
ReleaseDC(hwnd, hdc);
oldMouseX = Cursor.x; //new line
} // new line



That fixed it for me. But resizing the window is still an issue. I think (not an expert) that these things (like TextOut and so on) should be handled in a windows WM_PAINT message. Maybe that would solve the problem. Have to change some code though for that to work. But I think it will be necessary if you are doing a serious application

/Thomas