Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working(in "C") with a couple of dialog boxes in "C" and have them up and running well. NOW, I want to place an image/picture file on the dialog box's screen at designtime and have it stay there. I have the image file referenced in the .rc file. I read somewhere that you could copy and paste that .png file over on the dialog screen and it should appear. So I did, all I received was a big square rectangle with a little dot-flower in the middle of the screen(what a picture). My understanding of dialog boxes usually ends here. Where can I find some working "C" code examples showing me how this is done so I can get back to work. I could cut and paste my two functions paste them below, but that would take up too much space. I'm looking something that's simple, but yet it works well. Well thanks, but it's back to the grind.

15028314

What I have tried:

I've built "C" functions that calls these two dialog boxes and they're working well from within a "C"/C# program. Put together "C" program to call these dialog boxes.
Posted
Updated 30-Sep-22 2:18am

People rarely do this in C. Usually C++ with MFC or .net is used. Anyway, here is a sample that uses C++ and MFC : Working with Custom Background Bitmaps in Dialog Boxes and Property Sheets[^]. Here's another one : Bitmap Backgrounds For Dialog Boxes[^]. Here's another one in C: VC, Background image for dialog[^]. I found these by searching for dialog background image.

To do this in C you will need to implement handlers for WM_ERASEBACKGRND and WM_SIZE and translate the code for those handlers from the articles to work with your program. The MFC versions do exactly this but it's wrapped into class methods. If you get stuck you can always ask another question.
 
Share this answer
 
Try this code in response to the WM_ERASEBKGND message:
C++
BOOL OnEraseBkgnd(
	HWND	hDlg,
	HDC		hDC    // this is passed in WPARAM field in the message handler
)
{
	BITMAP bm;
    // if loading from a file then you need the filenmae and the LR_LOADFROMFILE option
	HBITMAP	hBmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BACKGROUND), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
	HDC hdcMem = CreateCompatibleDC(hDC);
	HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBmp); // select the bitmap into the memory DC

	GetObject(hBmp, sizeof(bm), &bm);  // get the BITMAP object from the handle

	BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); // Blit the image onto the background

	SelectObject(hdcMem, hbmOld); // restore the default
	DeleteDC(hdcMem);             // release the Windows resource

	return true;  // tell windows not to paint over the background.
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900