Click to See Complete Forum and Search --> : Win32. Writing on windows desktop


kakalake
December 18th, 2002, 12:22 PM
Hello!
Is it possible to write text with TextOut on the Desktop of my pc. I am using win32 within a win32 console application. Do i have to use a non win32 console application. I have the following steps but it doesn work:

HGDIOBJ oldPen, oldBrush;
RECT mRect;
PAINTSTRUCT ps;

HWND hwnd = GetDesktopWindow();

::GetClientRect( hwnd, &mRect );

BeginPaint( hwnd, &ps );

//set the color
oldPen = SelectObject( ps.hdc, GetStockObject( WHITE_PEN ) );
oldBrush = SelectObject( ps.hdc, GetStockObject( WHITE_BRUSH ) );

//set BKGround-Mode
SetBkMode( ps.hdc, TRANSPARENT );

//Output
if ( !TextOut( ps.hdc, mRect.right/2, mRect.bottom/2, "hallo wie gehts dir denn so", 10 ) )
std::cerr << "Keine Ausgabe möglich!!" << std::endl;


Ellipse( ps.hdc, 100, 100, 400, 400 );

//Reset pens und brushes
SelectObject( ps.hdc, oldPen );
SelectObject( ps.hdc, oldBrush );

EndPaint( hwnd, &ps );


I think everything is all rigth but it doesn´t work....Thanks.

PaulWendt
December 18th, 2002, 01:53 PM
I've had better success getting a DC to the main screen. There
may be better ways, but I'm not a WIN32 expert or anything:

void FlashScreen(LPCTSTR pszOutput,
const COLORREF& crText /* = RGB(255,255,255) */,
const COLORREF& crBackground /* = RGB(0,0,0) */)
{
int cx = ::GetSystemMetrics(SM_CXSCREEN);
int cy = ::GetSystemMetrics(SM_CYSCREEN);

//--------------------------------------
// get DC for the entire screen
//
HDC hdcScreen = ::GetDC(NULL);
HDC hdcMem = ::GetDC(NULL);
CDC dcScreen;
CDC dcMem;
dcScreen.Attach(hdcScreen);
dcMem.Attach(hdcMem);

//--------------------------------------
// do the bmp in memory for the mem dc
//
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dcMem, cx, cy);
dcMem.SelectObject(&bmp);

//--------------------------------------
// create a large font
//
CFont ftText;
ftText.CreateFont(40, 20, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0);
dcMem.SelectObject(&ftText);

//--------------------------------------
// get size information of output string
//
TEXTMETRIC stFontMetrics;
CString strOutput(pszOutput);
dcMem.GetOutputTextMetrics(&stFontMetrics);
CSize cSize = dcMem.GetOutputTextExtent(strOutput);

//--------------------------------------
// set up the color for output
//
dcMem.SetTextColor(crText);
dcMem.SetBkColor(crBackground);
CRect rcBounds(0, 0, ::GetSystemMetrics(SM_CXSCREEN), ::GetSystemMetrics(SM_CYSCREEN));
dcMem.FillSolidRect(0, 0, ::GetSystemMetrics(SM_CXSCREEN), ::GetSystemMetrics(SM_CYSCREEN), 0x00000000);

//--------------------------------------
// draw and bitblt
//
dcMem.TextOut( (cx / 2) - (cSize.cx / 2), (cy / 2) - (cSize.cy / 2), strOutput);
dcScreen.BitBlt(0, 0, cx, cy, &dcMem, 0, 0, SRCCOPY);

//--------------------------------------
// cleanup
//
ftText.DeleteObject();
bmp.DeleteObject();
::ReleaseDC(NULL, hdcMem);
::ReleaseDC(NULL, hdcScreen);
}

Paul McKenzie
December 18th, 2002, 02:12 PM
Console programs can do as much "GUI tricks" as non-console programs, so the problem is not the type of application.

I think the other "Paul" has the correct answer -- create a device context to the screen and write to it.

Regards,

Paul McKenzie

kakalake
December 18th, 2002, 02:30 PM
DEvice content. Do you mean CDC. I am using win32! I am only able to use HDC.

I have solved the problem. The problem was that GetDesktopWindows returns not the handle to the window where i am able to write. I used this instead:
HDC hdc = GetDC( ::FindWindow( NULL, "SysTray" ) );

But when i am drawing or writing something i am writing on the top of the windows but i want only to write on the desktop. I tried to use the string Explorer, Desktop instead of SysTray but both leads to the same result.
The second problem is the following. When i am writing to the screen the text shoul be persistent like the desktop wallpaper.

Thanks in advance

PaulWendt
December 18th, 2002, 02:52 PM
The code snippet I have does use MFC. I think all of the classes
used are just wrappers around the HANDLE version of the API.
Just "unwrap" the MFC code and you'll be fine. For example,
instead of bmp.CreateCompatibleBitmap(...), you'd call
CreateCompatibleBitmap(hBmp, ...).

--Paul

kakalake
December 20th, 2002, 06:50 AM
but thats not i am looking for. You are writing to a BITMAP an then you are showing the bitmap. I want to write on the desktop behind the icons. I want to write on the desktop like this sofware do: http://www.geisswerks.com/drempels/

Thanks!!!