CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Posts
    13

    Setting a bitmap as form background

    I want to load a bitmap file as background of my form. What i want is that the bitmap will be a external file and by making load call i should be able to load a particular bitmap as background.The number of bitmaps i should load in this manner is so large that i cannot afford the bitmap to be compiled into the program .exe.

    NOTE: I am A beginner in VC++


  2. #2
    Join Date
    Apr 1999
    Posts
    57

    Re: Setting a bitmap as form background

    Use CBitmap:: LoadBitmap function and then Select the return object into the Device context using SelectObject. Then Create a compatible Memory DC and load the Bitmap on to that.
    Finally use BitBlt.

    I present a sample code
    CPaintDC dc(this); // device context for painting
    Note: This dc depends on your program. I have done it in OnPaint( ) function


    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP1);
    CDC dcmem;
    dcmem.CreateCompatibleDC(&dc);
    dcmem.SelectObject(&bmp);
    dc.BitBlt(0,0,640,480,&dcmem,0,0,SRCCOPY);


    With regards
    Anand


    M.Anand
    Development - Lead ,
    Satyam Infoway Limited,
    Chennai - 600034
    India

  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Setting a bitmap as form background

    Unfortunately the other response relies on the bitmap being part of the resource script = part of the EXE, which is exactly what you didn't want.

    The following should help. Note that it only works on Windows '95 (I don't know about '98). Quite why it doesn't work on NT is a mystery, but that's what the help file says (Microsoft: Why?).

    ---
    void CMyWindow::PaintBitmap(CString strFilename)
    {
    BITMAP sBmp ;
    CBitmap bmpPicture, *pBmpOld ;
    CClientDC dc(this);
    CDC dcTemp ;
    HBITMAP hBmp ;
    int iWidth, iHeight ;

    // Create a temporary memory DC.
    dcTemp.CreateCompatibleDC(NULL); // Compatible with the screen.

    // Load the bitmap.
    hBmp = ::LoadImage(NULL,(LPCTSTR)strFilename, IMAGE_BITMAP, 0, 0,
    LR_LOADFROMFILE);
    bmpPicture.Attach(hBMP);

    // Get its dimensions.
    bmpPicture->GetObject(sizeof(BITMAP), &sBmp);
    iWidth = sBmp.bmWidth ;
    iHeight = sBmp.bmHeight ;


    // Select the bitmap into the memory DC ready for blitting.
    pBmpOld = (CBitmap *)dcTemp.SelectObject(&bmpPicture);

    // Blit to the screen DC.
    dc.BitBlt(0, 0, iWidth, iHeight, &dcTemp, 0, 0, SRCCOPY);

    // Select the bitmap out of the memory DC again.
    dcTemp.SelectObject(pBmpOld);

    // Delete the loaded bitmap.
    bmpPicture.DeleteObject();

    // Delete the temporary DC we created.
    dcTemp.DeleteDC();
    }


    ---

    If you need the code to work under NT (or 16-bit Windows?), e-mail me at [email protected] and I will dig out the code you need.

    This code should be called from your form's (= dialogue box's?) OnPaint() procedure.

    Does this help?



    --
    Jason Teagle
    [email protected]

  4. #4
    Join Date
    Apr 1999
    Posts
    7

    Re: Setting a bitmap as form background

    You can also use active-X (Microsoft forms 2.0 OR picture ) control by just pasting the jpg(it will be of lesser size than bmp) over the widow without writing any code.


  5. #5
    Join Date
    Apr 1999
    Posts
    57

    Re: Setting a bitmap as form background

    Hi
    Thanks for your correction. I had VB's LoadPicture in my mind ( which takes the full path of the Bitmap) and didn't concentrate much on that LoadBitmap function

    Anand

    M.Anand
    Development - Lead ,
    Satyam Infoway Limited,
    Chennai - 600034
    India

  6. #6
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Setting a bitmap as form background

    No problem, switching between VB and VC++ can be confusing sometimes, because your mind gets trained to the one language and you temporarily 'forget' the other stuff.

    --
    Jason Teagle
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured