Thread: Drawing Text on the Desktop

  1. #1
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135

    Drawing Text on the Desktop

    Just wondering if anyone can suggest the best way to draw text on the desktop that is persistant. I.e. if I draw text on the desktop using TextOut, the text will persist as long as it is not covered up. Of course this is expected, but in a normal app you would just process a WM_PAINT message to redraw the text, but how could one do this if the text is on the desktop? Perhaps using a timer to refresh it periodically?

    Any suggestions welcome.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Perhaps using a timer to refresh it periodically?
    That would work to some extent...

    You could create a transparent window that maintains itself at the bottom of the z-order - there's probably a better programatic solution though.

    However, this type of functionality is typically implemented using HTML and "active desktop".

    gg

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Ah ok - cheers, it's just one of those things I had never thought of until someone asked me to write something like this.

    Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    This should work:
    Code:
    while(true)
    {
      HDC DeskDC;
      RECT rc;
      rc.left = 0;
      rc.top = 0;
      rc.right = GetSystemMetrics(SM_CXSCREEN);
      rc.bottom = GetSystemMetrics(SM_CYSCREEN);
      DeskDC = GetDC(HWND_DESKTOP);
      SetBkMode(DeskDC, TRANSPARENT);
      DrawText(DeskDC, "Your text here", -1, &rc, DT_CENTER | DT_VCENTER | DT_EXPANDTABS);
    }

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well, I thought you could use a transparent window to do this as suggested by Codeplug but there is one small issue.

    If you are going to draw directly on the desktop, I suggest you hook the SysListView desktop child and only paint when it receives a WM_PAINT.

    Anyway, here is the transparent window code:
    Code:
    /* Win 2000/XP+ only */
    #include <windows.h>
    
    #ifndef WS_EX_LAYERED
    #     define WS_EX_LAYERED 0x80000 
    #     define LWA_COLORKEY  1 
    #     define LWA_ALPHA     2 
    #endif
    
    
    void OnPaint(HWND hwnd, HDC hdc)
    {
    	RECT rc;
    
    	GetClientRect(hwnd, &rc);
    	SetTextColor(hdc, RGB(255,0,0));
    	DrawText(hdc, "Your text here", -1, &rc,
    	         DT_CENTER | DT_VCENTER | DT_EXPANDTABS | DT_SINGLELINE);
    }
    
    
    static LRESULT CALLBACK WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    		case WM_CREATE:
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    
    		case WM_PAINT:
    		{
    			PAINTSTRUCT ps;
    			HDC hdc;
    
    			hdc = BeginPaint(hwnd, &ps);
    			OnPaint(hwnd, hdc);
    			EndPaint(hwnd, &ps);
    			return 0;
    		}
    	}
    
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc = { 0 };
    	POINT pt      = { 0 };
    	HWND hwnd;
    	MSG Msg;
    	HRGN hrgn;
    	RECT rcClient, rcWnd, rcRgn;
    	HANDLE hDll;
    
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = NULL;
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = TEXT("TransparentDemo");
    	wc.hIconSm       = NULL;
    
    	if(!RegisterClassEx(&wc)) return 0;
    
    	hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW, TEXT("TransparentDemo"), NULL,
    	                      0, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    	                      NULL, NULL, hInstance, NULL);
    
    	/* Hide title bar */
    	ClientToScreen(hwnd, &pt);
    	GetWindowRect(hwnd, &rcWnd);
    	GetClientRect(hwnd, &rcClient); 
    
    	rcRgn.left   = pt.x - rcWnd.left;
    	rcRgn.top    = pt.y - rcWnd.top;
    	rcRgn.right  = rcRgn.left + rcClient.right;
    	rcRgn.bottom = rcRgn.top  + rcClient.bottom;
    
    	hrgn = CreateRectRgn(rcRgn.left, rcRgn.top, rcRgn.right, rcRgn.bottom); 
    	SetWindowRgn(hwnd, hrgn, FALSE);
    
    	ShowWindow(hwnd, nCmdShow);
    
    	/* Send to bottom */
    	SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    
    	hDll = LoadLibrary(TEXT("user32"));
    
    	if (hDll)
    	{
    		typedef DWORD (WINAPI * PSLWA)(HWND, DWORD, BYTE, DWORD);
    		PSLWA pSetLayeredWindowAttributes;
    
    		pSetLayeredWindowAttributes = 
    	              (PSLWA) GetProcAddress(hDll,"SetLayeredWindowAttributes");
    
    		if (pSetLayeredWindowAttributes != NULL)
    		{
    			pSetLayeredWindowAttributes (hwnd, RGB(255,255,255), 0, LWA_COLORKEY);
    		}
    
    		FreeLibrary(hDll);
    	}
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    }

  6. #6
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Hey thanks - thats a cool way of doing it. I'll have a play with that code

  7. #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Text positioning and Text scrolling
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2004, 12:35 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM