i want to make a bitmap class where i can draw a bitmap on screen
but i also want to be able to undraw the bitmap, so therefore i also want to know whats underneath the bitmap
#pragma once
#include <windows.h>
class bitmap{
public:
bitmap(HWND hwnd,HINSTANCE hinstance, LPCTSTR str):
_hwnd(hwnd),
_hdc(GetDC(_hwnd)),
_hbitmap(LoadBitmap(hinstance,str)),
_hdcmem(CreateCompatibleDC(_hdc)),
_hdcunder(CreateCompatibleDC(_hdc)),
x_pos(0),
y_pos(0){
SelectObject(_hdcmem,_hbitmap);
BITMAP bitmap;
GetObject(_hbitmap,sizeof(BITMAP),&bitmap);
x_size = bitmap.bmWidth;
y_size = bitmap.bmHeight;
}
~bitmap(){
DeleteDC(_hdcmem);
ReleaseDC(_hwnd,_hdc);
}
void draw(int x, int y){
BitBlt(_hdcunder,x_pos=x,y_pos=y,x_size,y_size,_hdc,0,0,SRCCOPY);
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcmem,0,0,SRCCOPY);
}
void undraw(){
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcunder,0,0,SRCCOPY);
}
private:
HWND _hwnd; //windows handle
HDC _hdc; //dc for painting
HBITMAP _hbitmap; //bitmap handle
HDC _hdcmem; //bitmap memory
HDC _hdcunder; //memory fro what was under the bitmap that i drew
int x_size, //metrics of bitmap
y_size;
int x_pos,//position of bitmap for undrawing it
y_pos;
};
/what i'm trying to do is store what is underneath the position where the bitmap will be drawin to be put in _hdcunder, the draw the bitmap, then when undraw() is called it will putback the original imiage, why doesn't this work?
Joeman
August 22nd, 2008, 09:34 AM
I haven't tried to run it, but I do see a few problems
Are you using LoadBitmap to load from a file on the hard drive?
// bitmap_class.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "bitmap_class.h"
#include "bitmap.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are 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;
//
// FUNCTION: InitInstance(HINSTANCE, 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
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 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;
static int x(0),y(0);
HINSTANCE hinstance;
static bitmap* bmp;
static bitmap* backround;
switch (message)
{
case WM_CREATE:
hinstance = ((LPCREATESTRUCT) lParam)->hInstance;;
bmp = new bitmap(hWnd,((LPCREATESTRUCT) lParam)->hInstance,TEXT("bitmap_a"));
backround = new bitmap(hWnd,((LPCREATESTRUCT) lParam)->hInstance,TEXT("bitmap_b"));
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
backround->draw(100,100);
bmp->draw(x,y);
ValidateRect(hWnd,0);
break;
case WM_KEYDOWN:
bmp->undraw();
switch(wParam){
case VK_UP:
--y;
break;
case VK_DOWN:
++y;
break;
case VK_RIGHT:
++x;
break;
case VK_LEFT:
--x;
break;
default:
break;
}
bmp->draw(x,y);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
yep, and i'm pretty shore i don't need to as it should be evaluated to the value of x anyway as it's equivilent to
(x_pos=x,x_pos)
Joeman
August 23rd, 2008, 07:44 AM
oh right. I was thinking of an if statement :( where if( x = x_pos) would equal true. I will look better now :). I will reread the code. Can you upload a working project? It would make it much easier for me to run to debug.
EDIT: Nevermind. I have worked a small project out. I am trying to fix your problem. It seems like _hdcunder doesn't work correctly