已知HWND或HDC 重画窗口

本文介绍了一种在Windows环境下利用HDC句柄在指定窗口上绘制位图的方法。通过获取窗口句柄并创建与之兼容的设备上下文,加载位图资源,最终将位图绘制到目标窗口中。

示例代码如下,已知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;
}

 

#include <windows.h> #include <stdlib.h> #include <time.h> #define BUTTON_WIDTH 100 #define BUTTON_HEIGHT 50 #define BUFFER_SIZE 50 // 新增缓冲区大小常量 // 全局变量 int score = 0; RECT g_buttonRect; void DrawButton(HDC hdc, RECT rect); void MoveButton(HWND hWnd); LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // 绘制按钮 DrawButton(hdc, g_buttonRect); // 修复点:安全文本格式化 char scoreText[BUFFER_SIZE]; sprintf_s(scoreText, BUFFER_SIZE, "得分: %d", score); // 关键修复 TextOut(hdc, 10, 10, scoreText, (int)strlen(scoreText)); EndPaint(hWnd, &ps); } break; case WM_LBUTTONDOWN: { int xPos = LOWORD(lParam); int yPos = HIWORD(lParam); if (xPos >= g_buttonRect.left && xPos <= g_buttonRect.right && yPos >= g_buttonRect.top && yPos <= g_buttonRect.bottom) { score++; MoveButton(hWnd); InvalidateRect(hWnd, NULL, TRUE); } } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } void DrawButton(HDC hdc, RECT rect) { HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); FillRect(hdc, &rect, brush); DeleteObject(brush); Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom); } void MoveButton(HWND hWnd) { RECT clientRect; GetClientRect(hWnd, &clientRect); int maxX = clientRect.right - BUTTON_WIDTH; int maxY = clientRect.bottom - BUTTON_HEIGHT; g_buttonRect.left = rand() % maxX; g_buttonRect.top = rand() % maxY; g_buttonRect.right = g_buttonRect.left + BUTTON_WIDTH; g_buttonRect.bottom = g_buttonRect.top + BUTTON_HEIGHT; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { const char CLASS_NAME[] = "ButtonClickGame"; WNDCLASS wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); if (!RegisterClass(&wc)) { MessageBox(NULL, "窗口注册失败!", "错误", MB_ICONEXCLAMATION | MB_OK); return 0; } HWND hWnd = CreateWindowEx( 0, CLASS_NAME, "按钮点击游戏", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL ); if (hWnd == NULL) { MessageBox(NULL, "窗口创建失败!", "错误", MB_ICONEXCLAMATION | MB_OK); return 0; } srand((unsigned int)time(NULL)); MoveButton(hWnd); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } sprintf_s(scoreText, BUFFER_SIZE, "得分: %d", score);还是错的 给我重新全部用dev c++ 5.11版本的代码进行修改
最新发布
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值