Solved

Loading resources and LoadIconEx in visual c++

Posted on 1998-09-29
11
529 Views
Last Modified: 2013-12-14
I need to be able to load a resource and use LoadIconEx
with DrawIconEx in visual c++, I know how to do this in
Borland C++ butnot in Visual C++. Help in being able to do
this would be appreciated.
0
Comment
Question by:Matlock
  • 5
  • 3
  • 3
11 Comments
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
There's no LoadIconEx function - you mean LoadIcon

The code is the same for these 2 functions as they are Windows API calls which are the same on both compiles.

Try Giving a more specific explanation of the problem (e.g. is the problem going from OWL to MFC ? or something else)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Code that uses these API functions will be identical between Borland and VC.  You should need no changes at all.  code that uses the OWL library and the MFC library will need extensive changes.  But in that case, you wouldin't be using these functions.
0
 

Author Comment

by:Matlock
Comment Utility
Sorry, I guess first of all I need to completely rephrase the question. Again it is an issue of using API and not OWL/MFC/VCL etc.

What I am really looking for is a snippet of code that loads not just an icon but a bitmap and then displays at the most basic of API levels

I mentioned LoadIconEx as I thought it was a new call in WindowsCE that would also load other types of image - I'm probably mistaken

Again any help available as this is another one of my learning windows o/s programming question
Thanks
Matlock
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I would use LoadBitmap() for simple tasks.  It you need to load a bitmap image only and the image must be specified in the resources of your EXE or one of the DLLs it uses.

For more power use LoadImage().  It loads icons and cursors as well.  It can loads from a file on disk. (or from resources)  It has options for performing some usefull manipulations on the loaded image.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
To display a bitmap, you can use BitBlt() to copy it to a DC in its current dimensions or StretchBlt() to copy it to a DC and scale it to a new size (which ussually looks pretty bad, but sometimes you have to do it.)

Why don't you look at these functions and then ask if you have questions.   (As you can see there are lots of posibilities, and I'm not sure exactly what you need.)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
There may be a LoadIconEx in Windows CE (I don't have the CE help installed) but there isn't in normal windows

In API to display a bitmap

Say hDCtarget is the HDC or where you want to display

to display bitmap (forgive typos, I'm typing this right in)

HBITMAP hBitmap ;
HDC hDC ;
BITMAP bm ;

hBitmap = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) ) ; /* hInst is a parameter in WinMain, IDB_BITMAP1 is the resource id of the bitmap which should be included in your EXE */
GetObject( hBitmap, sizeof bm, &bm ) ;

hDC = CreateCompatibleDC( hDC ) ;
SelectObject( hDC, hBitmap ) ;
BitBlt( hDCtarget, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY ) ; /* an nietod says use StretchBlt if you want to scale the bitmap instead of BitBlt */
DeleteDC( hDC ) ;
DeleteObject( hBitmap ) ;


to display an icon (same assumptions)

HICON hIcon ;

hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON) ) ;
DrawIcon( hDCtarget, 0, 0, hIcon ) ;
DeleteObject( hIcon ) ;

You can also use DrawIconEx instead of DrawIcon


You may also use LoadImage to load from a resource or file.  As this returns handle you need to cast the return value depending on what you're loading

hIcon = (HICON)LoadImage( ...etc... ) ;

or

hBitmap = (HBITMAP)LoadBitmap( ...etc... ) ;
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
Comment Utility
There may be a LoadIconEx in Windows CE (I don't have the CE help installed) but there isn't in normal windows

In API to display a bitmap

Say hDCtarget is the HDC or where you want to display

to display bitmap (forgive typos, I'm typing this right in)

HBITMAP hBitmap ;
HDC hDC ;
BITMAP bm ;

hBitmap = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) ) ; /* hInst is a parameter in WinMain, IDB_BITMAP1 is the resource id of the bitmap which should be included in your EXE */
GetObject( hBitmap, sizeof bm, &bm ) ;

hDC = CreateCompatibleDC( hDC ) ;
SelectObject( hDC, hBitmap ) ;
BitBlt( hDCtarget, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY ) ; /* an nietod says use StretchBlt if you want to scale the bitmap instead of BitBlt */
DeleteDC( hDC ) ;
DeleteObject( hBitmap ) ;


to display an icon (same assumptions)

HICON hIcon ;

hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON) ) ;
DrawIcon( hDCtarget, 0, 0, hIcon ) ;
DeleteObject( hIcon ) ;

You can also use DrawIconEx instead of DrawIcon


You may also use LoadImage to load from a resource or file.  As this returns handle you need to cast the return value depending on what you're loading

hIcon = (HICON)LoadImage( ...etc... ) ;

or

hBitmap = (HBITMAP)LoadBitmap( ...etc... ) ;
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
There may be a LoadIconEx in Windows CE (I don't have the CE help installed) but there isn't in normal windows

In API to display a bitmap

Say hDCtarget is the HDC or where you want to display

to display bitmap (forgive typos, I'm typing this right in)

HBITMAP hBitmap ;
HDC hDC ;
BITMAP bm ;

hBitmap = LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) ) ; /* hInst is a parameter in WinMain, IDB_BITMAP1 is the resource id of the bitmap which should be included in your EXE */
GetObject( hBitmap, sizeof bm, &bm ) ;

hDC = CreateCompatibleDC( hDC ) ;
SelectObject( hDC, hBitmap ) ;
BitBlt( hDCtarget, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY ) ; /* an nietod says use StretchBlt if you want to scale the bitmap instead of BitBlt */
DeleteDC( hDC ) ;
DeleteObject( hBitmap ) ;


to display an icon (same assumptions)

HICON hIcon ;

hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON) ) ;
DrawIcon( hDCtarget, 0, 0, hIcon ) ;
DeleteObject( hIcon ) ;

You can also use DrawIconEx instead of DrawIcon


You may also use LoadImage to load from a resource or file.  As this returns handle you need to cast the return value depending on what you're loading

hIcon = (HICON)LoadImage( ...etc... ) ;

or

hBitmap = (HBITMAP)LoadBitmap( ...etc... ) ;
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
Oops sorry about that - having mouse trouble

Now, you should reject it if that's not satisfactory answer
0
 

Author Comment

by:Matlock
Comment Utility
Thanks
apart from the fact I used nietod's suggestion of LoadImage()
The code you gave me works perfectly and at least I now can
see how HBITMAP and BITMAP work together.
So  all's good except for one very strange anomaly
the Bitmap only appears in the window when I resize the window
over and back where the image should be.
I've accpeted the answer as being spot-on but any suggestions
on how to cure this anomaly would be good
Matlock
0
 

Author Comment

by:Matlock
Comment Utility
Apologies
I've found a typo of my own that was causing the strange effect
NOW it works absolutely spot on
Thanks again
Matlock
0

Featured Post

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

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

15 Experts available now in Live!

Get 1:1 Help Now