CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] [WIN32] - how add an image to main window?

    sometimes i don't understand these API functions
    Code:
    .......................
    HBITMAP g_hbmBall=NULL;
    
        switch (message)                  /* handle the messages */
        {
             case WM_CREATE:
            {
    
                g_hbmBall =(HBITMAP) LoadImage(NULL,"C:\\Chrysanthemum.bmp",IMAGE_BITMAP,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
                if(g_hbmBall == NULL)
                    MessageBox(hwnd, "Could not load the image!", "Error", MB_OK | MB_ICONEXCLAMATION);
            }
            break;
            case WM_PAINT:
            {
                BITMAP bm;
                PAINTSTRUCT ps;
    
                HDC hdc = BeginPaint(hwnd, &ps);
    
                HDC hdcMem = CreateCompatibleDC(hdc);
                HBITMAP hbmOld =(HBITMAP) SelectObject(hdcMem, g_hbmBall);
                GetObject(g_hbmBall, sizeof(bm), &bm);
    
                BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
                SelectObject(hdcMem, hbmOld);
                DeleteDC(hdcMem);
    
                EndPaint(hwnd, &ps);
            }
            break;
    .........................
    but why, with WM_PAINT message, the BitBlt() isn't showed the image?
    the main window is created with WS_EX_CLIENTEDGE and WS_OVERLAPPEDWINDOW styles.
    (i can share the rest of code)

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [WIN32] - how add an image to main window?

    Where is your error checking for the function calls in WM_PAINT? Every function call needs to be checked for errors like you do in the WM_CREATE case.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [WIN32] - how add an image to main window?

    Quote Originally Posted by 2kaud View Post
    Where is your error checking for the function calls in WM_PAINT? Every function call needs to be checked for errors like you do in the WM_CREATE case.
    my error was another one with g_hbmBall: or i must do it static or i just:
    Code:
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HBITMAP g_hbmBall=(HBITMAP) LoadImage(NULL,"C:\\Users\\Joaquim\\Documents\\Visual C 98\\CatchDiamonds\\Debug\\Images\\Chrysanthemum.bmp",IMAGE_BITMAP,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
    i'm getting an error here: the image isn't showed with original size, can you advice me?

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [WIN32] - how add an image to main window?

    Quote Originally Posted by Cambalinho View Post
    my error was another one with g_hbmBall: or i must do it static or i just:
    Code:
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HBITMAP g_hbmBall=(HBITMAP) LoadImage(NULL,"C:\\Users\\Joaquim\\Documents\\Visual C 98\\CatchDiamonds\\Debug\\Images\\Chrysanthemum.bmp",IMAGE_BITMAP,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
    i'm getting an error here: the image isn't showed with original size, can you advice me?
    resolved
    i had 1 class builded by me, that i had forgot:
    Code:
    //Image.h
    struct ImageInfo
    {
    	HBITMAP ImageImage;
    	BITMAP Imagebm;
    	HDC ImagehdcMem;
    	HBITMAP ImageMaskImage;
    	BITMAP ImageMaskbm;
    	HDC ImageMaskhdcMem;
    };
    
    enum DrawWay {Normal, Stretch};
    
    class Image
    {
    	//if MaskColor is -3 then the transparency is ignored;
    	//if MaskColor is -2 then the transparent color is the pixel width, height
    	//if MaskColor is -1 then the transparent color is the pixel 0,0
    	public : void Load(ImageInfo &imagem, char *FileName,COLORREF MaskColor=-3)
    	{
    		int posx;
    		int posy;
    
    		COLORREF b;
    
    
    		//Load the image
    		imagem.ImageImage  = (HBITMAP)LoadImage(0, FileName, IMAGE_BITMAP,  0, 0, LR_LOADFROMFILE);
    		GetObject(imagem.ImageImage , sizeof(BITMAP), &imagem.Imagebm);
    		imagem.ImagehdcMem = CreateCompatibleDC(NULL);
    		SelectObject(imagem.ImagehdcMem, imagem.ImageImage);
    
    		//Create the mask
    		if (MaskColor==-3)
    		{
    			//don't do the mask
    		}
    		else
    		{
    			imagem.ImageMaskImage  = (HBITMAP)LoadImage(0, FileName, IMAGE_BITMAP,  0, 0, LR_LOADFROMFILE);
    			GetObject(imagem.ImageMaskImage , sizeof(BITMAP), &imagem.ImageMaskbm);
    			imagem.ImageMaskhdcMem = CreateCompatibleDC(NULL);
    			SelectObject(imagem.ImageMaskhdcMem, imagem.ImageMaskImage);
    
    			if (MaskColor==-2)
    				MaskColor= GetPixel(imagem.ImageMaskhdcMem,imagem.Imagebm.bmWidth-1,imagem.Imagebm.bmHeight-1);
    			if (MaskColor==-1)
    				MaskColor= GetPixel(imagem.ImageMaskhdcMem,0  ,0);
    
    
    			for (posy=0;posy<imagem.Imagebm.bmHeight;posy++)
    			{
    				for (posx=0;posx<imagem.Imagebm.bmWidth ;posx++)
    				{
    					b=GetPixel(imagem.ImageMaskhdcMem,posx,posy);
    					if (b==MaskColor)
    						SetPixel(imagem.ImageMaskhdcMem,posx,posy,RGB(255,255,255));
    					else
    						SetPixel(imagem.ImageMaskhdcMem,posx,posy,RGB(0,0,0));
    				}
    			}
    		}
    	}
    
    	public: void Draw(HDC &Destination, ImageInfo &Origem, int PosX=0, int PosY=0, int Width=0, int Height=0, BOOL Transparent=false,DrawWay dwDraw=Normal)
    	{
    		if (Transparent==TRUE)
    		{
    			if (dwDraw==Stretch)
    			{
    				StretchBlt (Destination,
    					PosX, PosY,
    					Width, Height,
    					Origem.ImagehdcMem,
    					0, 0,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,SRCINVERT);
    				StretchBlt (Destination,
    					PosX, PosY,
    					Width, Height,
    					Origem.ImageMaskhdcMem,
    					0, 0,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight, SRCAND);
    				StretchBlt (Destination,
    					PosX, PosY,
    					Width, Height,
    					Origem.ImagehdcMem,
    					0, 0,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight, SRCINVERT);
    			}
    			else if (dwDraw==Normal)
    			{
    				BitBlt( Destination,
    					PosX, PosY,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,
    					Origem.ImagehdcMem,
    					0, 0,
    					SRCINVERT);
    
    				BitBlt( Destination,
    					PosX, PosY,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,
    					Origem.ImageMaskhdcMem,
    					0, 0,
    					SRCAND);
    				BitBlt( Destination,
    					PosX, PosY,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,
    					Origem.ImagehdcMem,
    					0, 0,
    					SRCINVERT);
    			}
    		}
    		else
    		{
    			if (dwDraw==Stretch)
    			{
    				StretchBlt (Destination,
    					PosX, PosY,
    					Width, Height,
    					Origem.ImagehdcMem,
    					0, 0,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,SRCCOPY);
    			}
    			else
    			{
    				BitBlt( Destination,
    					PosX, PosY,
    					Origem.Imagebm.bmWidth, Origem.Imagebm.bmHeight,
    					Origem.ImagehdcMem,
    					0, 0,
    					SRCCOPY);
    			}
    		}
    	}
    
    	public: void Destroy(ImageInfo &BitmapImage)
    	{
    		DeleteDC(BitmapImage.ImagehdcMem);
    		DeleteObject((HBITMAP)BitmapImage.ImageImage);
    	}
    };
    
    
    //in window procedure:
    case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                ImageInfo test;
                Image imOperators;
                imOperators.Load(test,"C:\\Users\\Joaquim\\Documents\\Visual C 98\\CatchDiamonds\\Debug\\Images\\Chrysanthemum.bmp");
                imOperators.Draw(hdc,test);
                imOperators.Destroy(test);
                EndPaint(hwnd, &ps);
            }
            break;
    the image is showed with original size
    ok.. i must rebuild these function, but can draw the image
    thanks to all

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