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

📄 txtinfoshlext.cpp

📁 Windows Shell扩展编程完全指南 3
💻 CPP
字号:
// TxtInfoShlExt.cpp : Implementation of CTxtInfoShlExt

#include "stdafx.h"
#include "resource.h"
#include "TxtInfo.h"
#include "TxtInfoShlExt.h"

/////////////////////////////////////////////////////////////////////////////
// CTxtInfoShlExt

STDMETHODIMP CTxtInfoShlExt::Load ( LPCOLESTR wszFilename, DWORD dwMode )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());    // init MFC
    UNREFERENCED_PARAMETER(dwMode);

    // Let CString convert the filename to ANSI.
    m_sFilename = wszFilename;

    return S_OK;
}

STDMETHODIMP CTxtInfoShlExt::GetInfoTip ( DWORD dwFlags, LPWSTR* ppwszTip )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());    // init MFC

USES_CONVERSION;
LPMALLOC   pMalloc;
CStdioFile file;
DWORD      dwFileSize;
CString    sFirstLine;
BOOL       bReadLine;
CString    sTooltip;

    UNREFERENCED_PARAMETER(dwFlags);

    // Try to open the file.
    if ( !file.Open ( m_sFilename , CFile::modeRead | CFile::shareDenyWrite ) )
        return E_FAIL;

    // Get an IMalloc interface from the shell.
    if ( FAILED( SHGetMalloc ( &pMalloc ) ))
        return E_FAIL;

    // Get the size of the file.
    dwFileSize = file.GetLength();

    // Read in the first line from the file.
    bReadLine = file.ReadString ( sFirstLine );

    sTooltip.Format ( _T("File size: %lu"), dwFileSize );

    // If we were able to read in the first line, add it to the tooltip.
    if ( bReadLine )
        {
        sTooltip += _T("\n");
        sTooltip += sFirstLine;
        }

    // Allocate a buffer for Explorer.  Note that the must pass the string 
    // back as a Unicode string, so the string length is multiplied by the 
    // size of a Unicode character.
ULONG cbText = (1 + sTooltip.GetLength()) * sizeof(wchar_t);

    *ppwszTip = (LPWSTR) pMalloc->Alloc ( cbText );

    // Release the IMalloc interface now that we're done using it.
    pMalloc->Release();

    if ( NULL == *ppwszTip )
        return E_OUTOFMEMORY;

    // Use the Unicode string copy function to put the tooltip text in the buffer.
    wcscpy ( *ppwszTip, T2COLE(LPCTSTR(sTooltip)) );

    return S_OK;
}

⌨️ 快捷键说明

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