如何直接在桌面上绘图

classicrock 2005-09-11 04:06:27
我根据GetDesktopWindow函数写了这么一段代码

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

HWND hWnd;

HDC hdc;

TCHAR szBuf[] = "CSDN社区";

while(1)
{
hWnd = GetDesktopWindow();
hdc = GetDC(hWnd);
TextOut( hdc, 0, 0, szBuf, strlen(szBuf) );
}

return 0;
}

经检查hWnd和hdc均不为NULL。但程序什么也不做。

引:MSDN中关于GetDesktopWindow的描述
The GetDesktopWindow function returns a handle to the desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which all icons and other windows are painted.
...全文
945 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
alloutoflove 2005-09-17
  • 打赏
  • 举报
回复
偶在上面说了,查找 Explorer相关的窗口, 比如一般情况下你可以FindWindow("Progman", "Program Manager"); 具体Google会给你好多回答的...
alloutoflove 2005-09-16
  • 打赏
  • 举报
回复
GetDesktopWindow()得到的是整个屏幕的窗口, 在其之上的DC绘制出来的会覆盖一般窗口的,如果想单纯的只在桌面上画的话, 要取Explorer相关的几个子窗口的DC, 而且按了win+D键后情况要特别处理...
寻开心 2005-09-16
  • 打赏
  • 举报
回复
其实就是:
HDC hDC = ::GetWindowDC(::GetDesktopWindow());
::MoveToEx(hDC, 0, 0, NULL);
::LineTo(hDC, 1000, 600);
寻开心 2005-09-16
  • 打赏
  • 举报
回复
http://www.ddvip.net/program/C++/index4/27.htm
younganne 2005-09-16
  • 打赏
  • 举报
回复
CWindowDC dc(GetDesktopWindow());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);

以上m_ptOrigin和point都是Point结构体
XJM36 2005-09-16
  • 打赏
  • 举报
回复
下面是一个可以运行的,自己看效果
#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tchar.h>
#include <assert.h>

void CenterText(HDC hDC, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point)
{
HFONT hFont = CreateFont(- point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH, szFace);

HGDIOBJ hOld = SelectObject(hDC, hFont);

SetTextAlign(hDC, TA_CENTER | TA_BASELINE);

SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0, 0, 0xFF));
TextOut(hDC, x, y, szMessage, _tcslen(szMessage));

SelectObject(hDC, hOld);
DeleteObject(hFont);
}

const TCHAR szMessage[] = _T("Hello, World");
const TCHAR szFace[] = _T("Times New Roman");

#pragma comment(linker, "-merge:.rdata=.text")
#pragma comment(linker, "-align:512")

extern "C" void WinMainCRTStartup()
{
HDC hDC = GetDC(NULL);

CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72);

ReleaseDC(NULL, hDC);
ExitProcess(0);
}
classicrock 2005-09-16
  • 打赏
  • 举报
回复
GetDC( NULL )似乎是在“最上层”的窗口DC上画图
我写了这么一段代码:

while(1)
{
hdc = GetDC( NULL );
TextOut( hdc, 0, 0, szBuf, strlen(szBuf) );
}
结果屏幕左上角始终有szBuf出现
我想要达到的效果是仅在windows桌面上绘图,就向桌面上“我的电脑”的图标,可以被其他窗口覆盖掉。
classicrock 2005-09-16
  • 打赏
  • 举报
回复
谢谢前面各位!
不过我的问题还没有解决。 :(

XJM36(Crazy For Star) 兄给的代码我试了,效果是在“最外层”画图。

而我想要的效果是:简单的说,就像windows的桌面背景一样。效果就和把某张图片设置成了桌面一样。
visio 2005-09-13
  • 打赏
  • 举报
回复
GetDC( NULL );
kenskens 2005-09-11
  • 打赏
  • 举报
回复
用NULL作GetDC()或CreateDC()的参数试试!
meymz1986 2005-09-11
  • 打赏
  • 举报
回复
要用CWindowDC这个类,具体你再查查

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧