Solved

GDI Drawing to desktop

Posted on 2006-06-22
4
3,730 Views
Last Modified: 2013-11-17
Hi,
Im new to GDI and I just want to draw a rectagle on the Windows Desktop to get started.  The code I have so far to do this when the user click a button is:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;

    HDC hdc = GetDesktopWindow();

    hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
    hbrush = CreateSolidBrush(RGB(0, 0, 255));

    // Select the new pen and brush then draw a rectangle.
    hpenOld = SelectObject(hdc, hpen);
    hbrushOld = SelectObject(hdc, hbrush);
    Rectangle(hdc, 0,0, 700,700);

    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
}


This doesnt work, and I assume it's because I'm not getting the windows desktop DC when I call:
HDC hdc = GetDesktopWindow();

can someone tell me how I should be doing this?
thanks!
0
Comment
Question by:blackfrancis75
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 500 total points
ID: 16958903
Hi it is working, but the desktop window keeps repainting itself so your rectangle gets deleted. If you put your code in a timer with a interval of 10 you will see that you are painting on the desktop.

I think you must create a hook to the desktop and modify the WM_PAINT message handler
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16958934
you can also get a handle to the desktop by doing:

hdc = CreateDC(NULL,NULL,NULL,NULL);
GetClientRect (GetDesktopWindow(), &rect) ;
DrawText (hdc, szStartMsg, -1, &rect,DT_SINGLELINE |   DT_CENTER | DT_VCENTER) ;
DeleteDC (hdc);
return 0 ;

you code is fine just remember you must call your function once the desktop repaints itself
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 16960346
thanks! I have a basic understanding of Windows message handling; do you know any good sites that deal with this?
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16961922
the best site to use is the mircosoft msdn
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question