示例代码如下,已知HWND或者HDC handler,想要在此窗口上重画或者贴图。
已知HDC必须得到这个窗口句柄然后重新获得一个HDC,因为HDC handler 只能用于创建它的进程.
http://msdn.microsoft.com/en-us/library/ms724291%28v=vs.85%29.aspx
http://stackoverflow.com/questions/2499487/sharing-hdc-between-different-processes
// testbitmap.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <afx.h>
#include <windows.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
//HDC hdc = GetDC(NULL);
//HWND hwnd = WindowFromDC((HDC)0xe6011ed1);
HWND hwnd = (HWND)0x50ba0;
char str[100];
GetWindowText(hwnd,str,sizeof(str));
HDC hdc = GetDC(hwnd);
printf("hwnd: %x, name: %s, hdc: %x /n", hwnd, str, hdc);
HBITMAP bitmap;
bitmap = (HBITMAP)LoadImage(NULL,_T("c://pac.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
HDC hdcsource = CreateCompatibleDC(NULL);
SelectObject(hdcsource,bitmap);
BitBlt(hdc,0,0,300,300,hdcsource,0,0,SRCCOPY);
printf("Hello World!/n");
return 0;
}