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

📄 explorer.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
字号:
/*
 * ReactOS Explorer
 *
 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <precomp.h>

HINSTANCE hExplorerInstance;
HANDLE hProcessHeap;
HKEY hkExplorer = NULL;

LONG
SetWindowStyle(IN HWND hWnd,
               IN LONG dwStyleMask,
               IN LONG dwStyle)
{
    LONG PrevStyle, Style;

    ASSERT((~dwStyleMask & dwStyle) == 0);

    PrevStyle = GetWindowLong(hWnd,
                              GWL_STYLE);
    if (PrevStyle != 0 &&
        (PrevStyle & dwStyleMask) != dwStyle)
    {
        Style = PrevStyle & ~dwStyleMask;
        Style |= dwStyle;

        PrevStyle = SetWindowLong(hWnd,
                                  GWL_STYLE,
                                  Style);
    }

    return PrevStyle;
}

LONG
SetWindowExStyle(IN HWND hWnd,
                 IN LONG dwStyleMask,
                 IN LONG dwStyle)
{
    LONG PrevStyle, Style;

    ASSERT((~dwStyleMask & dwStyle) == 0);

    PrevStyle = GetWindowLong(hWnd,
                              GWL_EXSTYLE);
    if (PrevStyle != 0 &&
        (PrevStyle & dwStyleMask) != dwStyle)
    {
        Style = PrevStyle & ~dwStyleMask;
        Style |= dwStyle;

        PrevStyle = SetWindowLong(hWnd,
                                  GWL_EXSTYLE,
                                  Style);
    }

    return PrevStyle;
}

HMENU
LoadPopupMenu(IN HINSTANCE hInstance,
              IN LPCTSTR lpMenuName)
{
    HMENU hMenu, hSubMenu = NULL;

    hMenu = LoadMenu(hInstance,
                     lpMenuName);

    if (hMenu != NULL)
    {
        hSubMenu = GetSubMenu(hMenu,
                              0);
        if (hSubMenu != NULL &&
            !RemoveMenu(hMenu,
                        0,
                        MF_BYPOSITION))
        {
            hSubMenu = NULL;
        }

        DestroyMenu(hMenu);
    }

    return hSubMenu;
}

HMENU
FindSubMenu(IN HMENU hMenu,
            IN UINT uItem,
            IN BOOL fByPosition)
{
    MENUITEMINFO mii;

    mii.cbSize = sizeof(mii);
    mii.fMask = MIIM_SUBMENU;

    if (GetMenuItemInfo(hMenu,
                        uItem,
                        fByPosition,
                        &mii))
    {
        return mii.hSubMenu;
    }

    return NULL;
}

BOOL
GetCurrentLoggedOnUserName(OUT LPTSTR szBuffer,
                           IN DWORD dwBufferSize)
{
    DWORD dwType;
    DWORD dwSize;

    /* Query the user name from the registry */
    dwSize = (dwBufferSize * sizeof(TCHAR)) - 1;
    if (RegQueryValueEx(hkExplorer,
                        TEXT("Logon User Name"),
                        0,
                        &dwType,
                        (LPBYTE)szBuffer,
                        &dwSize) == ERROR_SUCCESS &&
        (dwSize / sizeof(TCHAR)) > 1 &&
        szBuffer[0] != _T('\0'))
    {
        szBuffer[dwSize / sizeof(TCHAR)] = _T('\0');
        return TRUE;
    }

    /* Fall back to GetUserName() */
    dwSize = dwBufferSize;
    if (!GetUserName(szBuffer,
                     &dwSize))
    {
        szBuffer[0] = _T('\0');
        return FALSE;
    }

    return TRUE;
}

BOOL
FormatMenuString(IN HMENU hMenu,
                 IN UINT uPosition,
                 IN UINT uFlags,
                 ...)
{
    va_list vl;
    MENUITEMINFO mii;
    TCHAR szBuf[128];
    TCHAR szBufFmt[128];

    /* Find the menu item and read the formatting string */
    mii.cbSize = sizeof(mii);
    mii.fMask = MIIM_STRING;
    mii.dwTypeData = (LPTSTR)szBufFmt;
    mii.cch = sizeof(szBufFmt) / sizeof(szBufFmt[0]);
    if (GetMenuItemInfo(hMenu,
                        uPosition,
                        uFlags,
                        &mii))
    {
        /* Format the string */
        va_start(vl, uFlags);
        _vsntprintf(szBuf,
                    (sizeof(szBuf) / sizeof(szBuf[0])) - 1,
                    szBufFmt,
                    vl);
        va_end(vl);
        szBuf[(sizeof(szBuf) / sizeof(szBuf[0])) - 1] = _T('\0');

        /* Update the menu item */
        mii.dwTypeData = (LPTSTR)szBuf;
        if (SetMenuItemInfo(hMenu,
                            uPosition,
                            uFlags,
                            &mii))
        {
            return TRUE;
        }
    }

    return FALSE;
}

BOOL
GetExplorerRegValueSet(IN HKEY hKey,
                       IN LPCTSTR lpSubKey,
                       IN LPCTSTR lpValue)
{
    TCHAR szBuffer[MAX_PATH];
    HKEY hkSubKey;
    DWORD dwType, dwSize;
    BOOL Ret = FALSE;

    _tcscpy(szBuffer,
            TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"));
    _tcscat(szBuffer,
            _T("\\"));
    _tcscat(szBuffer,
            lpSubKey);

    dwSize = sizeof(szBuffer);
    if (RegOpenKeyEx(hKey,
                     szBuffer,
                     0,
                     KEY_QUERY_VALUE,
                     &hkSubKey) == ERROR_SUCCESS)
    {
        ZeroMemory(szBuffer,
                   sizeof(szBuffer));

        if (RegQueryValueEx(hkSubKey,
                            lpValue,
                            0,
                            &dwType,
                            (LPBYTE)szBuffer,
                            &dwSize) == ERROR_SUCCESS)
        {
            if (dwType == REG_DWORD && dwSize == sizeof(DWORD))
                Ret = *((PDWORD)szBuffer) != 0;
            else if (dwSize > 0)
                Ret = *((PUCHAR)szBuffer) != 0;
        }

        RegCloseKey(hkSubKey);
    }
    return Ret;
}


#if 1
/* FIXME: Should be implemented in shell32 */
BOOL WINAPI IsUserAnAdmin(VOID)
{
    HRESULT WINAPI IsUserAdmin(void);
    return (BOOL)IsUserAdmin();
}
#endif

static BOOL
SetShellReadyEvent(IN LPCTSTR lpEventName)
{
    HANDLE hEvent;

    hEvent = OpenEvent(EVENT_MODIFY_STATE,
                       FALSE,
                       lpEventName);
    if (hEvent != NULL)
    {
        SetEvent(hEvent);

        CloseHandle(hEvent);
        return TRUE;
    }

    return FALSE;
}

INT WINAPI
_tWinMain(IN HINSTANCE hInstance,
          IN HINSTANCE hPrevInstance,
          IN LPTSTR lpCmdLine,
          IN INT nCmdShow)
{
    ITrayWindow *Tray = NULL;
    HANDLE hShellDesktop = NULL;
    BOOL CreateShellDesktop = FALSE;

    if (RegOpenKey(HKEY_CURRENT_USER,
                   TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"),
                   &hkExplorer) != ERROR_SUCCESS)
    {
        /* FIXME - display error */
        return 1;
    }

    hExplorerInstance = hInstance;
    hProcessHeap = GetProcessHeap();

    InitCommonControls();
    OleInitialize(NULL);

    if (GetShellWindow() == NULL)
        CreateShellDesktop = TRUE;

    /* FIXME - initialize SSO Thread */

    if (CreateShellDesktop)
    {
        if (RegisterTrayWindowClass() && RegisterTaskSwitchWndClass())
        {
            Tray = CreateTrayWindow();

            if (Tray != NULL)
                hShellDesktop = DesktopCreateWindow(Tray);
        }

        /* WinXP: Notify msgina to hide the welcome screen */
        if (!SetShellReadyEvent(TEXT("msgina: ShellReadyEvent")))
            SetShellReadyEvent(TEXT("Global\\msgina: ShellReadyEvent"));
    }
    else
    {
        /* A shell is already loaded. Parse the command line arguments
           and unless we need to do something specific simply display
           the desktop in a separate explorer window */
        /* FIXME */
    }

    if (Tray != NULL)
        TrayMessageLoop(Tray);

    if (hShellDesktop != NULL)
        DesktopDestroyShellWindow(hShellDesktop);

    /* FIXME - shutdown SSO Thread */

    OleUninitialize();

    RegCloseKey(hkExplorer);
    hkExplorer = NULL;

    return 0;
}

⌨️ 快捷键说明

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