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

📄 dialogs.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 3 页
字号:
}



/*****************************************************************************\
* MakeWindowName
*
* Builds the window name from the window handle passed in.
*
* Arguments:
*     HWND hwnd - handle to the window.
*     LPSTR lpString - String to put window name into.
*     INT nSTringLen - Length of window string.
*
* Returns:
*     VOID
\*****************************************************************************/

PRIVATE VOID
MakeWindowName(
    HWND hwnd,
    LPSTR lpString,
    INT nStringLen
    )
{
    wsprintf(lpString, "%8.8lX:", hwnd);

    if (hwnd == NULL || !IsWindow(hwnd))
    {
        lstrcat(lpString, "!!!");
    }
    else
    {
        GetWindowText(hwnd, lpString + 9, nStringLen - 9);
    }
}



/*****************************************************************************\
* FindHwndInListBox
*
* Gets the window from the list of windows in the listbox.
*
* Arguments:
*     HWND hwndList - handle to the listbox.
*     HWND hSpyWnd - handle to the spy window.
*
* Returns:
*     INT - Index to the window in the listbox. 
\*****************************************************************************/

PRIVATE INT
FindHwndInListBox(
    HWND hwndList,
    HWND hSpyWnd
    )
{
    CHAR rgBuf[9];
    INT nIndex;

    wsprintf(rgBuf, "%08lX", (LONG)hSpyWnd);

    nIndex = SendMessage(hwndList, LB_FINDSTRING, (WPARAM)-1, (LPARAM)rgBuf);

    if (nIndex == LB_ERR)
        nIndex = 0;

    return nIndex;
}



/*****************************************************************************\
* HighlightWindow
*
* Used to temporarily highlight the window that the user has selected from
* the Select Window dialog.  It does this by inverting a border around the
* window.
*
* Arguments:
*     HWND hwnd - handle to the selected window.
*     BOOL fdraw - whether to draw the window inverted on non-inverted.
*
* Returns:
*     VOID
\*****************************************************************************/

PRIVATE VOID
HighlightWindow(
    HWND hwnd,
    BOOL fDraw
    )
{
    HDC hdc;
    RECT rc;

    bBorderOn = fDraw;

    if (hwnd == NULL || !IsWindow(hwnd))
        return;

    hdc = GetWindowDC(hwnd);
    GetWindowRect(hwnd, &rc);
    OffsetRect(&rc, -rc.left, -rc.top);

    if (!IsRectEmpty(&rc))
    {
        PatBlt(hdc, rc.left, rc.top, rc.right - rc.left, DINV,  DSTINVERT);
        PatBlt(hdc, rc.left, rc.bottom - DINV, DINV,
            -(rc.bottom - rc.top - 2 * DINV), DSTINVERT);
        PatBlt(hdc, rc.right - DINV, rc.top + DINV, DINV,
            rc.bottom - rc.top - 2 * DINV, DSTINVERT);
        PatBlt(hdc, rc.right, rc.bottom - DINV, -(rc.right - rc.left),
            DINV, DSTINVERT);
    }

    ReleaseDC(hwnd, hdc);
}



/*****************************************************************************\
* SelectWindowUpdateInfo
*
* Updates the informational fields in the Select Window dialog when
* a new window is selected from the hwnd listbox.
*
* Arguments:
*     HWND hDlg - handle to the select window dialog box.
*     HWND hwnd - handle to the new window selected.
*
* Returns:
*     VOID
\*****************************************************************************/

PRIVATE VOID
SelectWindowUpdateInfo(
    HWND hDlg,
    HWND hwnd
    )
{
    HWND hParent;
    DWORD dwStyle;
    RECT rc;
    CHAR szTemp[MAXSTRING];

    if (hwnd)
    {
        hParent = GetParent(hwnd);
        dwStyle = GetWindowLong(hwnd, GWL_STYLE);

        MakeWindowName(hwnd, szTemp, MAXSTRING);
        SetDlgItemText(hDlg, DID_SELWINWINDOW, szTemp);

        GetClassName(hwnd, szTemp, MAXSTRING);
        SetDlgItemText(hDlg, DID_SELWINCLASS, szTemp);

        if (hParent)
        {
            MakeWindowName(hParent, szTemp, MAXSTRING);
            SetDlgItemText(hDlg, DID_SELWINPARENT, szTemp);
        }
        else
        {
            SetDlgItemText(hDlg, DID_SELWINPARENT,
                           LoadResourceString(IDS_NOPARENT));
        }

        GetWindowRect(hwnd, &rc);
        wsprintf(szTemp, "(%d,%d)-(%d,%d) %dx%d", rc,
            rc.right-rc.left, rc.bottom-rc.top);
        SetDlgItemText(hDlg, DID_SELWINRECT, szTemp);

        if (dwStyle & WS_POPUP)
            wsprintf (szTemp, "%08lX: WS_POPUP", dwStyle);
        else if (dwStyle & WS_CHILD)
            wsprintf (szTemp, "%08lX: WS_CHILD, ID: %lX", dwStyle,
            GetWindowLong(hwnd, GWL_ID));
        else if (dwStyle & WS_ICONIC)
            wsprintf (szTemp, "%08lX: WS_ICONIC", dwStyle);
        else
            wsprintf (szTemp, "%08lX: WS_OVERLAPPED", dwStyle);

        SetDlgItemText(hDlg, DID_SELWINSTYLE, szTemp);
    }
    else
    {
        TCHAR lpBuf[256];
        LoadString(GetModuleHandle(NULL), IDS_UNDEFINED, lpBuf, sizeof(lpBuf));
        SetDlgItemText(hDlg, DID_SELWINWINDOW, lpBuf);
        SetDlgItemText(hDlg, DID_SELWINCLASS,  lpBuf);
        SetDlgItemText(hDlg, DID_SELWINPARENT, lpBuf);
        SetDlgItemText(hDlg, DID_SELWINRECT,   lpBuf);
        SetDlgItemText(hDlg, DID_SELWINSTYLE,  lpBuf);
    }
}



/*****************************************************************************\
* SelectWindowEnableFields
*
* Enables/disables the different fields in the Select Window dialog
* based on whether the user wants to spy on all windows or individually
* select one.
*
* Arguments:
*   HWND hwnd    - Dialog window handle.
*   BOOL fEnable - TRUE to enable the fields, FALSE to disable them.
*
* Returns:
*   VOID
\*****************************************************************************/

PRIVATE VOID
SelectWindowEnableFields(
    HWND hwnd,
    BOOL fEnable
    )
{
    EnableWindow(GetDlgItem(hwnd, DID_SELWINLIST), fEnable);
    EnableWindow(GetDlgItem(hwnd, DID_SELWINWINDOW), fEnable);
    EnableWindow(GetDlgItem(hwnd, DID_SELWINCLASS), fEnable);
    EnableWindow(GetDlgItem(hwnd, DID_SELWINPARENT), fEnable);
    EnableWindow(GetDlgItem(hwnd, DID_SELWINRECT), fEnable);
    EnableWindow(GetDlgItem(hwnd, DID_SELWINSTYLE), fEnable);
}



/*****************************************************************************\
* OutputDlgProc
*
* Dialog proc for the Output dialog.
*
* Arguments:
*   HWND hwnd - handle to the output dialog
*   UINT msg - message sent to output dialog
*   WPARAM wParam - message parameter.
*   LPARAM lParam - message parameter.
*
* Returns:
*   The value that the dialog proc should return, based on the processing
*   of the specific WM_COMMAND message received.
*
\*****************************************************************************/

BOOL CALLBACK
OutputDlgProc(
    HWND hwnd,
    UINT msg,
    WPARAM wParam,
    LPARAM lParam
    )
{
    switch (msg)
    {
        case WM_INITDIALOG:
            CheckDlgButton(hwnd, DID_OUTPUTWINDOW, gfOutputWin);
            CheckDlgButton(hwnd, DID_OUTPUTCOM1, gfOutputCom1);
            CheckDlgButton(hwnd, DID_OUTPUTFILE, gfOutputFile);

            SetDlgItemText(hwnd, DID_OUTPUTFILENAME, gszFile);
            SetDlgItemInt(hwnd, DID_OUTPUTLINES, gnLines, FALSE);

            return TRUE;

        case WM_COMMAND:
            return OutputCommand(hwnd, LOWORD(wParam), HIWORD(wParam));
    }

    return FALSE;
}



/*****************************************************************************\
* OutputCommand
*
* Handles the WM_COMMAND messages for the Output dialog.
*
* Arguments:
*   HWND hwnd       - Window handle of the dialog.
*   INT nCmd        - Command value.
*   INT nNotifyCode - The notify code.
*
* Returns:
*   The value that the dialog proc should return, based on the processing
*   of the specific WM_COMMAND message received.
*
\*****************************************************************************/

PRIVATE BOOL
OutputCommand(
    HWND hwnd,
    INT nCmd,
    INT nNotifyCode
    )
{
    HFILE fh;
    INT i;
    CHAR szTemp[MAXSTRING];

    switch (nCmd)
    {
        case IDOK:
            i = GetDlgItemInt(hwnd, DID_OUTPUTLINES, &i, FALSE);
            if (i != gnLines)
            {
                if ( i > 0 && i <= LINES_MAX)
                {
                    gnLines = i;
                    MyCreatePrintfWin(ghwndSpyApp);
                }
                else
                {
                    Message(MB_OK | MB_ICONEXCLAMATION,
                        LoadResourceString(IDS_ERROR_WND_LINE), LINES_MAX);
                    SetFocus(GetDlgItem(hwnd, DID_OUTPUTLINES));
                    break;
                }
            }

            gfOutputWin = IsDlgButtonChecked(hwnd, DID_OUTPUTWINDOW);
            gfOutputCom1 = IsDlgButtonChecked(hwnd, DID_OUTPUTCOM1);
            gfOutputFile = IsDlgButtonChecked(hwnd, DID_OUTPUTFILE);

            if (gfOutputFile)
            {
                GetDlgItemText(hwnd, DID_OUTPUTFILENAME, szTemp, MAXSTRING);
                /*
                 * If they changed the filename, or the file is not open
                 * then open it.
                 */
                if (lstrcmp(gszFile, szTemp) != 0 || gfhFile == 0)
                {
                    fh = _lcreat(szTemp, 0);
                    if (fh == (HFILE)(-1))
                    {
                        if (Message(MB_OKCANCEL | MB_ICONEXCLAMATION,
                            LoadResourceString(IDS_ERROR_CANT_OPEN_FILE), szTemp) == IDCANCEL)
                        {
                            EndDialog(hwnd, FALSE);
                        }

                        return TRUE;
                    }

                    lstrcpy(gszFile, szTemp);
                    if (gfhFile)
                        _lclose(gfhFile);
                    gfhFile = fh;
                }
            }
            else
            {
                if (gfhFile)
                {
                    _lclose(gfhFile);
                    gfhFile = 0;
                }
            }

            if (gfOutputCom1)
            {
                if (gfhCom1 != INVALID_HANDLE_VALUE)
                    CloseHandle(gfhCom1);

                gfhCom1 = CreateFile(
                        "com1",
                        GENERIC_WRITE,
                        0,                    // exclusive access
                        NULL,                 // no security attrs
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
                if (gfhCom1 == INVALID_HANDLE_VALUE)
                {
                    if (Message(MB_OKCANCEL | MB_ICONEXCLAMATION,
                        LoadResourceString(IDS_ERROR_CANT_OPEN_COM1)) == IDCANCEL)
                    {
                        EndDialog(hwnd, FALSE);
                    }

                    return TRUE;
                }
            }
            else
            {
                if (gfhCom1 != INVALID_HANDLE_VALUE)
                {
                    CloseHandle(gfhCom1);
                }
            }

            EndDialog(hwnd, TRUE);
            return TRUE;

        case IDCANCEL:
            EndDialog(hwnd, FALSE);
            return TRUE;
    }

    return FALSE;
}



/*****************************************************************************\
* SelectFont
*
* Allows the user to select a new font for the display.
*
* Arguments:
*    none
*
* Returns:
*    VOID
\*****************************************************************************/

VOID
SelectFont(
    VOID
    )
{
    CHOOSEFONT cf;
    LOGFONT lf;
    HFONT hfontNew;

    GetObject(ghfontPrintf, sizeof(LOGFONT), (LPVOID)&lf);

    cf.lStructSize = sizeof(cf);
    cf.hwndOwner = ghwndSpyApp;
    cf.hDC = NULL;
    cf.lpLogFont = &lf;
    cf.iPointSize = 0;
    cf.Flags = CF_ANSIONLY | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT
        | CF_SCREENFONTS;
    cf.rgbColors = 0;
    cf.lCustData = 0;
    cf.lpfnHook = NULL;
    cf.lpTemplateName = NULL;
    cf.hInstance = NULL;
    cf.lpszStyle = NULL;
    cf.nFontType = 0;
    cf.nSizeMin = 0;
    cf.nSizeMax = 0;

    if (ChooseFont(&cf))
    {
        if (hfontNew = CreateFontIndirect(&lf))
        {
            SetPrintfFont(ghwndPrintf, hfontNew);
            ghfontPrintf = hfontNew;
        }
    }
}



/*****************************************************************************\
* MessagesDlgProc
*
* Dialog proc for the Messages dialog.  This dialog allows the user
* to select which messages they want to spy on.
*
* Arguments:
*    HWND hwnd - handle to the dialog window.
*    UINT msg - message to the window
*    WPARAM wParam - message parameter
*    LPARAM lParam - message parameter
*
* Returns:

⌨️ 快捷键说明

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