C language > Expert questions

Would like to change default icon in Console Window. Only lead I have is C++ !

(1/3) > >>

EdPellesC99:


   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: ---
#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;
}


--- End code ---

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

   Thanks much for any help/leads.

   Ed

EdPellesC99:


  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:

  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: ---#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);
}


--- End code ---


  Thanks, Ed

TimoVJL:

--- Code: ---#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;
}

--- End code ---

EdPellesC99:

   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

Navigation

[0] Message Index

[#] Next page

Go to full version