Solved

GDI Drawing to desktop

Posted on 2006-06-22
4
3,726 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now