Thread: Make My Window Invisible for a Split Second

  1. #1
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341

    Make My Window Invisible for a Split Second

    I've been working on skinning my own window. I'm trying to recreate the Vista transparent background. The only real problem I have is that I don't know how to see what's behind the window. I can get a fast screenshot using DirectX, but it of course includes my window. I don't even know if it's possible to see what's behind my window. I was thinking having all the other windows redraw themselves over my window by InvalidateRect or something. I don't even know if that would work though. I doubt there's a function to get the screen behind my window, but I was hoping someone would know of a function that could hide my window really fast and make it reappear really fast.
    Don't quote me on that... ...seriously

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Do a WM_MOVE offscreen, take the picture and move it back ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'll check the times on that. Would you recommend SetWindowPos? There are several functions that seem to do the same thing: move, resize, and adjust Z order. I would think SetWindowPos would be the best but I don't know.
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    That WM_MOVE sounds like a good bet. Being a nut, I'd probably try ShowWindow() to both hide and make the window reappear, but would that work with a running function?


    .

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I tried ShowWindow. Using SW_HIDE, it hides every part of the window including the part in the taskbar. Then it activates the window behind my window so that the background I capture is different then the background I see when my window is visible. Are you suggesting send a WM_MOVE message or using one of the functions for moving? I've never been good at sending any messages other than WM_CLOSE which is pretty hard to mess up.
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Try what's below;

    Code:
     case WM_RBUTTONDOWN: 
          {
         MoveWindow(hwnd,810,0,240,120,true);
          break;
           }
    *I just use right clicks for testing purposes

    (x,y, width, height of window)

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Not sure if it is what you need, but Windows has supported transparent windows since Win2000. Code and screenshot follows.
    Code:
    #define _WIN32_WINNT (0x500)
    #include <windows.h>
    #include <commctrl.h>
    #include <stdio.h>
    
    #define MAIN_APP_WINDOW_CLASS TEXT("ScratchProgramWindowClass")
    
    
    //+-----------------------------------------------------------------------------
    LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
    	switch(uMsg)
    	{
    		case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    	}
    
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    
    //+-----------------------------------------------------------------------------
    BOOL SetupWindowClass( void )
    {
    	WNDCLASSEX           wc  = { 0 };
    	INITCOMMONCONTROLSEX ice = { 0 };
    
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = GetModuleHandle(NULL);
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hIconSm       = NULL;
    	wc.lpfnWndProc   = WndProc;
    	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = MAIN_APP_WINDOW_CLASS;
    
    	return (BOOL) RegisterClassEx(&wc);
    }
    
    
    //+-----------------------------------------------------------------------------
    BOOL CALLBACK CreateAppWindows( int nCmdShow )
    {
    	DWORD style;
    	HWND  hwnd, hwndChild;
    
    	hwnd = CreateWindowEx(WS_EX_LAYERED, MAIN_APP_WINDOW_CLASS, TEXT("Main Window"), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
    	                      CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
    	                      NULL, NULL, GetModuleHandle(NULL), NULL);
    
    	/* Set a transparency value of 70% */
    	SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA);
    
    	if ( !hwnd )
    		return FALSE;
    
    	style = ES_MULTILINE | ES_WANTRETURN | ES_LEFT | WS_VISIBLE |
    	        WS_CHILD | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL;
    
    	hwndChild = CreateWindowEx(0, TEXT("EDIT"), TEXT("Hello World!"), style,
    	                           0, 0, 400, 400,
    	                           hwnd, (HMENU) 1337, GetModuleHandle(NULL), NULL);
    
    	if ( !hwndChild )
    	{
    		DestroyWindow(hwnd);
    		return FALSE;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	return TRUE;
    }
    
    
    //+-----------------------------------------------------------------------------
    INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE reserved, LPSTR lpCommand, INT nCmdShow )
    {
    	MSG  msg;
    
    	if( !SetupWindowClass() )
    		return -1;
    
    	if ( !CreateAppWindows( nCmdShow ) )
    		return -1;
    
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    I've seen that before, pretty neat. Although I still use WinMe so I couldn't test it out (not supported).

  9. #9
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    That's cool. But I'm doing a custom skin and I want to have some pretty strange transparency settings. Plus I only want the frame to be transparent and my compiler is too old for that method anyway. I'm about to start trying the move thing.

    [edit]I just timed it and it takes less than a millisecond on my computer to move the window off the screen and then back on. Most of the time, you can't even tell that the window was moved.(Although occasionally, I get a flicker)[/edit]
    Last edited by Brad0407; 04-09-2007 at 09:08 PM.
    Don't quote me on that... ...seriously

  10. #10
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I've got a working version up and running. You can't click and drag the window and none of the buttons work, but it updates itself. I went with MoveWindow. I'm pretty pleased with it. I've attached a screenshot and the source code. It runs with DirectX 8.1 so you need that SDK to compile it. Also, if you're looking at my transparency routine, you'll think I'm crazy. How it works is if the red value of the pixel of the skin is less than 100 like say 25 then my program thinks you want the make the background pixel 75% black. If the value is 100 then it doesn't change the background pixel. If it's between 100 and 200 like say 125 then the program thinks you want the pixel 25% white. I still need to speed up the fnGetBackground.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  4. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM
  5. how to make 2 text window
    By bluexrogue in forum C Programming
    Replies: 2
    Last Post: 09-15-2001, 08:51 PM