一个简单的hdcMem问题

jronald 2004-01-14 12:31:22
// bitmap.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

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

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BITMAP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BITMAP);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BITMAP);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_BITMAP;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc,hdcMem;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
static INT cxClient,cyClient;

static HBITMAP hBitmap;

static BITMAP bitmap={0,20,5,4,1,1};

static BYTE bits [] = { 0x51, 0x77, 0x10, 0x00,
0x57, 0x77, 0x50, 0x00,
0x13, 0x77, 0x50, 0x00,
0x57, 0x77, 0x50, 0x00,
0x51, 0x11, 0x10, 0x00 } ;

static RECT rect={100,100,200,200};


switch (message)
{
case WM_CREATE:
bitmap.bmBits=(PSTR)bits;

hBitmap = CreateBitmapIndirect(&bitmap);

break;

case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

hdcMem=CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
Rectangle(hdcMem,100,100,200,200);
BitBlt(hdc,0,0, cxClient,cyClient,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);


EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

//rect 不显示,用hdcMem应该行啊,不是说hdc的函数几乎都适用于hdcMem吗?
...全文
68 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2004-01-14
  • 打赏
  • 举报
回复
A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required.

jronald 2004-01-14
  • 打赏
  • 举报
回复
我那个程序
那个矩形画不出来啊
CCsdnCC 2004-01-14
  • 打赏
  • 举报
回复
应该可以了呀,你的程序我都试了一下午了。
jronald 2004-01-14
  • 打赏
  • 举报
回复
to hahu(网痞 -- 勿近): (偶不知道怎么引用)
//hdc的背景是怎么样的hdcMem的背景也是怎么样的
不是呀我的client是白的背景,hdcMem先选一个compatible bitmap再blt到hdc后,背景就是黑的了

要多创建一个hdc呀,好了,结贴
yjh1982 2004-01-14
  • 打赏
  • 举报
回复
一定要建一个HBITMAP并选进memory DC中
hahu 2004-01-14
  • 打赏
  • 举报
回复
我上面有两个内存DC阿
dcMem:用来放小图片
dcMem2:有屏幕那么大
hahu 2004-01-14
  • 打赏
  • 举报
回复
小图片不用CreateCompatibleBitmap
用HBITMAP hbitmap=LoadBitmap(HINSTANCE hinst,LPCTSTR lpBitmapFileName);
jronald 2004-01-14
  • 打赏
  • 举报
回复
多谢指教,但是还有一点不明白:
”然后把你的小Bitmap SRCCOPY到hdcMem指定的矩形,就可以在里面乱画其它的了“

这一步具体怎么实现,BitBlt这类函数要两上hdc 参数的呀,但是我只有一个hdcMem啊,要怎么把一个bitmap贴到hdcMem上?
hahu 2004-01-14
  • 打赏
  • 举报
回复
hdcMem=CreateCompatibleDC(hdc);
hBitmap=CreateCompatibleBitmap(hdc,cx,cy);//cx,cy是小图片的宽和高
SelectObject(hdcMem, hBitmap);
hdcMem2=CreateCompatibleDC(hdc);
hBitmap2=CreateCompatibleBitmap(hdc,cxClient,cyClient);//这个有整个屏幕大
SelectObject(hdcMem2, hBitmap2);

//hdc的背景是怎么样的hdcMem的背景也是怎么样的
PatBlt(hdcMem2,0,0,cxClient,cyClient,WHITENESS);//
BitBlt(hdcMem2,0,0,hdcMem,0,0,cx,cy,SRCCOPY);
lambochan 2004-01-14
  • 打赏
  • 举报
回复
默认是黑色的,可以简单地刷它,成为别的颜色:
FillRect(HDC hDC,CONST RECT *lprc,HBRUSH hbr)
然后把你的小Bitmap SRCCOPY到hdcMem指定的矩形,就可以在里面乱画其它的了
jronald 2004-01-14
  • 打赏
  • 举报
回复
回答了马上给分~
jronald 2004-01-14
  • 打赏
  • 举报
回复
不明白的地方
hdcMem=CreateCompatibleDC(hdc);
hBitmap=CreateCompatibleBitmap(hdc,cxClient,cyClient);
SelectObject(hdcMem, hBitmap);

这样hdcMem的背景是黑的,不知道怎么改变

这么说吧,我想把一个小的bitmap贴到hdcMem,但是hdcMem的大小要和屏幕一样大,然后再在hdcMem上画别的,要怎么做?



lambochan 2004-01-14
  • 打赏
  • 举报
回复
没有Compatible的Bitmap选进内存DC,你的内存DC只有个默认的1x1位图,你在里面能画什么哦..
jronald 2004-01-14
  • 打赏
  • 举报
回复
100分快来拣啊~
jronald 2004-01-14
  • 打赏
  • 举报
回复
hBitmap就不要了
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
hdcMem=CreateCompatibleDC(hdc);
Rectangle(hdcMem,100,100,200,200);
BitBlt(hdc,0,0, cxClient,cyClient,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);
break;

光这样还是画不出矩形,是为什么?谢



DotLSong 2004-01-14
  • 打赏
  • 举报
回复
注意你的 hBitmap ,你可以通过 CreateCompatibleBitmap 来创建他,只有这样,位图才是兼容的,你绘制的东西才是正确的
hahu 2004-01-14
  • 打赏
  • 举报
回复
hdc = BeginPaint(hWnd, &ps);

hdcMem=CreateCompatibleDC(hdc);
hBitmap=CreateCompatibleBitmap(hdc,cxClient,cyClient);
SelectObject(hdcMem, hBitmap);

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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