Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 1st, 2003, 12:33 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't display a bitmap in a window

Hi all,
I have this piece of code which was butchered from a book. I tried
to make a class that would load a specified bitmap into a window. There are no error messages generated but when its run the window just contains a snap shot of whatever was underneath it (usually .NET). When the window is moved the snapshot moves with it. Needless to say i'm new to windows programming and can't see what the problems are. I expect its something to do with the DC for the bitmap. If anyone could supply some example code I would be very grateful.
Cheers
Drb2k2



int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd = InitWindow(hInstance, nCmdShow);

    while(1)
    {
        MSG msg;
        if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            int result = GetMessage(&msg, NULL, 0, 0);
            if(!result) return(msg.wParam);

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 0;
}

and the class files
#include <windows.h>
#include "BitmapLoad.h"

const char *g_strAppName = "Windows Bitmaps"; // name of application
int g_iScreenWidth = 640; // Screen width in pixels
int g_iScreenHeight = 480; // Screen height in pixels

void PaintWindow(HDC hdc, CBitmapLoad Bitmap1)
{
    // set mapping mode as text
    SetMapMode(hdc, MM_TEXT);

    BitBlt(hdc, 0, 0, g_iScreenWidth, g_iScreenHeight, Bitmap1.m_hdcBitmap, 0, 0, SRCCOPY);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    CBitmapLoad Bitmap1;
    switch(uMsg)
    {
    case WM_CREATE:
        {
        //the window is being created load a bitmap into an off screen DC

        HDC hdcWindow = GetDC(hwnd); // get a DC for this window
        Bitmap1.m_hdcBitmap = CreateCompatibleDC(hdcWindow); // create a compatible in-memory device context
        Bitmap1.LoadBitmap("Trippy.bmp");
        ReleaseDC(hwnd, hdcWindow);
        }
    break;

    case WM_DESTROY:
        {
            if(!Bitmap1.m_hOldBitmap == NULL)
            Bitmap1.Delete();
            //DeleteObject((HGDIOBJ)Bitmap1);
        }
        break;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;

            BeginPaint(hwnd, &ps); // Get the DC we're supposed to use from Windows
            HDC hdc = ps.hdc;

            PaintWindow(hdc, Bitmap1); // Paint the window

            EndPaint(hwnd, &ps);
        }
        return(0);

    case WM_CLOSE:
        {
            PostQuitMessage(0);
            return(0);
        }

    case WM_SIZE:
            g_iScreenWidth = LOWORD(lParam); // new width of client area
            g_iScreenHeight = LOWORD(lParam);
        break;
    }
    return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}

HWND InitWindow(HINSTANCE hinst, int nCmdShow)
{
    HWND hwnd;
    WNDCLASSEX wc;

    // set up and register window class
  memset(&wc, 0, sizeof(wc));
  wc.cbSize = sizeof(wc);
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = WindowProc; // change this to NULL and crash!
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hinst;
  wc.hIcon = NULL;
  wc.hIconSm = NULL;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = NULL; // don't repaint background. this kills flicker.
  wc.lpszMenuName = NULL;
  wc.lpszClassName = "MyCoolWindow";
  RegisterClassEx(&wc);

  // create a window that's 640 pixels wide, 480 tall.
  hwnd = CreateWindowEx(0, "MyCoolWindow", g_strAppName,
        WS_POPUP | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
      10, 10, g_iScreenWidth, g_iScreenHeight, NULL, NULL, hinst, NULL);

  if (!hwnd) {
    ::MessageBox(NULL, "CreateWindow failed!", g_strAppName, MB_ICONSTOP);
    exit(-1);
  }

  ShowWindow(hwnd, nCmdShow);
  return(hwnd);
}

HEADER
#pragma once
#include<windows.h>

class CBitmapLoad
{
public:
    CBitmapLoad(void);
    virtual ~CBitmapLoad(void);
    HDC m_hdcBitmap;
    HBITMAP m_hBitmap;
    HBITMAP m_hOldBitmap;
    int m_iWidth;
    int m_iHeight;
    void LoadBitmap(LPCTSTR lpszFilename);
    void Delete(void);
};

[/code]
and the class file
Code:
#include "bitmapload.h"

CBitmapLoad::CBitmapLoad(void)
: m_iWidth(0)
, m_iHeight(0)
, m_hdcBitmap(NULL)
, m_hBitmap(NULL)
, m_hOldBitmap(NULL)
{
}

CBitmapLoad::~CBitmapLoad(void)
{
}

void CBitmapLoad::LoadBitmap(LPCTSTR lpszFilename)
{
    m_hBitmap = (HBITMAP)LoadImage(NULL, lpszFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    m_hOldBitmap = (HBITMAP)SelectObject(m_hdcBitmap, m_hBitmap);
    BITMAP bmp;
    GetObject(m_hBitmap,sizeof(BITMAP),(LPVOID)&bmp);
    m_iWidth = bmp.bmWidth;
    m_iHeight = bmp.bmHeight;
}

void CBitmapLoad::Delete(void)
{
    m_hBitmap = (HBITMAP)SelectObject(m_hdcBitmap, m_hOldBitmap);
    DeleteDC(m_hdcBitmap);
}
Finally the header for the class
Code:
#pragma once
#include<windows.h>

class CBitmapLoad
{
public:
    CBitmapLoad(void);
    virtual ~CBitmapLoad(void);
    HDC m_hdcBitmap;
    HBITMAP m_hBitmap;
    HBITMAP m_hOldBitmap;
    int m_iWidth;
    int m_iHeight;
    void LoadBitmap(LPCTSTR lpszFilename);
    void Delete(void);
};
Reply With Quote
  #2 (permalink)  
Old September 3rd, 2003, 04:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Studying your code and fixing it seemed
like too much so I'm giving you this piece
of code that you simply graft in the switch-
case sequence of your windproc

case WM_PAINT:
{
    HDC dc = GetDC(hWnd);
    HDC memDC ;
    HBITMAP memBitmap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP_ON E));
    HBITMAP oldBitmap ;

    memDC = CreateCompatibleDC(dc) ;
    oldBitmap = (HBITMAP)SelectObject(memDC,memBitmap) ;

    BITMAP bm;
    GetObject(memBitmap, sizeof(BITMAP), (LPVOID)&bm);
    if (memDC != NULL)
    {
        StretchBlt(dc,0, 0, m_nClientWidth, m_nClientHeight, memDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY) ;
    }
    SelectObject(memDC,oldBitmap) ;
}
break;

you need to provide this code with the
size of the window, put this code
somewhere under before this code after the.

    RECT rect;
    GetClientRect(hWnd,&rect);
    m_nClientWidth = rect.right-rect.left;
    m_nClientHeight = rect.bottom - rect.top;

Also the code assummes that you have a bitmap with name IDB_BITMAP_ONE to be drawn.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display image in a pop-up window nrlahoti ASP.NET 2.0 Professional 1 October 15th, 2007 07:28 AM
Gallery Image display in popup Window caitydev ASP.NET 1.0 and 1.1 Basics 3 April 3rd, 2006 10:23 AM
how to display message to window.status bar tllcll BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 0 November 22nd, 2005 02:04 AM
Display DB Window problem bradles Access 5 March 4th, 2004 06:34 PM
display value from opened window hosefo81 Javascript How-To 4 December 30th, 2003 03:48 PM



All times are GMT -4. The time now is 01:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.