Question

Load PNG File in Picture Control Win32 API

Asked by: jwnrb

I am trying to load a PNG file at runtime into a dialog.  I read that VS 2008 is able to do this, but must be done programmatically.  I have the file in the resource editor, IDB_IMAGE and the picture control IDC_PICT.

But i cant find anything that really would work.  Even if I use a BMP, nothing seems to help.

Any help is greatly appreciated!

This is in pure C, not C++, I can't find anything since ther is so much in MFC and other classes.

LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	static HBRUSH hBrushStatic;
	HBITMAP Png;   // somewhere here perhaps?
	
	switch(message)
	{
	case WM_INITDIALOG:		
		CheckDlgButton(hDlg, IDC_CHECK, FALSE);
		EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
		return TRUE;
                // IDC_PICT is the pixcture control
	        // IDB_IMAGE is the PNG file (IDB_IMAGE2 is the BMP version for testing)
	case WM_CTLCOLORSTATIC:
		if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING))
		{
			SetBkColor((HDC) wParam, (COLORREF) GetSysColor(COLOR_BTNFACE));
			SetTextColor((HDC) wParam, RGB(255, 0, 0));
			SetBkMode((HDC) wParam, TRANSPARENT);
			if(!hBrushStatic)
			{
				hBrushStatic = CreateSolidBrush( (COLORREF) GetSysColor(COLOR_BTNFACE));
			}
			return (UINT) hBrushStatic;
		}
		return FALSE;	
	
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_CHECK:
			if (IsDlgButtonChecked(hDlg, IDC_CHECK))
			{
				EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
				EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);
			}
			else
			{
				EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
				EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE);
			}
			break;
		case IDOK:
			{			
				EndDialog(hDlg, TRUE);
				return FALSE;
			}
		case IDCANCEL:
			{				
				EndDialog(hDlg, FALSE);
				return FALSE;
			}
		
	return TRUE;
		}
	}
	return FALSE;
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-07 at 14:07:30ID24794045
Tags

win32

,

win32 api

,

c

,

png

,

picture control

Topic

C Programming Language

Participating Experts
1
Points
250
Comments
37

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. BMP - PNG; PNG - BMP
    Hi, Does anyone know of a FREE component or code that allows me to import BMP and save them as PNG's and visa-versa. Ulimatley I would like to store these PNG's in a BLOB field in a database. When I run my program I want to get the PNG's and convert then to a Bitmap an...
  2. Conversion of .png to .bmp
    Where can I find the source code for converting png image to a bitmap image in Windows NT?
  3. convert png to bmp (or jpg)
    Hi, Is there a relatively simple way to convert a png file into a bmp or jpg? I'm working with a png (from a third party) and want to pass the image into OleLoadPicture. It supports gif, jpg, and bmp but not png.
  4. How To Convert PNG Image To BMP Format Via PHP?
    php bmp png

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: pgnatyukPosted on 2009-10-07 at 22:58:57ID: 25522585


You posted a diloag procedure. I don't see any code loading the image or creating the picture control you are talking about.

This code will load the image:
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, _T("sunset.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
same about the loading from the resource:

HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szResName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);  
Check LoadImage function in MSND:
http://msdn.microsoft.com/en-us/library/ms648045(VS.85).aspx

And here:
http://www.relisoft.com/Win32/bitmap.html

http://www.winprog.org/tutorial/bitmaps.html

PNG is only a bit a different story.

We are in C zone. Are you sure that the answer on your question will be related to C programming language? Maybe, better to move this question to Microsft Visual C++, Window Programming?

 

by: pgnatyukPosted on 2009-10-08 at 00:04:14ID: 25522806

Function to load the bmp from resources:
HBITMAP LoadPicture(UINT nResID);

Draw in DC:
BOOL DrawPicture(HDC hDC, LPRECT lpRect, HBITMAP hBitmap);

If you will declare a static HBITMAP variable in the file with your window procedure:

HBITMAP s_hBitmap = NULL;

and load the bitmap before you create the dialog:

s_hBitmap = LoadPicture(IDB_BITMAP1);

will declare in the window procedure:
PAINTSTRUCT ps;
HDC hdc;
RECT rect;

and will add WM_PAINT handler:
case WM_PAINT:

   hdc = BeginPaint(hWnd, &ps);
   ::GetClientRect(hWnd, &rect);
   DrawPicture(hdc, &rect, s_hBitmap);
   EndPaint(hWnd, &ps);
   break;

You will see the image as a background of your window.

HBITMAP LoadPicture(UINT nResID)
{
	HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(nResID), 
		IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE); 
	return hBitmap;
}
 
BOOL DrawPicture(HDC hDC, LPRECT lpRect, HBITMAP hBitmap)
{
	if (hBitmap == NULL || hDC == NULL || lpRect == NULL)
		return FALSE;
	BITMAP bitmap = { 0 };
	GetObject(hBitmap, sizeof(BITMAP), &bitmap);
	HDC hMemDC = CreateCompatibleDC(hDC);
	HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
	StretchBlt(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, hMemDC, 0, 0, bitmap.bmWidth, bitmap. bmHeight, SRCCOPY);
	SelectObject(hMemDC, hBitmapOld);
	DeleteDC(hMemDC);
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-08 at 02:08:36ID: 25523319

In this thread: http://www.experts-exchange.com/Microsoft/Development/Microsoft_Programming/Q_24734105.html

you can find a way to load PNG from the resources.

 

by: jwnrbPosted on 2009-10-09 at 06:59:07ID: 25535009

@pgnatyuk:

I deleted the code before hand after it didn't work (meaning I copied and pasted the wrong C file for the forum, oops).

But I don't want the dialog itself to have a png background, i want the picture control to contain a PNG file rather than a BMP file.

 

by: pgnatyukPosted on 2009-10-09 at 07:27:56ID: 25535314

So we need to load a bitmap from the application resources and draw it in a static control (or in a predefined position) in the dialog Win32 application?
Probably the answer is yes, because, seems like i have taken the code I posted and this code draw the image as a window background. So you can use the same DrawPicture function but use another rectangle in the second parameter.
Do you see this code:
  hdc = BeginPaint(hWnd, &ps);
   ::GetClientRect(hWnd, &rect);
   DrawPicture(hdc, &rect, s_hBitmap);
   EndPaint(hWnd, &ps);
   break;

you can change the rectange:
  hdc = BeginPaint(hWnd, &ps);
  // ::GetClientRect(hWnd, &rect);
   rect.left = 50;
   rect.top = 80;
   rect.right = rect.left + 128;
   rect.bottom = rect.top + 64;
   DrawPicture(hdc, &rect, s_hBitmap);
   EndPaint(hWnd, &ps);
   break;

I propose this way because I don't know where is your static control, what is its ID and so on. Actually, you need an image to be drawn in a predefined rectangle. It will be faster to draw it from the WM_PAINT handler then to add a child window.
But how you wish.
If if you have a static on the dialog and you know its Control ID or the window handle, you can take the DC of this controol and the control rectange and call DrawPicture with these parameters. Do you understand? Something like that:
HDC hWindowDC = ::GetWindowDC(hControlWnd);
::GetWindowRect(hControlWnd, &rect);
DrawPicture(hWindowDC, &rect, s_hBitmap);
But anyway you need to do it in the WM_PAINT handler.

 

by: jwnrbPosted on 2009-10-09 at 08:15:27ID: 25535861

Sorry, how do I use the onPain(hwnd) function?

 

by: jwnrbPosted on 2009-10-09 at 08:19:10ID: 25535896

I sorta understand, I know its control, the ID is IDC_PICTURE, which is on the top left hand corner of the dialogue, right on the blue line/margins.

The picture resource is called IDB_BANG

So I put this in the WM_INITDIALOG case?

Would ::GetWindowDC work?  I am doing this in C using only pure Win32 APIs.  Isn't ::GetWindowDC a class thats only useable in C++?  

 

by: pgnatyukPosted on 2009-10-09 at 08:25:04ID: 25535955

>>Sorry, how do I use the onPain(hwnd) function?
I didn't mention any OnPaint. Do you want to add this function to your application?

in WM_INITDIALOG you can load the image.
If you will draw the image in WM_INITDIALOG it will be drawn only once - if you will overide this dialog with another application, you will lose the drawn image. Everything you want to see on your window should be drawn in WM_PAINT.
I posted and example here ID:25522806.

 

by: pgnatyukPosted on 2009-10-09 at 08:32:11ID: 25536020

Oh! You have a dialog. sorry, you are right. let's use the static. Exactly as you said - for the beginning.

 

by: jwnrbPosted on 2009-10-09 at 09:14:50ID: 25536376

Sorry for being so dumb, this project is my firs titme using Win32, if you can please give me more direction in what i need to do here.

 

by: pgnatyukPosted on 2009-10-09 at 09:22:07ID: 25536429

I don't know what exactly you want to do.
If you want you can ask in Windows Programming and C/C++ zones the question about the Win32 application - how to create.
Here we can say that LoadImage loads the bitmaps from the project resources and we know how to draw it - comment ID:25522806 is the answer.

 

by: jwnrbPosted on 2009-10-09 at 11:08:00ID: 25537333

I mean, where in my code to I put this block?

 

by: pgnatyukPosted on 2009-10-09 at 11:19:48ID: 25537435

How your code looks now?
I've told, you can load the image in WM_INITDIALOG. Do you have it already?
Then you need to draw the image. You said, you have a static on the dialog? when it will redraw itself it should draw the loaded image.
I proposed you to draw the image without control - in this case you need to add case WM_PAINT in the Dialog Procedure.
Again:
LoadImage load the BMP-file from the BMP-file or registry. That's the answer for your original question.
If you have related question you are welcome to ask - we'll be glad to assist you.

 

by: jwnrbPosted on 2009-10-09 at 11:28:22ID: 25537507

But here for example, this returns several errors

BOOL DrawPicture(HDC hDC, LPRECT lpRect, HBITMAP hBitmap)
{
        BITMAP bitmap;   // this needs to be declared first
		if (hBitmap == NULL || hDC == NULL || lpRect == NULL)
                return FALSE;
        bitmap = { 0 };  // this gives a LOT of errors
        GetObject(hBitmap, sizeof(BITMAP), &bitmap);
        HDC hMemDC = CreateCompatibleDC(hDC);
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
        StretchBlt(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
                lpRect->bottom - lpRect->top, hMemDC, 0, 0, bitmap.bmWidth, bitmap. bmHeight, SRCCOPY);
        SelectObject(hMemDC, hBitmapOld);
        DeleteDC(hMemDC);
        return TRUE;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window

 

by: jwnrbPosted on 2009-10-09 at 11:30:03ID: 25537530

This is my entire C code.  Maybe thuis can help you more with helping me?

I know I am asking a lot.  I am out of points, but I can raise it up by additional 80 if you like?

#define _IBATWARNING_
 
#include "resource.h"
#include "IBATWarning.h"
 
// NSIS stack structure 
typedef struct _stack_t 
{
	struct _stack_t *next;
	char text[256];
} stack_t;
 
stack_t **g_stacktop;
 
 
// Function prototypes
char *getvar(int varnum);
void setvar(int varnum, char *var);
int runDialogBox();
HBITMAP LoadPicture(UINT nResID);
BOOL DrawPicture(HDC hDC, LPRECT lpRect, HBITMAP hBitmap);
 
// Global variables 
char szBuf[256]="";
char szError[4]="";
int nVarError;
int res = 0;
HINSTANCE g_hInstance;
HWND g_hwndParent;
int g_stringsize;
char *g_variables;
HBITMAP s_hBitmap = NULL;
 
 
 
// wtf is this? this needs to be changed most likely
enum 
{
	INST_0,         // $0
	INST_1,         // $1
	INST_2,         // $2
	INST_3,         // $3
	INST_4,         // $4
	INST_5,         // $5
	INST_6,         // $6
	INST_7,         // $7
	INST_8,         // $8
	INST_9,         // $9
	INST_R0,        // $R0
	INST_R1,        // $R1
	INST_R2,        // $R2
	INST_R3,        // $R3
	INST_R4,        // $R4
	INST_R5,        // $R5
	INST_R6,        // $R6
	INST_R7,        // $R7
	INST_R8,        // $R8
	INST_R9,        // $R9
	INST_CMDLINE,   // $CMDLINE
	INST_INSTDIR,   // $INSTDIR
	INST_OUTDIR,    // $OUTDIR
	INST_EXEDIR,    // $EXEDIR
	__INST_LAST
};
 
BOOL APIENTRY DllMain( HANDLE hModulePar, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
switch (ul_reason_for_call)
    {
case DLL_PROCESS_ATTACH:
	hModule = hModulePar;
	break;
 
case DLL_PROCESS_DETACH:	
	break;
    }
 
return TRUE;
}
 
LRESULT CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	static HBRUSH hBrushStatic;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rect; 
		
	switch(message)
	{
	case WM_INITDIALOG:		
		CheckDlgButton(hDlg, IDC_CHECK, FALSE);
		EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
		return TRUE;
	
	case WM_PAINT:
		hdc = BeginPaint(hDlg, &ps);
		// ::GetClientRect(hWnd, &rect);
		rect.left = 50;
		rect.top = 80;
		rect.right = rect.left + 128;
		rect.bottom = rect.top + 64;
		DrawPicture(hdc, &rect, s_hBitmap);
		EndPaint(hDlg, &ps); 
	case WM_CTLCOLORSTATIC:
		if ((HWND)lParam == GetDlgItem(hDlg,IDC_WARNING))
		{
			SetBkColor((HDC) wParam, (COLORREF) GetSysColor(COLOR_BTNFACE));
			SetTextColor((HDC) wParam, RGB(255, 0, 0));
			SetBkMode((HDC) wParam, TRANSPARENT);
			if(!hBrushStatic)
			{
				hBrushStatic = CreateSolidBrush( (COLORREF) GetSysColor(COLOR_BTNFACE));
			}
			return (UINT) hBrushStatic;
		}
		return FALSE;	
	
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_CHECK:
			if (IsDlgButtonChecked(hDlg, IDC_CHECK))
			{
				EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
				EnableWindow(GetDlgItem(hDlg, IDCANCEL), FALSE);
			}
			else
			{
				EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
				EnableWindow(GetDlgItem(hDlg, IDCANCEL), TRUE);
			}
			break;
		case IDOK:
			{			
				EndDialog(hDlg, TRUE);
				return FALSE;
			}
		case IDCANCEL:
			{				
				EndDialog(hDlg, FALSE);
				return FALSE;
			}
		
	return TRUE;
		}
	}
	return FALSE;
}
// Sets user variable
void setvar( int varnum, char * var ) 
{
	if (var != NULL && varnum >= 0 && varnum < __INST_LAST) 
	{
		lstrcpy (g_variables + varnum*g_stringsize, var);
	}
}
 
// Gets user variable
char *getvar(int varnum) 
{
	if (varnum < 0 || varnum >= __INST_LAST) 
		return NULL;
	return g_variables + varnum*g_stringsize;
}
 
void __declspec(dllexport) Show(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
{
	g_hwndParent=hwndParent;
	g_stringsize=string_size;
	g_variables=variables;
	s_hBitmap = LoadPicture(IDB_WARNG);
    res = runDialogBox();
	if ( res == 0 )
		setvar(INST_0,"Abort" );
	else
		setvar(INST_0,"Agree" );
}
 
 
int runDialogBox()  
{
	int result = FALSE;
	result = DialogBoxParam(hModule, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc, (LPARAM)(NULL));	
	
	return result;
}
 
HBITMAP LoadPicture(UINT nResID)
{
        HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(nResID), 
                IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE); 
        return hBitmap;
}
 
BOOL DrawPicture(HDC hDC, LPRECT lpRect, HBITMAP hBitmap)
{
        BITMAP bitmap;
		if (hBitmap == NULL || hDC == NULL || lpRect == NULL)
                return FALSE;
        bitmap = { 0 };
        GetObject(hBitmap, sizeof(BITMAP), &bitmap);
        HDC hMemDC = CreateCompatibleDC(hDC);
        HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
        StretchBlt(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
                lpRect->bottom - lpRect->top, hMemDC, 0, 0, bitmap.bmWidth, bitmap. bmHeight, SRCCOPY);
        SelectObject(hMemDC, hBitmapOld);
        DeleteDC(hMemDC);
        return TRUE;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-09 at 11:49:47ID: 25537706

:)
Nice.
You are cool guy. But we are just voluneetrs. It's for fun.  Like a hobby.

So you have a pure C program? I mean your files have names like main.c?
Let me check your code.


 

by: jwnrbPosted on 2009-10-09 at 11:50:25ID: 25537713

But where is the picture control defined?

IDB_WARNG is the picture png resource.
IDC_PICT is the picture control ID.

 

by: jwnrbPosted on 2009-10-09 at 11:52:25ID: 25537733

Yes, this is pure C, using only Win32 APIs.  I don't know much about this stuff.  I don't normally even use this, I dabble more in .NET, but the example code I got came in all C.

Perhaps it would of been easier if I redid this in MFC in C++, but I did 98% of it in C using only Win32 APIs, and now I think it's kinda cool how much I actually did in this, even without help on these final touches!

 

by: pgnatyukPosted on 2009-10-09 at 11:55:59ID: 25537775

Ok. Wait a minute.

 

by: pgnatyukPosted on 2009-10-09 at 12:41:06ID: 25538181

Your code is a DLL. Just to show you how this BMP-loading work, I made an empty Win32 application in Visual studio and added empty main.c file. Then, I added a dailog resource with one button on it. Then added a bitmap. The code of my main.c file is attached. I attached also the screenshot.

It will be great if you can update your code yourself.

I don't have the file you are including in the posted code, it is a dll, etc. You are always welcome with more questions.

#include <Windows.h>
#include "resource.h"
 
HBITMAP s_hBitmap = NULL;
HBITMAP LoadPicture(UINT nID);
BOOL DrawPicture(HDC hDC, LPRECT lpRect);
 
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, 
					  WPARAM wParam, LPARAM lParam);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
 
	int ret = DialogBox(GetModuleHandle(NULL), 
		MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);
 
	return 0;
}
 
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	HDC hDC;
	PAINTSTRUCT ps;
	RECT rect;
 
	switch(Message)
	{
	case WM_INITDIALOG:
		s_hBitmap = LoadPicture(IDB_BITMAP1);
		return TRUE;
 
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd, IDOK);
			break;
 
		case IDCANCEL:
			EndDialog(hwnd, IDCANCEL);
			break;
 
		case IDC_BUTTON1:
			EndDialog(hwnd, IDCANCEL);
			break;
		}
		break;
 
	case WM_PAINT:
		hDC = BeginPaint(hwnd, &ps);
		GetWindowRect(hwnd, &rect);
		rect.top += 32;
		rect.left += 32;
		rect.right = rect.left + 128;
		rect.bottom = rect.top + 64;
		DrawPicture(hDC, &rect);
		EndPaint(hwnd, &ps);
		break;
 
	default:
		return FALSE;
	}
 
	return TRUE;
}
 
BOOL DrawPicture(HDC hDC, LPRECT lpRect)
{
	BITMAP bitmap;
	HDC hMemDC;
	HBITMAP hBitmapOld;
	if (s_hBitmap == NULL)
		return FALSE;
 
	GetObject(s_hBitmap, sizeof(BITMAP), &bitmap);
	hMemDC = CreateCompatibleDC(hDC);
	hBitmapOld = (HBITMAP)SelectObject(hMemDC, s_hBitmap);
	StretchBlt(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, hMemDC, 0, 0, bitmap.bmWidth, bitmap. bmHeight, SRCCOPY);
	SelectObject(hMemDC, hBitmapOld);
	DeleteDC(hMemDC);
	return TRUE;
}
 
HBITMAP LoadPicture(UINT nID)
{
	HBITMAP hBitmap;
	hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), 
		MAKEINTRESOURCE(nID), IMAGE_BITMAP, 0, 0, 
		LR_CREATEDIBSECTION | LR_DEFAULTSIZE); 
	return hBitmap;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:

Select allOpen in new window

  • temp.png
    • 18 KB

    screenshot from the dialog app

    screenshot from the dialog app
 

by: pgnatyukPosted on 2009-10-09 at 12:43:31ID: 25538194

I found a nice link: http://winprog.org/tutorial/start.html

Please take a look - a lot of code, tutorials, and all in C.

 

by: jwnrbPosted on 2009-10-09 at 13:24:38ID: 25538534

Yes, I am sorry, it is a dll.  

It is simple a NSIS plugin, where uit just calls up a single dialogue box with several options.
the icon is simply somehting similar to the MB_ICONEXCLAMATION flag.

Though a bit larger.  I am using PNG bc i need the alpha transparency.

 

by: pgnatyukPosted on 2009-10-09 at 13:31:50ID: 25538596

Try to apply the code I posted. If it will work, we will continue.

 

by: jwnrbPosted on 2009-10-09 at 13:32:16ID: 25538602

The new code does compile, however my picture did not show up.  The static control, IDC_PICT is where the bitmap control is.  where does that get populated with the bitmap i am loading whose resource is IDB_WARNG?

 

by: pgnatyukPosted on 2009-10-09 at 13:33:58ID: 25538618

I'm afraid a primitive transparency will be enough.
AlphaBlend is also not a big deal. The file format does not matter for us.


 

by: pgnatyukPosted on 2009-10-09 at 13:35:41ID: 25538632

In the code I posted you can see IDB_BITMAP1. It is your IDB_WARNG.
IDC_PICT is a static control? I'd delete it.

 

by: jwnrbPosted on 2009-10-09 at 13:36:45ID: 25538647

This is the resouece edtiuor.

  • rc.png
    • 60 KB

    Resourece Editor

 

by: jwnrbPosted on 2009-10-09 at 13:37:51ID: 25538661

Some reason the first file wasnt liked :-(

 

by: jwnrbPosted on 2009-10-09 at 13:38:59ID: 25538670

Man, stupid paint.net!

  • rc.png
    • 24 KB

    God, third time is a charm, I swear!

    God, third time is a charm, I swear!
 

by: pgnatyukPosted on 2009-10-09 at 13:39:03ID: 25538671

I don't understand your last comment.

 

by: jwnrbPosted on 2009-10-09 at 13:41:49ID: 25538701

Yes, IDC_PICT is my static control, the picture frame set to Bitmap type.  That is where I want the PNG file, IDB_WARNG to go.

 

by: pgnatyukPosted on 2009-10-09 at 13:46:26ID: 25538733

We can load PNG only if we will use GDI+ or PNGLib.

Do you know how to use GDI+? It is not a pure C. We will need to use Image class. Your application is in C.

Probably you don't have PNGLib? And we cannot use a known wrapper for this library, because I know only class libraries such as CxImage. I do have my own wrapper for it, it is almost C-style, but I cannot post it here.

So you set up such conditions that I will never answer on this question.

 

by: jwnrbPosted on 2009-10-09 at 13:56:48ID: 25538815

Ah, why does Microsoft allow PNG for resource then?  I read it was possible, to do this endeavour, that it must be loaded programmatically rather than usse the Image pull down.  If what I want to do is not possible, is it possible to force it to use the 48x48 icon size instead of 32x32?

 

by: jwnrbPosted on 2009-10-09 at 13:58:21ID: 25538827

I think then, I will just use the standard 32x32 icon, and convert this into MFC C++.

Oh well.

 

by: pgnatyukPosted on 2009-10-09 at 14:03:41ID: 25538860

Yes. You can work with PNG. I post three functions below.

And this small console application (it's from MSDN) will show you how to use GDI+:


#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#pragma comment (lib,"gdiplus.lib")
using namespace Gdiplus;
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
 Image* image = new Image(L"two.png");
printf("The width of the image is %u.\n", image->GetWidth());
printf("The height of the image is %u.\n", image->GetHeight());
 delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}

BOOL DrawPicture(HDC hDC, Image* pImage, LPRECT lpRect)
{
	if (pImage == NULL || lpRect == NULL) 
		return FALSE;
	Graphics graphics(hDC);
	graphics.DrawImage(pImage, lpRect->left, lpRect->top, 
		lpRect->right - lpRect->left, lpRect->bottom - lpRect->top);
	return TRUE;
}
 
Image* LoadPicture(UINT nResID)
{
	HMODULE hModule = GetModuleHandle(NULL);
	HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(nResID), L"Binary");
	if (hResource == NULL)
		return NULL;
 
	HGLOBAL hImage = LoadResource(hModule, hResource);
	if (hImage == NULL)
		return NULL;
	LPVOID pImage = LockResource(hImage);
	if (pImage == NULL)
		return NULL;
 
	Image* pPicture = NULL;
	int size = SizeofResource(hModule, hResource);
	HGLOBAL hBlock = GlobalAlloc(GHND, size);
	if (hBlock != NULL) 
	{
		LPVOID pBlock = GlobalLock(hBlock);
		if (pBlock != NULL) 
		{
			memmove(pBlock, pImage, size);
			IStream* pStream;
			if (CreateStreamOnHGlobal(hBlock, FALSE, &pStream) == S_OK) 
				pPicture = new Image(pStream);
 
			GlobalUnlock(pBlock);
		}
		GlobalFree(hBlock);
	}
	return pPicture;
}
 
Image* LoadPicture(LPCWSTR lpszFileName)
{
	Image* pImage = Image::FromFile(lpszFileName, FALSE);
	return pImage;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

Select allOpen in new window

 

by: jwnrbPosted on 2009-10-09 at 14:10:11ID: 25538901

Is this still Win32/C?

Or do I have to do this now in MFC C++?  
Or some other method?

I wil get back to you on Monday how I make out, I need to get out of the office for now!  Long, long week!

 

by: pgnatyukPosted on 2009-10-09 at 14:15:08ID: 25538932

The code I posted is C++, Win32 API and GDI+.
MFC/ATL have classes that will help help you. But it will be better if you will  open a related question and close this one - I'm sure I have answered.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...