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

📄 page07ea.cpp

📁 This ZIP archive includes the sources (and executable) for the FileTest utility, written by Ladisla
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/* Page08Ea.cpp                           Copyright (c) Ladislav Zezula 2005 */
/*---------------------------------------------------------------------------*/
/* Description :                                                             */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 15.08.05  1.00  Lad  The first version of Page08Ea.cpp                    */
/*****************************************************************************/

#include "FileTest.h"
#include "resource.h"

#define MAX_SUBITEM_BYTES 64

//-----------------------------------------------------------------------------
// Local functions

static void UpdateDialogButtons(HWND hDlg)
{
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    HWND hButton;
    int nSelected = ListView_GetSelectedCount(hListView);
    int nItems = ListView_GetItemCount(hListView);
    int nItem = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);

    // "Insert will be always enabled
    hButton = GetDlgItem(hDlg, IDC_INSERT);
    EnableWindow(hButton, TRUE);

    // Enable/disable "Edit"
    hButton = GetDlgItem(hDlg, IDC_EDIT);
    EnableWindow(hButton, (nSelected == 1));

    // Enable/disable "Delete"
    hButton = GetDlgItem(hDlg, IDC_DELETE);
    EnableWindow(hButton, (nSelected == 1));

    // Enable/disable "Up"
    hButton = GetDlgItem(hDlg, IDC_UP);
    EnableWindow(hButton, (nItem > 0));

    // Enable/disable "Down"
    hButton = GetDlgItem(hDlg, IDC_DOWN);
    EnableWindow(hButton, (0 <= nItem && nItem < (nItems - 1)));
}

void MoveItem(HWND hDlg, BOOL bUp)
{
    LVITEM lvi1;
    LVITEM lvi2;
    TCHAR szText1[MAX_PATH + 1] = _T("");
    TCHAR szText2[MAX_PATH + 1] = _T("");
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    int nItems = ListView_GetItemCount(hListView);
    int nItem1 = 0;
    int nItem2 = 0;

    if(bUp == FALSE)
    {
        nItem1 = ListView_GetNextItem(hListView, -1, LVNI_FOCUSED);
        nItem2 = nItem1 + 1;
        if(nItem1 == nItems - 1)
            return;

    }
    else
    {
        nItem1 = ListView_GetNextItem(hListView, -1, LVNI_FOCUSED) - 1;
        nItem2 = nItem1 + 1;
        if(nItem1 < 0)
            return;
    }

    // Disable redrawing during item acrobatics
    SendMessage(hListView, WM_SETREDRAW, FALSE, 0);

    // Retrieve the both items
    ZeroMemory(&lvi1, sizeof(LVITEM));
    lvi1.mask       = LVIF_IMAGE | LVIF_STATE | LVIF_TEXT | LVIF_PARAM;
    lvi1.stateMask  = (UINT)-1;
    lvi1.iItem      = nItem1;
    lvi1.pszText    = szText1;
    lvi1.cchTextMax = _tsize(szText1);
    ListView_GetItem(hListView, &lvi1);

    ZeroMemory(&lvi2, sizeof(LVITEM));
    lvi2.mask       = LVIF_IMAGE | LVIF_STATE | LVIF_TEXT | LVIF_PARAM;
    lvi2.stateMask  = (UINT)-1;
    lvi2.iItem      = nItem2;
    lvi2.pszText    = szText2;
    lvi2.cchTextMax = _tsize(szText2);
    ListView_GetItem(hListView, &lvi2);

    // Switch the main items
    lvi2.iItem = nItem1;
    ListView_SetItem(hListView, &lvi2);
    lvi1.iItem = nItem2;
    ListView_SetItem(hListView, &lvi1);

    // Switch the texts for the subitems
    ListView_GetItemText(hListView, nItem1, 1, szText1, _tsize(szText1));
    ListView_GetItemText(hListView, nItem2, 1, szText2, _tsize(szText2));
    ListView_SetItemText(hListView, nItem1, 1, szText2);
    ListView_SetItemText(hListView, nItem2, 1, szText1);

    // Enable redrawing and redraw the window
    SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
    InvalidateRect(hListView, NULL, TRUE);
}

static int SetListViewEaEntry(HWND hListView, int nIndex, PFILE_FULL_EA_INFORMATION EaInfo, BOOL bInsertNew)
{
    LVITEM lvi;
    TCHAR szBuffer[(3 * MAX_SUBITEM_BYTES) + 1] = _T("");
    LPTSTR szSubItem = szBuffer;
    LPTSTR szItemText;
    PBYTE pbData = (PBYTE)EaInfo->EaName + EaInfo->EaNameLength + 1;
    int nBytes = EaInfo->EaValueLength;

    // -1 means insert to the end of the list
    if(nIndex == -1)
        nIndex = ListView_GetItemCount(hListView);

    szItemText = new TCHAR[EaInfo->EaNameLength + 1];
#ifdef _UNICODE
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, EaInfo->EaName, EaInfo->EaNameLength + 1, szItemText, EaInfo->EaNameLength + 1);
#else
    _tcscpy(szItemText, EaInfo->EaName);
#endif

    // Set the main item
    ZeroMemory(&lvi, sizeof(LVITEM));
    lvi.mask    = LVIF_TEXT | LVIF_PARAM;
    lvi.iItem   = nIndex;
    lvi.pszText = szItemText;
    lvi.lParam  = (LPARAM)EaInfo;

    // Insert or set the item
    if(bInsertNew)
        nIndex = ListView_InsertItem(hListView, &lvi);
    else
        ListView_SetItem(hListView, &lvi);

    // Create the text for the subitem
    if(nBytes > MAX_SUBITEM_BYTES)
        nBytes = MAX_SUBITEM_BYTES;
    for(int i = 0; i < nBytes; i++)
        szSubItem += _stprintf(szSubItem, _T("%02lX "), (*pbData++ & 0x000000FF));
    ListView_SetItemText(hListView, nIndex, 1, szBuffer);

    delete [] szItemText;
    return TRUE;
}

//-----------------------------------------------------------------------------
// Message handlers

static TListViewColumns Columns[] =
{
    {IDS_EA_NAME, 120},
    {IDS_EA_VALUE, -1},
    {0, 0}
};

static int OnInitDialog(HWND hDlg, LPARAM lParam)
{
    PROPSHEETPAGE * pPage = (PROPSHEETPAGE *)lParam;
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);

    SetWindowLongPtr(hDlg, DWLP_USER, (LONG)pPage->lParam);
    ListView_CreateColumns(hListView, Columns);
    ListView_SetExtendedListViewStyle(hListView, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
    UpdateDialogButtons(hDlg);
    return TRUE;
}

static int OnSetActive(HWND hDlg)
{
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    BOOL bEnable = FALSE;

    if(IsHandleValid(pData->hFile))
        bEnable = TRUE;
    EnableDlgItems(hDlg, bEnable, IDC_QUERY_EA, IDC_SET_EA, 0);
    UpdateDialogButtons(hDlg);
    return TRUE;
}

static int OnInsertEa(HWND hDlg)
{
    PFILE_FULL_EA_INFORMATION EaInfo = NULL;
    HWND hListView;

    // Call the dialog for creating new EA
    if(EaEditorDialog(hDlg, &EaInfo) != IDOK)
        return TRUE;

    // Insert listview
    hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    SetListViewEaEntry(hListView, -1, EaInfo, TRUE);
    UpdateDialogButtons(hDlg);
    return TRUE;
}

static int OnEditEa(HWND hDlg)
{
    PFILE_FULL_EA_INFORMATION EaInfo = NULL;
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    int nSelected = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);

    // If no item selected, do nothing
    if(nSelected == -1)
        return 0;

    // Call the dialog for editing EA
    EaInfo = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nSelected);
    if(EaEditorDialog(hDlg, &EaInfo) != IDOK)
        return TRUE;
    
    SetListViewEaEntry(hListView, nSelected, EaInfo, FALSE);
    UpdateDialogButtons(hDlg);
    return TRUE;
}

static int OnDeleteEa(HWND hDlg)
{
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    int nSelected = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);

    // If no item selected, do nothing
    if(nSelected != -1)
        ListView_DeleteItem(hListView, nSelected);
    UpdateDialogButtons(hDlg);
    return TRUE;
}

static int OnQueryEa(HWND hDlg)
{
    PFILE_FULL_EA_INFORMATION EaBufferItem;
    PFILE_FULL_EA_INFORMATION EaItemCopy;
    PFILE_FULL_EA_INFORMATION EaBuffer = NULL;
    FILE_EA_INFORMATION FileEaInfo;
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    IO_STATUS_BLOCK IoStatus = {0};
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG EaBufferSize = 0;
    ULONG EaItemSize = 0;
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);

    //
    // Although it is possible to get the size of the extended attributes,
    // I've tried and the size is usually not enough for receiving EAs.
    // I don't know what the QueryFileInfo for FileEaInformation is good for then :-(
    //

    Status = NtQueryInformationFile(pData->hFile,
                                   &IoStatus, 

⌨️ 快捷键说明

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