Click to See Complete Forum and Search --> : Refreshing transparent window


Chandra
June 20th, 1999, 11:10 AM
Hi y'all,

(I'm reposting this for I didn't receive any response for my previous posting.)

This is the code to create a transparent window and do some stuff like a
paint program. But I've been having some problems in refreshing the
window. I'll be glad if someone could help me with regards to this.
I'm pretty new to windows programming and so am unable to figure out the
exact reasons. PLEASE HELP....

Thanks a lot in advance

// Here goes the program.....

#include <afxwin.h> // for any MFC routines used
#include <windows.h> // for basic win32 functionality
#include <iostream.h> // for any C++ routines used
#include <windef.h> // for RECT structure definition
#include "resource.h" // for the resources included in the project




long FAR PASCAL WndProc3 (HWND, UINT, WPARAM, LPARAM) ;

HRGN rgnThree;
HWND hWnd3; // the window's "handle"

int FAR PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg; // a message structure
WNDCLASS Wndclass; // window class structure


Wndclass.style = CS_HREDRAW | CS_VREDRAW;
Wndclass.lpfnWndProc = WndProc3;
Wndclass.cbClsExtra = 0;
Wndclass.cbWndExtra = 0;
Wndclass.hInstance = hInstance;
Wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
Wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
Wndclass.hbrBackground = NULL;
Wndclass.lpszMenuName =
MAKEINTRESOURCE(IDR_MENU1);
Wndclass.lpszClassName = "CClass";


// register the window class

if (!RegisterClass (&Wndclass))
return 0;

// create the windows


hWnd3 = CreateWindowEx (WS_EX_TOPMOST,// | WS_EX_TRANSPARENT,
"CClass","Example3 Program",
//WS_BORDER | WS_CAPTION | WS_SYSMENU,
//WS_THICKFRAME
WS_OVERLAPPEDWINDOW,
300, 300, 300, 600, NULL,//(HMENU)IDR_MENU1
NULL, hInstance, NULL);


rgnThree = (HRGN)CreateRectRgn(300,300,600,900);



ShowWindow (hWnd3, SW_SHOW); // display the window
UpdateWindow (hWnd3); // update window's client area


while (GetMessage (&msg, NULL, 0, 0)) // message loop
{
TranslateMessage (&msg); // translate keyboard messages
DispatchMessage (&msg); // send message to WndProc()
}
return (msg.wParam);
}

long FAR PASCAL WndProc3 (HWND hWnd3, UINT wMessage,
WPARAM wParam, LPARAM lParam)
{


HDC hDC; // the device context handle
static int nXpos1, nYpos1;
static int nNewXpos,nNewYpos,nStartXpos,nStartYpos;// mouse position
static int menuSelected,lButtonPressed,nPrevXpos,nPrevYpos;
char cBuf[]=" ";
PAINTSTRUCT ps;
HPEN hPen, hOldPen; // some drawing objects
HWND hWndTemp,hWndTemp1;
RECT rectTemp;
static int nTimer;
static HMENU hPopupMenu;
POINT pFloater;
// HRGN rgnInvalid;
HMENU menuToDestroy;

switch (LOWORD(wMessage)) // process Windows messages

{

case WM_CREATE :

hWndTemp = FindWindow(NULL,NULL);


//hWndTemp = FindWindow(NULL,"Home Page -- OSU OOP Course");
hWndTemp1 = FindWindow(NULL,"Example3 Program");
menuSelected =0;
lButtonPressed = 0;
hPopupMenu = CreatePopupMenu();
AppendMenu(hPopupMenu,MF_STRING,ID_WND_LINE," Draw &a &Line");
AppendMenu(hPopupMenu,MF_STRING,ID_WND_FREEHAND," Freehand &drawing");
AppendMenu(hPopupMenu,MF_STRING,ID_WND_CIRCLE," Draw &a &Circle");
AppendMenu(hPopupMenu,MF_STRING,ID_WND_RECTANGLE," Draw &a &Rectangle");
AppendMenu(hPopupMenu,MF_STRING,ID_WND_CLEAR," Clear &the &Screen");
AppendMenu(hPopupMenu,MF_STRING,ID_WND_QUIT," Quit &the &Application");

nTimer = SetTimer (hWndTemp,1,1000,NULL);
nTimer = SetTimer (hWnd3,1,1000,NULL);
break;

case WM_TIMER :

hDC = GetDC(hWnd3);

// This is to get the handle of the window which would be underneath the
// transparent window created. Refreshing the window beneath too so as to
// get a clear one.

//hWndTemp = FindWindow(NULL,"Home Page -- OSU OOP Course");
hWndTemp = FindWindow(NULL,NULL);
hWndTemp1 = FindWindow(NULL,"Example3 Program");


SetTextColor(hDC,RGB(255,0,0));
SetBkMode(hDC,TRANSPARENT);
// TextOut(hDC,10,10,"Timer",15);
GetWindowRect(hWndTemp,&rectTemp);
UpdateWindow(hWndTemp);
UpdateWindow(hWndTemp1);
UpdateWindow(hWnd3);
//InvalidateRect(hWndTemp,&rectTemp,TRUE);
ReleaseDC (hWnd3, hDC);
break;


case WM_COMMAND :

hDC = GetDC(hWnd3);
switch (wParam)
{

case ID_WND_LINE:
menuSelected = 1;
break;

case ID_WND_FREEHAND:
menuSelected = 2;
break;

case ID_WND_CIRCLE:
menuSelected = 3;
break;

case ID_WND_RECTANGLE:
menuSelected = 4;
break;

case ID_WND_CLEAR: // clear screen by forcing WM_PAINT

//Doesn't help in clearing the menu after selection is being made..
//GetWindowRect(hWnd3,&rectThree);
//InvalidateRect(hWnd3,&rectThree,TRUE);

// The following statements wouldn't clear the screeen...

//UpdateWindow(hWndTemp);
//UpdateWindow(hWnd3);
InvalidateRect (hWnd3, NULL, TRUE);
menuSelected =5;
break;

case ID_WND_QUIT:
DestroyWindow (hWnd3); // destroy window
menuSelected =6;
break; // terminating application
}
ReleaseDC(hWnd3,hDC);
break;

case WM_MOUSEMOVE :

hDC = GetDC(hWnd3);
SetBkMode(hDC,TRANSPARENT);


if(lButtonPressed==1)
{
if(menuSelected==1)
{

MoveToEx(hDC,nStartXpos,nStartYpos,NULL);
nNewXpos = LOWORD (lParam); // in window's client area.
nNewYpos = HIWORD (lParam);

nPrevXpos = nNewXpos;
nPrevYpos = nNewYpos;
}

else if(menuSelected==2)
{

MoveToEx(hDC,nStartXpos,nStartYpos,NULL);
nNewXpos = LOWORD (lParam); // in window's client area.
nNewYpos = HIWORD (lParam);

LineTo(hDC,nNewXpos,nNewYpos);

nStartXpos = nNewXpos;
nStartYpos = nNewYpos;
}


else if(menuSelected==3)
{
MoveToEx(hDC,nStartXpos,nStartYpos,NULL);

nNewXpos = LOWORD (lParam); // in window's client area.
nNewYpos = HIWORD (lParam);



nPrevXpos = nNewXpos;
nPrevYpos = nNewYpos;

}

else if(menuSelected==4)
{
MoveToEx(hDC,nStartXpos,nStartYpos,NULL);

nNewXpos = LOWORD (lParam); // in window's client area.
nNewYpos = HIWORD (lParam);

nPrevXpos = nNewXpos;
nPrevYpos = nNewYpos;

}

}

UpdateWindow(hWndTemp);
UpdateWindow(hWnd3);
ReleaseDC (hWnd3, hDC);
break;


case WM_PAINT :

BeginPaint(hWnd3,&ps);
SetTextColor(hDC,RGB(255,0,0));

// rgnInvalid = (HRGN)CreateRectRgn(nStartXpos,nStartYpos,nNewXpos,nNewYpos);
// InvalidateRgn(NULL,rgnInvalid,TRUE);


// The following two statements do not help in refreshing the window
// properly.

//GetWindowRect(hWnd3,&rectThree);
//InvalidateRect(hWnd3,&rectThree,TRUE);

//ValidateRect(hWnd3,NULL);


// / The following statement, if included, helps in the transparent
// window getting refreshed when moved. But does again has the affect of
// the popup menu not disappearing once the menu item is selected and is
// also resposible for the text not being displayed at all. /


// By including this line, the transparency of the window works perfectly
// but everything gets screwed up. By NOT including this, it's the vice-versa.

InvalidateRgn(hWnd3,NULL,TRUE);


//The following line doesn't suffice the purpose of refreshing
//either /

// InvalidateRgn(hWnd3,rgnThree,TRUE);



EndPaint(hWnd3,&ps);
break;

case WM_LBUTTONDOWN: // left mouse button depressed

lButtonPressed=1;

hDC = GetDC(hWnd3);


nNewXpos = LOWORD (lParam); // in window's client area.
nNewYpos = HIWORD (lParam); // draw an L at spot



ReleaseDC(hWnd3,hDC);

nStartXpos = nNewXpos;
nStartYpos = nNewYpos;

nPrevXpos = nNewXpos;
nPrevYpos = nNewYpos;

break;



case WM_LBUTTONUP:

hDC = GetDC(hWnd3);

SetBkMode(hDC,TRANSPARENT);
SetTextColor(hDC,RGB(255,0,0));

if(menuSelected==1)
{

MoveToEx(hDC,nStartXpos,nStartYpos,NULL);
LineTo(hDC,nNewXpos,nNewYpos);
}

else if(menuSelected==3)
{

MoveToEx(hDC,nStartXpos,nStartYpos,NULL);
hPen = CreatePen (PS_SOLID, 1, RGB (0,0,0));
hOldPen = (HPEN)SelectObject (hDC,hPen); // select into DC
Ellipse (hDC, nStartXpos, nStartYpos, nNewXpos, nNewYpos); // draw circle
SelectObject (hDC, hOldPen); // same for pen
DeleteObject (hPen);

}

else if(menuSelected==4)
{

MoveToEx(hDC,nStartXpos,nStartYpos,NULL);
hPen = CreatePen (PS_SOLID, 1, RGB (0,0,0));
hOldPen = (HPEN)SelectObject (hDC,hPen); // select into DC
Rectangle (hDC, nStartXpos, nStartYpos, nNewXpos, nNewYpos); // draw rectangle
SelectObject (hDC, hOldPen); // same for pen
DeleteObject (hPen);

}

lButtonPressed=0;
menuSelected =0;
UpdateWindow(hWnd3);
ReleaseDC(hWnd3,hDC);

break;

case WM_SIZE :
case WM_MOVE :

// Testing this code


hWndTemp = FindWindow(NULL,NULL);
hWndTemp1 = FindWindow(NULL,"Example3 Program");

GetWindowRect(hWndTemp,&rectTemp);
UpdateWindow(hWndTemp);
UpdateWindow(hWndTemp1);
UpdateWindow(hWnd3);

// Ends here
RECT rectThree;

GetWindowRect(hWnd3,&rectThree);
ClipCursor((LPRECT) &rectThree); //trap mouse in window
// InvalidateRect(hWnd3,NULL,TRUE);
UpdateWindow(hWnd3);
UpdateWindow(hWndTemp);
break;

case WM_RBUTTONDOWN: // right mouse button depressed
// No function to clear the menu from the screen...(It's done automatically after selection)
// UpdateWindow(hWnd3);(doesn't help in clearing the menu)


UpdateWindow(hWndTemp);
UpdateWindow(hWnd3);


menuToDestroy = GetMenu(hWnd3);
DestroyMenu(menuToDestroy);


break;

case WM_RBUTTONUP :


nXpos1 = LOWORD (lParam); // in window's client area.
nYpos1 = HIWORD (lParam); // draw an R at spot

pFloater.x = nXpos1;
pFloater.y = nYpos1;
ClientToScreen(hWnd3,&pFloater);

// This menu disppears automatically once the menu item is selcted.

TrackPopupMenu(hPopupMenu,NULL,pFloater.x,pFloater.y,NULL,hWnd3,NULL);
GetWindowRect(hWnd3,&rectThree);
// InvalidateRect(hWnd3,&rectThree,TRUE);(doesn't help in clearing the menu)
// UpdateWindow(hWnd3);(doesn't help in clearing the menu)
UpdateWindow(hWndTemp);
UpdateWindow(hWnd3);

break;

case WM_CHAR: // keyboard character entered

hDC = GetDC (hWnd3); // display in upper left corner
cBuf[0] = (char) wParam;
SetTextColor(hDC,RGB(255,0,0));
SetBkMode(hDC,TRANSPARENT);
TextOut (hDC, 0, 0, cBuf, 3);
ReleaseDC (hWnd3, hDC);
// UpdateWindow(hWnd3);
UpdateWindow(hWndTemp);
UpdateWindow(hWnd3);
break;

case WM_DESTROY: // stop application

KillTimer(hWndTemp,nTimer);
DestroyMenu(hPopupMenu);
PostQuitMessage (0);
break;

default: // default Windows message processing
return DefWindowProc (hWnd3, wMessage, wParam, lParam);
}

return (0);

}