Using Controls in the Microsoft Foundation Class Library - Image Handling continued (Page 4 of 4 )
Now we move on to the proper image displaying. It can be done with a few lines added in the OnDraw section:
if(!_fileName.IsEmpty())
{
Graphics graphics(*pDC);
Image image(_fileName);
graphics.DrawImage(&image, 0, 40);
}
"If _fileName isn't empty" means that we had to choose an image from our HDD. So we have that to show on the screen. We can now move on.
First we create a device that is assigned to the current client area. The image is created by referring to the fileName(Path Name). The pure draw part is resolved in the final line. The DrawImage function handles it, carrying three parameters. The first points toward the image itself. The remaining two are coordinates pointing to the place where the image should be drawn in the client area.
Pretty simple, isn't it? Build the project and start up the application. If you made everything correctly, after opening a picture you should see something like this (obviously with the opened image):

And here is the zipped source file I created just in case you have any issues creating it on your own. Be aware that I used Visual Studio 2005, so if you have an earlier version, you will need to create a new solution/workspace for it.

Conclusion
In this article, we concluded the GDI part of MFC. You learned about standard controls (edit box), common controls (the spinner), and also how to handle/display your pictures. Also you got a small introduction to the power of GDI+. We need at least 7-8 long lines to open an image in the traditional way, while we can accomplish the same task with three short lines using GDI+.
Don't go far away because we are close to the end. One last part is waiting for you where we'll cover all that you should know about the resource editor, creating dialog boxes and filling them with standard/common controls, customizing your applications icon, acceleration keys, and modifying the menu so your toolbar won't be forgotten.
Until next time, I wish you a pleasant day and don't forget, if there are any further questions or unresolved issues that are related to this article or to IT in general, visit the friendly, ever-growing community at DevHardware Forums.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |