⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 icons.c

📁 ICon文件格式
💻 C
📖 第 1 页 / 共 4 页
字号:
    // Return the count
    return Input;
}
/* End ReadICOHeader() ****************************************************/




/****************************************************************************
*
*     FUNCTION: MyEnumProcedure
*
*     PURPOSE:  Callback for enumerating resources in a DLL/EXE
*
*     PARAMS:   HANDLE  hModule  - Handle of the module
*               LPCTSTR lpszType - Resource Type
*               LPTSTR  lpszName - Resource Name
*               LONG    lParam   - Handle of ListBox to add name to
*
*     RETURNS:  BOOL - TRUE to continue, FALSE to stop
*
* History:
*                July '95 - Created
*
\****************************************************************************/
BOOL CALLBACK MyEnumProcedure( HANDLE  hModule, LPCTSTR  lpszType, LPTSTR  lpszName, LONG  lParam )	
{
    TCHAR	szBuffer[256];
    LONG    nIndex = LB_ERR;
    LPTSTR	lpID = NULL;

    // Name is from MAKEINTRESOURCE()
    if( HIWORD(lpszName) == 0 )
    {
        wsprintf( szBuffer, "Icon [%d]", (DWORD)lpszName );
        lpID = lpszName;
    }
    else
    {
        // Name is string
        lpID = strdup( lpszName );
        wsprintf( szBuffer, "Icon [%s]", lpID );
    }
    // Add it to the listbox
    nIndex = SendDlgItemMessage( (HWND)lParam, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)(szBuffer) );
    // Set the item data to be the name of the resource so we can get it later
    SendDlgItemMessage( (HWND)lParam, IDC_LIST1, LB_SETITEMDATA, (WPARAM)nIndex, (LPARAM)lpID );
    return TRUE;
}
/* End MyEnumProcedure() ***************************************************/



/****************************************************************************
*
*     FUNCTION: GetIconFromInstance
*
*     PURPOSE:  Callback for enumerating resources in a DLL/EXE
*
*     PARAMS:   HINSTANCE hInstance - Instance handle for this module
*               LPTSTR    nIndex    - Resource index
*
*     RETURNS:  HICON - Handle to the icon, NULL for failure
*
* History:
*                July '95 - Created
*
\****************************************************************************/
HICON GetIconFromInstance( HINSTANCE hInstance, LPTSTR nIndex )
{
    HICON	hIcon = NULL;
    HRSRC	hRsrc = NULL;
    HGLOBAL	hGlobal = NULL;
    LPVOID	lpRes = NULL;
    int    	nID;

    // Find the group icon
    if( (hRsrc = FindResource( hInstance, nIndex, RT_GROUP_ICON )) == NULL )
        return NULL;
    if( (hGlobal = LoadResource( hInstance, hRsrc )) == NULL )
        return NULL;
    if( (lpRes = LockResource(hGlobal)) == NULL )
        return NULL;

    // Find this particular image
    nID = LookupIconIdFromDirectory( lpRes, TRUE );
    if( (hRsrc = FindResource( hInstance, MAKEINTRESOURCE(nID), RT_ICON )) == NULL )
        return NULL;
    if( (hGlobal = LoadResource( hInstance, hRsrc )) == NULL )
        return NULL;
    if( (lpRes = LockResource(hGlobal)) == NULL )
        return NULL;
    // Let the OS make us an icon
    hIcon = CreateIconFromResource( lpRes, SizeofResource(hInstance,hRsrc), TRUE, 0x00030000 );
    return hIcon;
}
/* End GetIconFromInstance() ***********************************************/



/****************************************************************************
*
*     FUNCTION: ExtractDlgProc
*
*     PURPOSE:  Window Procedure for the Extract Dialog
*
*     PARAMS:   HWND hWnd     - This window handle
*               UINT Msg      - Which Message?
*               WPARAM wParam - message parameter
*               LPARAM lParam - message parameter
*
*     RETURNS:  BOOL - FALSE for cancel, TRUE for ok
*
* History:
*                July '95 - Created
*
\****************************************************************************/
BOOL CALLBACK ExtractDlgProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
    // Variable that holds info on this EXE/DLL
    static LPEXEDLLICONINFO lpEDII;

    switch( Msg )
    {
        // During Paint, we will draw the currently selected icon
        case WM_PAINT:
        {
            HDC                	hDC;
            PAINTSTRUCT        	ps;
            DWORD            	nIndex;
            LPTSTR            	lpIconID;

            hDC = BeginPaint( hWnd, &ps );
            // Get the current selection
            if( (nIndex = SendDlgItemMessage( hWnd, IDC_LIST1, LB_GETCURSEL, 0, 0 )) != CB_ERR )
            {
                // Get the data associated with the current selection - its the icon name
                if( (lpIconID = (LPTSTR)SendDlgItemMessage( hWnd, IDC_LIST1, LB_GETITEMDATA, nIndex, 0 )) != (LPTSTR)CB_ERR )
                {
                    RECT        Rect, ButtonRect, DlgRect;
                    HWND        hWndButton;
                    HICON    	hIcon;
                    ICONINFO    IconInfo;
                    BITMAP    	bm;
                    POINT    	UpperLeft, LowerRight;

                    // Make an Icon
                    hIcon = GetIconFromInstance( lpEDII->hInstance, lpIconID );
                    // Locate the icon
                    GetIconInfo( hIcon, &IconInfo );
                    GetObject( IconInfo.hbmColor, sizeof(BITMAP), &bm );
                    hWndButton = GetDlgItem( hWnd, IDCANCEL );
                    GetWindowRect( hWndButton, &ButtonRect );
                    GetWindowRect( hWnd, &DlgRect );
                    UpperLeft.x = ButtonRect.left;
                    UpperLeft.y = ButtonRect.bottom;
                    LowerRight.x = ButtonRect.right;
                    LowerRight.y = DlgRect.bottom;
                    ScreenToClient( hWnd, &UpperLeft );
                    ScreenToClient( hWnd, &LowerRight );
                    SetRect( &Rect, UpperLeft.x, UpperLeft.y, LowerRight.x, LowerRight.y );
                    // Draw it
                    DrawIcon( hDC, Rect.left + ((Rect.right - Rect.left - bm.bmWidth)/2), 
                            Rect.top + ((Rect.bottom - Rect.top - bm.bmHeight)/2), hIcon );
                    // Kill it
                    DestroyIcon( hIcon );
                }
            }
            EndPaint( hWnd, &ps );
        }
        break; // End WM_PAINT

        // Dialog is being initialized
        case WM_INITDIALOG:
        {
            UINT    nCount;
            TCHAR	szBuffer[MAX_PATH], szFileTitle[MAX_PATH];

            // Are we being sent data about an EXE/DLL?
            if( (lpEDII = (LPEXEDLLICONINFO)lParam) != NULL )
            {
                // Set the title of the dialog to reflect the EXE/DLL filename
                GetFileTitle( lpEDII->szFileName, szFileTitle, MAX_PATH );
                wsprintf( szBuffer, "Extract Icon [%s]", szFileTitle );
                SetWindowText( hWnd, szBuffer );
                // Fill in the listbox with the icons available
                if( ! EnumResourceNames( lpEDII->hInstance, RT_GROUP_ICON, MyEnumProcedure, (LPARAM)hWnd ) )
                {
                    MessageBox( hWnd, "Error Enumerating Icons", "Error", MB_OK );
                    PostMessage( hWnd, WM_CLOSE, 0, 0 );
                }
                SendDlgItemMessage( hWnd, IDC_LIST1, LB_SETCURSEL, 0, 0 );
                // If we have <= 1, post an OK message
                if( (nCount = SendDlgItemMessage(hWnd, IDC_LIST1, LB_GETCOUNT, 0, 0)) == 1 )
                {
                    PostMessage( hWnd, WM_COMMAND, IDOK, 0 );
                }
                // If there were no icons, let the user know
                if( nCount == 0 )
                {
                    MessageBox( hWnd, "No Icons in this File", "Error", MB_OK );
                    PostMessage( hWnd, WM_CLOSE, 0, 0 );
                }
            }
            return FALSE;
        }
        break; // End WM_INITDIALOG

        // Shut 'er down
        case WM_CLOSE:
            PostMessage( hWnd, WM_COMMAND, IDCANCEL, 0l );
        break; // End WM_CLOSE

        // Children are sending messages
        case WM_COMMAND:
            switch( LOWORD(wParam) )
            {
                // Its the listbox, just redraw the icon
                case IDC_LIST1:
                    switch( HIWORD(wParam) )
                    {
                        case CBN_SELCHANGE:
                        case CBN_SELENDOK:
                            InvalidateRect( hWnd, NULL, TRUE );
                        break;
                    }
                break; // End IDC_LIST1

                // User has chosen an icon, shut it down
                case IDOK:
                {
                    LONG nIndex;

                    lpEDII->lpID = NULL;
                    if( (nIndex = SendDlgItemMessage( hWnd, IDC_LIST1, LB_GETCURSEL, 0, 0 )) != LB_ERR )
                        lpEDII->lpID = (LPTSTR)SendDlgItemMessage( hWnd, IDC_LIST1, LB_GETITEMDATA, nIndex, 0 );
                    EndDialog( hWnd, TRUE );
                }
                break; // End IDOK

                // BAIL!
                case IDCANCEL:
                    EndDialog( hWnd, FALSE );
                break; // End IDCANCEL

            }
        break;
        default:
            return FALSE;
        break;
    }
    return TRUE;
}
/* End ExtractDlgProc() ****************************************************/




/****************************************************************************
*
*     FUNCTION: ChooseIconFromEXEFile
*
*     PURPOSE:  Ask the user which icon he/she wants from the DLL/EXE
*
*     PARAMS:   LPEXEDLLICONINFO lpEDII - info on this DLL/EXE
*
*     RETURNS:  LPTSTR - pointer to the resource name
*
* History:
*                July '95 - Created
*
\****************************************************************************/
LPTSTR ChooseIconFromEXEFile( LPEXEDLLICONINFO lpEDII )
{
    // Just launch the dialog box and let it handle it
    if( DialogBoxParam( hInst, MAKEINTRESOURCE(IDD_EXTRACTDLG), hWndMain, ExtractDlgProc, (LPARAM)(lpEDII) ) )
    {
        // User chose 'Ok'
        return lpEDII->lpID;
    }
    // User chose 'Cancel', or an error occurred, fail the call
    return NULL;
}
/* End ChooseIconFromEXEFile() **********************************************/




/****************************************************************************
*
*     FUNCTION: ReadIconFromEXEFile
*
*     PURPOSE:  Load an Icon Resource from a DLL/EXE file
*
*     PARAMS:   LPCTSTR szFileName - name of DLL/EXE file
*
*     RETURNS:  LPICONRESOURCE - pointer to icon resource
*
* History:
*                July '95 - Created
*
\****************************************************************************/
LPICONRESOURCE ReadIconFromEXEFile( LPCTSTR szFileName )
{
    LPICONRESOURCE    	lpIR = NULL, lpNew = NULL;
    HINSTANCE        	hLibrary;
    LPTSTR            	lpID;
    EXEDLLICONINFO    	EDII;

    // Load the DLL/EXE - NOTE: must be a 32bit EXE/DLL for this to work
    if( (hLibrary = LoadLibraryEx( szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE )) == NULL )
    {
        // Failed to load - abort
        MessageBox( hWndMain, "Error Loading File - Choose a 32bit DLL or EXE", szFileName, MB_OK );
        return NULL;
    }
    // Store the info
    EDII.szFileName = szFileName;
    EDII.hInstance = hLibrary;
    // Ask the user, "Which Icon?"
    if( (lpID = ChooseIconFromEXEFile( &EDII )) != NULL )
    {
        HRSRC        	hRsrc = NULL;
        HGLOBAL        	hGlobal = NULL;
        LPMEMICONDIR    lpIcon = NULL;
        UINT            i;

        // Find the group icon resource
        if( (hRsrc = FindResource( hLibrary, lpID, RT_GROUP_ICON )) == NULL )
        {
            FreeLibrary( hLibrary );
            return NULL;
        }
        if( (hGlobal = LoadResource( hLibrary, hRsrc )) == NULL )
        {
            FreeLibrary( hLibrary );
            return NULL;
        }
        if( (lpIcon = LockResource(hGlobal)) == NULL )
        {
            FreeLibrary( hLibrary );
            return NULL;
        }
        // Allocate enough memory for the images
        if( (lpIR = malloc( sizeof(ICONRESOURCE) + ((lpIcon->idCount-1) * sizeof(ICONIMAGE)) )) == NULL )
        {
            MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK );
            FreeLibrary( hLibrary );
            return NULL;
        }
        // Fill in local struct members
        lpIR->nNumImages = lpIcon->idCount;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -