NO

Author Topic: Would like to change default icon in Console Window. Only lead I have is C++ !  (Read 16429 times)

EdPellesC99

  • Guest


   Hi,

   I would like the ability to change the default title bar icon of the console window.

   The only lead I have is an undocumented function in the kernel32.dll called "SetConsoleIcon".
The only code I have found is in C++.

   I really don't know where I should post as this is for C, and a C++ forum would be upset if I asked for help in converting to C.

   I have enough right now trying to learn C, so I don't see how I could learn enough about C++ very quickly to convert the code to C.

   Does anyone have any experience changing the icon to a custom icon?

   Is there a C++ Guru out there who could help converting?

   Of course if I compile as a .c file, I get so many errors I hardly know where to start.

   I wrote the source file author, but he has never written back.

   Source code from one of several jobs on this page:
http://nibuthomas.wordpress.com/category/undocumented-winapi/

Here is the code:
Code: [Select]

#include <stdio.h>
#include <stdlib.h>


void ChangeIcon( const HICON hNewIcon )
{
   // Load kernel 32 library
   HMODULE hMod = LoadLibrary( _T( "Kernel32.dll" ));
   ASSERT( hMod );

   // Load console icon changing procedure
   typedef DWORD ( __stdcall *SCI )( HICON );
   SCI pfnSetConsoleIcon = reinterpret_cast<sci>( GetProcAddress( hMod, "SetConsoleIcon" ));
   ASSERT( pfnSetConsoleIcon );

   // Call function to change icon
   pfnSetConsoleIcon( hNewIcon );

   FreeLibrary( hMod );
}// End ChangeIcon

// Main function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   int nRetCode = 0;

   HMODULE hMainMod = GetModuleHandle( 0 );
   ASSERT( hMainMod );

   HICON hMainIcon = ::LoadIcon( hMainMod, MAKEINTRESOURCE( IDI_ICON3 ));
   ASSERT( hMainIcon );

   // Change main window icon
   ChangeIcon( hMainIcon );

   // To reset to old icon uncomment this code
   // ChangeIcon( 0 );

   return nRetCode;
}


If nothing else can someone suggest a forum I should post to in C++ ?

   Thanks much for any help/leads.

   Ed
« Last Edit: September 10, 2010, 04:23:58 PM by EdPellesC99 »

EdPellesC99

  • Guest


  Like to add that I have been unable to compile the file using standard includes using CodeBlocks.
Can someone verify this .cpp file can be compiled and allows a change of icon?

  Otherwise I will get back when I have found my answers.

  Ed

EdPellesC99

  • Guest

  Ok here is another lead..... also C++.
The prototype and the function. I just need it modified to be C !

A lead fr:
http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx


Code: [Select]
#include <windows.h>
#include <tchar.h>


BOOL WINAPI SetConsoleIcon(HICON hIcon);  //prototype

BOOL WINAPI SetConsoleIcon(HICON hIcon) {
typedef BOOL (WINAPI *PSetConsoleIcon)(HICON);
static PSetConsoleIcon pSetConsoleIcon = NULL;
if(pSetConsoleIcon == NULL)
pSetConsoleIcon = (PSetConsoleIcon)::GetProcAddress(::GetModuleHandle(_T("kernel32")), "SetConsoleIcon");
if(pSetConsoleIcon == NULL) return FALSE;
return pSetConsoleIcon(hIcon);
}



  Thanks, Ed
« Last Edit: August 16, 2010, 05:48:11 PM by EdPellesC99 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Code: [Select]
#include <windows.h>
#include <tchar.h>

#pragma lib "user32.lib"

BOOL WINAPI SetConsoleIcon(HICON hIcon);  //prototype

BOOL WINAPI SetConsoleIcon(HICON hIcon) {
typedef BOOL (WINAPI *PSetConsoleIcon)(HICON);
static PSetConsoleIcon pSetConsoleIcon = NULL;
if(pSetConsoleIcon == NULL)
pSetConsoleIcon = (PSetConsoleIcon)GetProcAddress(GetModuleHandle(_T("kernel32")), "SetConsoleIcon");
if(pSetConsoleIcon == NULL)
return FALSE;
return pSetConsoleIcon(hIcon);
}

int main(int argc, char **argv)
{
SetConsoleIcon(LoadIcon(0, IDI_EXCLAMATION));
return 0;
}
« Last Edit: August 16, 2010, 09:35:19 AM by timovjl »
May the source be with you

EdPellesC99

  • Guest

   Timo !!!!

 
Thanks very much ! It would have taken me a lot of time over a period of a month or more to have come close, and probably I would have given up .... to try again another year !

   Yikes I just can't believe it.  ;D ;D I think it is an understatement: you really know your stuff ! You are more capable, and willing to share your expertise than I often see in forums. Usually in forums elsewhere (on all subjects) the responder is either capable and not helpful, or helpful but not capable enough to help very much!

  Any forum lucky enough to have your regular participation, has a lot of lucky forum members.

   I will be playing with over the next couple days....... and come back with more comment.

   Just super. It will help so much with making console apps better looking, and interesting to work with using C.
   Can't say enough !!!!!!!!!!

   Thanks again,
   
   Ed

EdPellesC99

  • Guest

   Timo


   Demonstrating my appreciation for your help (by my quick get-back). Here is the project and source files I can contribute to the forum.
A working example project with a custom icon resource.


Quote

// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •


#include <windows.h>
#include <stdio.h>
#include <tchar.h> //using in SetConsoleIcon external function
#include "resource.h"
// // ~  Compiles w/ ZE option
#pragma lib "user32.lib" // needed for LoadIcon in SetConsoleIcon function

// // ~ •  •  •  •  •  •  •  •  •  •  •  •
BOOL WINAPI SetConsoleIcon(HICON hIcon)//prototype
// // ~ •  •  •  •  •  •  •  •  •  •  •  •


int main(int argc, char **argv)
{
//SetConsoleIcon(LoadIcon(0, IDI_EXCLAMATION));

HMODULE hMainMod = GetModuleHandle( 0 );
SetConsoleIcon(LoadIcon( hMainMod, MAKEINTRESOURCE( EdsIcon1)));
printf("Cool Work Timo. Best Icon loaded !\n\n");
system("pause");
return 0;
}



BOOL WINAPI SetConsoleIcon(HICON hIcon)
{
typedef BOOL (WINAPI *PSetConsoleIcon)(HICON);
static PSetConsoleIcon pSetConsoleIcon = NULL;
if(pSetConsoleIcon == NULL)
pSetConsoleIcon = (PSetConsoleIcon)GetProcAddress(GetModuleHandle(_T("kernel32")), "SetConsoleIcon");
if(pSetConsoleIcon == NULL)
return FALSE;
return pSetConsoleIcon(hIcon);
}

// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •






   Thanks again,

   Ed

Source files attached.

« Last Edit: August 17, 2010, 05:42:20 AM by EdPellesC99 »

Juni

  • Guest
Nice Demo ty  :)

( I had to change the icon in the .ppj to Pelle_Orinius.ico )

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Here is another version :

Code: [Select]
#include <windows.h>

/* Check BuildLib.lib and kernel32.def to build kernel32v2.lib exporting the SetConsoleIcon function */

BOOL WINAPI SetConsoleIcon(HICON hIcon);

int main( int argc, char **argv )
{
HICON hIcon=LoadIcon(GetModuleHandle(0),MAKEINTRESOURCE(200));

  MessageBox(0,"Click the message box to change the console icon","Test",MB_OK);
 
    SetConsoleIcon(hIcon);
 
    MessageBox(0,"Console icon changed","Test",MB_OK);

return 0;
}
Code it... That's all...

EdPellesC99

  • Guest

  Thanks Vortex,

  I will check it out and get back in a day.

  ....... Ed

dbareis

  • Guest
Demonstrating my appreciation for your help (by my quick get-back). Here is the project and source files I can contribute to the forum.
A working example project with a custom icon resource.

I'm trying to get this to work in Visual Studio 2017. The generated EXE runs and says "Cool Work Timo. Best Icon loaded", but doesn't change the console icon.  Is it possible it doesn't work in 64 bit WIN10?

Quote
// // ~  Compiles w/ ZE option
I'm new to VS (and very rusty in C), am I supposed to set these options somewhere?

Quote
#pragma lib "user32.lib" // needed for LoadIcon in SetConsoleIcon function
This #pragma is unknown

I'm going to keep hammering at it but with any luck someone will take pity and point me to the solution.


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Yes, it won't work in Windows 10.

replace
Code: [Select]
#pragma lib "user32.lib"with
Code: [Select]
#pragma comment(lib, "user32.lib")
/Ze is for PellesC msvc compability mode

EDIT: for Windows 10
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#pragma comment(lib, "user32.lib") // LoadIcon

int main(int argc, char **argv)
{
HWND hWnd = GetConsoleWindow();
HICON hIcon = LoadIcon(0, IDI_EXCLAMATION);
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
// SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
return 0;
}
« Last Edit: July 30, 2017, 07:11:05 AM by TimoVJL »
May the source be with you