Solved

Displaying Bitmaps In The SDK

Posted on 1998-09-28
8
385 Views
Last Modified: 2013-12-04

I'm trying to learn the SDK and Im experimenting with displaying bitmaps.

I made a skeleton program ( below ) to test out what I understand to be the code necessary to output a bitmap file.

I can't get it to work ( suprise *chuckle* ).  I think my LoadBitmap() function is failing and Im reasonably sure it is the second parameter, but I can't find what I am doing wrong.

Anyway, any tips/advice would be greatly appreciated.  The program is below and it is very small

Thanks
Steve




*
*******************************************
File junk.rc

Junk BITMAP stopsign.bmp
*******************************************
*/

#include <windows.h>


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;


static char szAppName[] = "Junk";

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
     
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateWindow (szAppName,         // window class name
                        "The Hello Program",     // window caption
                    WS_OVERLAPPEDWINDOW,     // window style
                    CW_USEDEFAULT,           // initial x position
                    CW_USEDEFAULT,           // initial y position
                    CW_USEDEFAULT,           // initial x size
                    CW_USEDEFAULT,           // initial y size
                    NULL,                    // parent window handle
                    NULL,                    // window menu handle
                    hInstance,               // program instance handle
                        NULL) ;                         // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     
     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
}
/*
*************************************************************************************
**                         WndProc()
*************************************************************************************
*/

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc, hdcMemory ;
       HBITMAP        hBmpStopSign;

       HINSTANCE hInst;

       BITMAP bm;
     //PAINTSTRUCT ps ;
     RECT        rect ;

     switch (iMsg)
          {
          case WM_CREATE :

            hInst = ((LPCREATESTRUCT) lParam)->hInstance;

            if( hInst == NULL )
                              MessageBox(hwnd, "hInst = Null", "Problem:", MB_OK );


                  
                  hBmpStopSign = LoadBitmap(hInst, "szAppName");
                        if( hBmpStopSign == NULL )
                              MessageBox(hwnd, "LoadBitmap() Failure", "Problem:", MB_OK );

             
               return 0 ;

          case WM_PAINT :

           
            

                  GetObject( hBmpStopSign, sizeof bm, &bm ) ;

                  hdc = GetDC(hwnd) ;

                  hdcMemory = CreateCompatibleDC(hdc);

                  SelectObject( hdcMemory, hBmpStopSign ) ;

                  GetClientRect (hwnd, &rect) ;

                  BitBlt( hdc, rect.left, rect.top, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCCOPY ) ;

   
                  ReleaseDC(hwnd, hdc) ;
                  DeleteDC( hdcMemory ) ;
                  DeleteObject( hBmpStopSign ) ;
                
               return 0 ;

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;
          }

     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
     }
0
Comment
Question by:anatta18
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Accepted Solution

by:
BudVVeezer earned 50 total points
ID: 1414884
I notice that for your wndclass.style does NOT have CS_OWNDC.  You need this in order for you to control your window.  That might be the problem.  Another thing I see is the amount of calculations you are doing during the WM_PAINT message.  This message gets called EVERY time the mouse gets moved.  That is a LOT of calculations to do, and will slow WAY the hell down...if you want some working code posted with comments to go off of, just ask, I'll mail them to ya.

~Aaron
0
 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1414885
ALSO!  Your LoadBitmap is not correct.  It should look like this:
LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
What you do is load the bitmap into your program as a resource before you compile the program.  How you do so depends on the compiler you use.  Some more info might be helpfull.  What does your program do when you run it?  ie, do you get the message box saying that the handle is NULL?
~Aaron
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1414886
COMMENTS ON BUD/AARON's COMMENTS

1.
>> NOT have CS_OWNDC
Not necessary except for to improve performance

2.
>> Another thing I see is the amount of calculations you are doing during the WM_PAINT message.  This message gets called EVERY time the mouse gets moved.  
This not a problem because (a) WM_PAINT only gets called when you repaint the window not when you move the mouse (that's WM_MOUSEMOVE), and (b) updates are quick

3.
>> LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
Aaron, kind of correct on this.
LoadBitmap can be of the form
hBitmap = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) ) ; /* For bitmap resources identified with id numbers, IDB_BITMAP1 will usually be #define'd in resource.h or similar

or
hBitmap = LoadBitmap( hInst, "SomeName" ) ; /* For bitmap with string names */


ACTUAL PROBLEMS

1. hInst variable never initialized
A good way to do this is
(a) remove the declaration from the WndProc:
HINSTANCE hInst;
and move it to a global variable

(b) iN WinMain, add
hInst = hInstance ;

2. hBmpStopSign looses it's value between messages
(a) WM_CREATE you initialize it, then the function exits, so it's value is lost
(b) to fix change
HBITMAP        hBmpStopSign;
to
static HBITMAP        hBmpStopSign;

3. Even with fix #2, you want to keep hBmpStopSign after each paint, move this line
DeleteObject( hBmpStopSign ) ;
from WM_PAINT to WM_DESTROY

4. No call to BeginPaint/EndPaint in WM_PAINT
(a) Change
hdc = GetDC(hwnd) ;
to
hdc = BeginPaint( hwnd, &ps ) ;
(b) Change
ReleaseDC(hwnd, hdc) ;
to
EndPaint( hwnd, &ps ) ;
(c)Change
     //PAINTSTRUCT ps ;
to
     PAINTSTRUCT ps ;

5. Fix the LoadBitmap

e.g.
hBmpStopSign = LoadBitmap(hInst, "szAppName");
becomes something like
hBmpStopSign = LoadBitmap(hInst, szAppName); /* Assuming app name matches the string name of the bitmap */
or
hBmpStopSign = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));




0
 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1414887
That's true. I have had major problems without having CS_OWNDC, but that's just my craptop..  =oP  Answers is right on his comments too, I should have seen that.

~Aaron
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:anatta18
ID: 1414888
He was very helpful.  He offered to send me code to show me how to do it.  However, regarding my own code he never actually mentioned what was preventing the program from working and some of the things he mentioned about how to use the SDK were not entirely correct.

I did learn good stuff from the comments other people made about his answers.

I appreciate him going to the trouble to help me
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1414889
anatta - what did I get wrong or miss ?
0
 

Author Comment

by:anatta18
ID: 1414890
Answer2000  my comments referred Budvezer's original answers to my question.  Your comments about his answer being *slightly* off were correct and useful

Steve
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1414891
OK I would have appreciated some pts, but that's okay

See ya round
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now