Archived

This topic is now archived and is closed to further replies.

EnumWindows problems...

This topic is 5574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I think I found a comprimise to drawing on the desktop...what if I simply draw to the screen ( GetDC(NULL) ) and then EnumWindows to get all the handles to the existing windows. Then determine if the window is visible or not, then determine if it is in the region im drawing on. If it is then I stop drawing on the screen. If not, then I keep drawing. Below is that very code, however I''m having problems with it, like: 1.) It only detects window hide (SW_HIDE) if the mouse is not over the window (I guess it means that the window is not actively painting?) if it is over the window it detects the SW_SHOW. Anyhow here''s the code..please let me know if you see something wrong, or if I could do something better.. Thanks!! #include <windows.h> #include <stdio.h> HWND window; HWND hwnd; HWND desktop_handle; HDC hdc; HRGN clip_region; RECT parameter[100]; HDC HDC_Desktop; int is_visible[100]; int should_draw; WINDOWPLACEMENT window_param[100]; int end_loop = 0; int window_count = 0; BOOL CALLBACK GetAllWindowsProc( HWND hwnd, LPARAM lParam ) { should_draw = 0; GetWindowPlacement(hwnd,&window_param[window_count]); GetWindowRect(hwnd, ¶meter[window_count]); if (end_loop == 1) return true; if ((window_param[window_count].showCmd == SW_SHOW) && (window_param[window_count].rcNormalPosition.left<100)) invisible[window_count] = 1; else if (window_param[window_count].showCmd == SW_HIDE) is_visible[window_count] = 0; if (is_visible[window_count] == 0) { should_draw = 1; end_loop = 0; } else if (is_visible[window_count] == 1) { should_draw = 0; end_loop = 1; } window_count ++; return true; } int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND desktop_handle = GetDesktopWindow(); HDC_Desktop = GetWindowDC(desktop_handle); while (true) { EnumWindows(GetAllWindowsProc, 0); if (should_draw) { HDC_Desktop = GetWindowDC(desktop_handle); char buffer_queue[256]; sprintf(buffer_queue,"parameter.left = (%d) parameter.right = (%d) parameter.top = (%d) parameter.bottom = (%d)" ,window_param[window_count].showCmd, parameter[window_count].right ,parameter[window_count].top, parameter[window_count].bottom); TextOut(HDC_Desktop, 300,300, buffer_queue, strlen(buffer_queue)); ReleaseDC(desktop_handle, HDC_Desktop); } window_count = 0; end_loop = 0; } return (0); } PS: Sorry about the word wrap stuff...

Share this post


Link to post
Share on other sites
It would seem from this thread that he's trying to do some animation right on the desktop.

Boltimus, the truth is that you don't actually want to animate the desktop window; you just want to make it seem like you did. I'd suggest creating a transparent window (no border, no caption, no minimize/maximize/size boxes) on which you'll perform all your drawing, then set it as the lowest window. You can also hide the taskbar icon for it so it can't be selected by a casual user (it would be a good idea to provide a shell icon for options and exit, though) and make sure it ignores alt+tab. Since it can't be selected and doesn't show up for alt-tabbing, it stays at the bottom.

Reference functions (see MSDN)
CreateWindowEx (window styles and extended window styles)
SetWindowPos

[Edit: Formatting.]

[edited by - Oluseyi on September 7, 2002 9:42:59 PM]

Share this post


Link to post
Share on other sites
I thought that might be what he was after, but it wasn''t clear, so rather than guess, I thought I would just ask. Bolitmus, Oluseyi has suggested an excellent approach. By making your window the lowest, you don''t have to fuss with clipping the other windows, Windows will do that for you.

Share this post


Link to post
Share on other sites
Okay, I''ll try that...last time I tried I got the transparent window, but it would keep popping up then going transparent again, plus I could move it..obviously I was doing something wrong. Anyhow, Oluseyi could you elaborate more on the shell icon and what you mean by that (sorry, I''ve never heard of..although I''m sure I probably have seen it )

Thanks for your patience and time!!

~Bolt

Share this post


Link to post
Share on other sites
quote:
Original post by Boltimus
...last time I tried I got the transparent window, but it would keep popping up then going transparent again, plus I could move it..obviously I was doing something wrong.

SetWindowPos allows you to specify that a window cannot be moved. It''s also important to pay attention to extended window styles as they give you more control.

quote:
Original post by Boltimus
...could you elaborate more on the shell icon and what you mean by that (sorry, I''ve never heard of..although I''m sure I probably have seen it )

System tray, I meant to say. You know how some applications put an icon in the system tray, next to the volume control and clock (anti-virus, Task Manager, ATI display driver, some audio drivers, etc)? Do that for your app so it''s easy to gain control of it whenever necessary (see Shell_NotifyIcon).

Share this post


Link to post
Share on other sites
Thanks so much for your patience, Oluseyi! I'm trying to keep the darn window from moving, I've tried actually intercepting the WM_WINDOWPOSCHANGED and WM_WINDOWPOSCHANGING wndproc messages to keep the windo from moving but to no avail. As far as the transparency goes, it's pretty cool, however I did set the z-position to lowest (HWND_BOTTOM) but when the window is transparent and I try to grab one of the desktop Icons the window is in the way. Is this normal? If so then I guess I need to draw the window only around the immediate area I'm gonna draw in.... Anyhow, here's the revised code (the only reason I'm printing the code examples in here is for future reference when others may have questions... ) The thing is, my TextOut won't actually print anything....

  
#include <windows.h>

int window_init = 0;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

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

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindowEx (WS_EX_TRANSPARENT,
szAppName, // window class name

TEXT ("The Hello Program"), // window caption

WS_OVERLAPPEDWINDOW & ~WS_BORDER & ~WS_CAPTION
& ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX & ~WS_SIZEBOX
& ~WS_THICKFRAME & ~WS_CAPTION,
0, // initial x position

0, // initial y position

1024, // initial x size

768, // initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters


window_init = 1;
SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 1024, 768, ~SWP_DRAWFRAME & SWP_NOOWNERZORDER &
SWP_SHOWWINDOW);
window_init = 0;

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

while (true) {

hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint (hwnd, &ps) ;

GetMessage (&msg, NULL, 0, 0);

TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message)
{

case WM_WINDOWPOSCHANGED:

if (window_init == 1)
return DefWindowProc (hwnd, message, wParam, lParam);

else return 0;

case WM_WINDOWPOSCHANGING:

if (window_init == 1)
return DefWindowProc (hwnd, message, wParam, lParam);

else return 0;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

GetClientRect (hwnd, &rect) ;

DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}


[ Edit: Placed code in source box. Excessive scrolling aggravates my RSIs (both wrists). ]

[edited by - Oluseyi on September 7, 2002 12:52:09 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Boltimus
I''m trying to keep the darn window from moving, I''ve tried actually intercepting the WM_WINDOWPOSCHANGED and WM_WINDOWPOSCHANGING wndproc messages to keep the windo from moving but to no avail.

Try WM_MOVE and WM_MOVING.

quote:

...when the window is transparent and I try to grab one of the desktop Icons the window is in the way. Is this normal?

Yes. You can''t reach "through" a window, even if it is transparent (like birds, bugs, butterflies and glass).

quote:
If so then I guess I need to draw the window only around the immediate area I''m gonna draw in....

Pretty much. Otherwise an end user would get very frustrated.

Of course, you could also look into Active Desktop...

Share this post


Link to post
Share on other sites
Without having looked over all of your code - take a look at SystemParametersInfo - in particular with the SPI_GETWORKAREA flag. That will provide you with the coordinates for the entire desktop - minus the task bar - which you'll want to keep if you want to use the systray.

// edit - also consider changing the CreateWindowEx style to just WS_POPUPWINDOW | WS_VISIBLE - well, maybe not the visible part unless you want the window to start off visible.

Here's a snippet from something similar that I did once


  
RECT WorkArea;

// get current screen dimensions

SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &WorkArea, 0);

// create main window

if ( !( hwnd = CreateWindowEx(WS_EX_APPWINDOW
, WINDOW_CLASS_NAME
, WINDOW_TITLE
, WS_POPUPWINDOW | WS_VISIBLE
, 0, 0, WorkArea.right, WorkArea.bottom
, HWND_DESKTOP, NULL, hinstance, NULL) ) )
{
return(0);
}


[edited by - lessbread on September 8, 2002 1:05:48 AM]

Share this post


Link to post
Share on other sites
If you''re aiming for Windows 2000/XP only, you can use WS_EX_LAYERED for better transparancy support. I''ve written a crappy little monitoring app which displays CPU usage, network usage, etc. It''s just sits on the desktop drawing a little graph. You can still access the desktop under the bits which don''t have anything drawn on them, etc.

It''s a bit hard to describe, so here''s a screen shot of what I mean:



As you can see, I can drag boxes around underneath the window on the transparent bits. This was all done using the WS_EX_LAYERED extended style bit that''s available in Windows 2000/XP - have a look in MSDN for more info.

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites