How to add icon (.ico file) to the submenu ?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 6806 Views - Last Post: 24 May 2012 - 03:51 PM Rate Topic: -----

#1 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 01:30 AM

How to add icon (.ico file) to the submenu ?
i have 3 files on my visual c++ project below
1. resource.h
2. resource.rc
3. main.cpp

The code are below
1. resource.h
#define METU_PEOPLE 101
#define METU_NEWS 102
#define METU_CLOSE 103
#define METU_ABOUT 104

#define METU_MENU 201
#define METU_FAVICON 202




2. resource.rc
#include "resource.h"

METU_PEOPLE ICON "images/people.ico"
METU_NEWS ICON "images/news.ico"
METU_CLOSE ICON "images/close.ico"

METU_FAVICON ICON "images/favicon.ico"
METU_MENU MENU

BEGIN
	POPUP "&Messenger"
		BEGIN
			MENUITEM "People", METU_PEOPLE
			MENUITEM "News", METU_NEWS
			MENUITEM "Exit", METU_CLOSE
		END
	POPUP "&Help"
		BEGIN
			MENUITEM "About", METU_ABOUT
		END
END



3. main.cpp
#include <windows.h>
#include "resource.h"


//kita deklarasikan (declare) identifier WinProc
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain( //convention system function
	HINSTANCE hInst, //Handle to instance
	HINSTANCE hPrevInst, // handle previous instance
	LPSTR lpCmdLine, //pointer str dalam unicode
	int nShowCmd) //fungsi yang dipanggil, maximize, minimize etc,
{
	WNDCLASSEX wClass; //mari kita buat window class 'wClass'
	ZeroMemory(&wClass,sizeof(WNDCLASSEX)); //terlebih dahulu kita clear kan semua parameter
	wClass.cbClsExtra=NULL;
	wClass.cbSize=sizeof(WNDCLASSEX);
	wClass.cbWndExtra=NULL;
	wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wClass.hIcon= LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_FAVICON));
	wClass.hIconSm= (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_FAVICON), IMAGE_ICON, 16, 16, 0);//tidak perlu 16x16 bila sudah 16
	wClass.hInstance=hInst;
	wClass.lpfnWndProc=(WNDPROC)WinProc;
	wClass.lpszClassName="Window Class";
	wClass.lpszMenuName= MAKEINTRESOURCE(METU_MENU);
	wClass.style=CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClassEx(&wClass)) //mari kita register class
	{
		//bila windows class gagal maka akan return 0
		//tapi kita dapat menggunakan fungsi GetLastError();
		int nResult = GetLastError();
		MessageBox(NULL,
			"Window class creation failed",
			"Window Class Failed",
			MB_IConerror);
	}

	//HWND = Handle to Window, salah satu bagian dari windows api
	HWND hWnd = CreateWindowEx(
			NULL, //dwExStyle
			"Window Class", //lpClassName
			"MeTu+ Messenger", //lpWindowName
			WS_OVERLAPPEDWINDOW, //dwStyle
			200,//x
			200,//y
			640,//nWidth
			480,//nHeight
			NULL,//hWndParent
			NULL,//hMenu
			hInst,//hInstance
			NULL);//lpParam

	if(!hWnd)
	{
		int nResult=GetLastError();

		MessageBox(NULL,
			"Window creation failed",
			"Window Creation Failed",
			MB_IConerror);
	}

	ShowWindow(hWnd,nShowCmd);

	//dibawah ini merupakan main loop untuk program
	//digunakan untuk mengolah message
	//sebelum window ditampilkan
	MSG msg;
	ZeroMemory(&msg,sizeof(MSG));

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg); //translates virtual key messages to character messages
		DispatchMessage(&msg); //sends any messages to the callback procedure
	}

	return 0;
}


//dibawah ini merupakan callback prosedure
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
		case WM_DESTROY://saat user mengclick tombol close (x) pada window
			{
				PostQuitMessage(0);
				return 0;
			}
			break;
		case WM_CREATE:
			{

			}
			break;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}



I don't have any idea to place my .ico files to the submenu,
can somebody tell me how?

Is This A Good Question/Topic? 0
  • +

Replies To: How to add icon (.ico file) to the submenu ?

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,236
  • Joined: 20-March 10

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 03:45 AM

Yes, i can actually

See my Tutorial on it here


My Tutorial click here

Best wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 08:15 AM

nice tutorial, but you use bitmaps, i'm using ico,
i can not use your code there.
any another suggestion for ico file?
Was This Post Helpful? 0
  • +
  • -

#4 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 08:28 AM

On your first block, you use the code below
HBITMAP hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(BM1));
HBITMAP hBitmap2 = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(BM2));



then, now i'm trying the same
HICON METU_PEOPLE_ICON = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_PEOPLE));
HICON METU_NEWS_ICON = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_NEWS));
HICON METU_CLOSE_ICON = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_CLOSE));
static HMENU Menu;



then, you write on your 6th block
case WM_CREATE:
	 
	    {
	 
	        Menu = GetMenu(hwnd);
	 
	        SetMenuItemBitmaps(Menu, IDM_SMILEY, MF_BYCOMMAND, hBitmap , hBitmap2);
	        SetMenuItemBitmaps(Menu, IDM_TREE, MF_BYCOMMAND, sBitmap , sBitmap2);
	        SetMenuItemBitmaps(Menu, IDM_CAR, MF_BYCOMMAND, lBitmap , lBitmap2);
	        break;
	    }



So, what should i write in this message loop? i really don't have any idea
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,236
  • Joined: 20-March 10

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 09:31 AM

You would convert your icons
To bitmaps

HBITMAP menuicon1;
ICONINFO iconinfo1;
GetIconInfo(METU_PEOPLE_ICON, &iconinfo1);
DeleteObject(iconinfo1.hbmMask);
DestroyIcon(METU_PEOPLE_ICON);
menuicon1 = iconinfo1.hbmColor;

Use SetMenuItemBitmap as normal

Or just use bitmaps instead of icons
Why are you so attached to icons anyway ?


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#6 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 10:06 AM

I prefer use .ico becouse it can be transparent,
bitmap is really ugly,

oke, let me try digging
Was This Post Helpful? 0
  • +
  • -

#7 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 10:13 AM

View Postsnoopy11, on 17 May 2012 - 09:31 AM, said:

Why are you so attached to icons anyway ?


Snoopy.


And what does it mean?
Was This Post Helpful? 0
  • +
  • -

#8 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 10:23 AM

i have just tried it, unfortunately, my image become black, please see the picture attached,

This post has been edited by OKIEWARDOYO: 17 May 2012 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,236
  • Joined: 20-March 10

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 02:00 PM

Right,

Well I have tested this and it works for me

Don't know what your doing wrong.

Its simpler using bitmaps as there is no direct way to place individual .ico files on menus

There is no SetMenuItemIcon command.

Why are bitmaps ugly I dont understand that

Yet sometimes I do struggle to understand people on this site in general.

Well sometimes...

I should not be so grumpy... well sometimes.


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#10 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 02:45 PM

Unfortunately, you cannot use icons directly in a menu that I am aware of. You could store the image as an icon in your resource then convert it to a bmp to display in the menu. AFAIK, bitmaps can be transparent too, you would use LoadImage and the flag LR_LOADTRANSPARENT and it will take the color of the first pixel in your image and turn any pixel in the image (that is that color) transparent.
Was This Post Helpful? 0
  • +
  • -

#11 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 03:57 PM

@snoopy11:

Quote

Well I have tested this and it works for me

Don't know what your doing wrong.

Its simpler using bitmaps as there is no direct way to place individual .ico files on menus

There is no SetMenuItemIcon command.


Actually it works for me too, but my ico files are icons with transparent border, using your code above my image become a black image, i have attached the screenshot but i don't know everytime i click 'attach this file' nothing happend.

Thanks for imform me that there is no direct way to place individeoal .ico files on menus. Hi, wait, i see many programs have their transparent border icon on the menu, is that .ico or bitmaps?? or png???

@GunnerInc:

Quote

AFAIK, bitmaps can be transparent too, you would use LoadImage and the flag LR_LOADTRANSPARENT and it will take the color of the first pixel in your image and turn any pixel in the image (that is that color) transparent.


Really???
like this?
LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(METU_FAVICON), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
let's me try

This post has been edited by OKIEWARDOYO: 17 May 2012 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 04:12 PM

The only sample code that I can show you is in Assembly but here is what you do:
Using a 16x16 bitmap (Actually 2 bitmaps the second one contains the mask)

LoadImage with the IMAGE_BITMAP flag for the normal bitmap and save the returned handle

LoadImage with the IMAGE_BITMAP flag for the bitmap with the mask and save the returned handle.

create a variable for an ICONINFO structure
.fIcon == FALSE
.xHotspot == 0
.yHotspot == 0
.hbmMask == handle of the masked bitmap
.hbmColor == handle of the colored bitmap

Pass that info to CreateIconIndirect and it will return an icon handle... Oh wait, you need a bitmap not an icon eh I will leave this in the post anyways if someone needs it.

Icon to bitmap:
HBITMAP BitmapFromIcon(HICON hIcon)
{
   HDC hDC = CreateCompatibleDC(NULL);
   HBITMAP hBitmap = CreateCompatibleBitmap(hDC, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
   HBITMAP hOldBitmap = (HBITMAP)SelectObject(hDC, hBitmap);
   DrawIcon(hDC, 0, 0, hIcon);
   SelectObject(hDC, hOldBitmap);
   DeleteDC(hDC);

   return hBitmap;
}


I do believe you can use icons directly in menus BUT you have to use owner drawn menus.

This post has been edited by GunnerInc: 17 May 2012 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#13 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,236
  • Joined: 20-March 10

Re: How to add icon (.ico file) to the submenu ?

Posted 17 May 2012 - 11:36 PM

Yes in owner draw menus
you can use anything .png .jpg .ico whatever you like

since you are handling all the drawing.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#14 OKIEWARDOYO  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-April 12

Re: How to add icon (.ico file) to the submenu ?

Posted 23 May 2012 - 12:06 PM

@gunner inc: do you thinkthat we should have at least 2 bitmap? colored and mask? i have found about this here http://www.winprog.o...ansparency.html unfortunately, it was dificult to implemented. Any solution except creating 2 bitmaps?

@snoopy: "owner draw menus", what does it mean? what is "owner draw menu"?
Was This Post Helpful? 0
  • +
  • -

#15 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: How to add icon (.ico file) to the submenu ?

Posted 23 May 2012 - 02:16 PM

If you are just using bitmaps, then yes you need two: one normal and one for the mask so the functions now which pixels to make transparent.

Owner Drawn anything:
Normal controls the OS is responsible for drawing text, pictures, borders, colors, painting, updating, etc...

Owner Drawn is what it sounds like, YOU the owner of the control is responsible for all visual aspects of the control instead of the OS, so you could make purple controls if you wanted to with the first character bold and a different font face if you wanted to.. In other words, you have complete control of what the control looks like.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2