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

📄 dlgflags.cpp

📁 This ZIP archive includes the sources (and executable) for the FileTest utility, written by Ladisla
💻 CPP
字号:
/*****************************************************************************/
/* DlgFlags.cpp                           Copyright (c) Ladislav Zezula 2004 */
/*---------------------------------------------------------------------------*/
/* Description:                                                              */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 06.01.04  1.00  Lad  The first version of DlgFlags.cpp                    */
/*****************************************************************************/

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

//-----------------------------------------------------------------------------
// Dialog handlers

static int OnInitDialog(HWND hDlg, LPARAM lParam)
{
    TFlagDialogData * pData = (TFlagDialogData *)lParam;
    TCHAR szTitle[256] = _T("");
    DWORD dwExStyle;
    DWORD dwStyle;
    HFONT hFont;
    POINT pt;
    HWND hCheck = GetDlgItem(hDlg, IDC_CHECK1);
    HWND hWnd;
    RECT borders;                       // Borders of the dialog
    RECT rect;                          // Rectangle for the checkbox
    int  nIDCtrl = IDC_CHECK1;
    int  nLeft;
    int  nTop;
    int  nHeight;
    int  nWidth;

    // Configure the dialog
    SetWindowLongPtr(hDlg, DWLP_USER, lParam);
    SetDialogIcon(hDlg, IDI_TESTFILE);
    LoadString(g_hInst, pData->nIDTitle, szTitle, _tsize(szTitle));
    SetWindowText(hDlg, szTitle);

    // Get the settings of the "master" checkbox
    GetWindowRect(hCheck, &rect);
    hFont     = (HFONT)SendMessage(hCheck, WM_GETFONT, 0, 0);
    dwExStyle = (DWORD)GetWindowLongPtr(hCheck, GWL_EXSTYLE);
    dwStyle   = (DWORD)GetWindowLongPtr(hCheck, GWL_STYLE) & ~(WS_TABSTOP | WS_GROUP);
    ScreenRectToClientRect(hDlg, &rect);
    nLeft     = rect.left;
    nTop      = rect.top;
    nHeight   = rect.bottom - rect.top;
    nWidth    = rect.right - rect.left;

    //
    // Create all checkboxes. The first one, with IDC_COLUMN1,
    // is already pre-created in the resource editor. 
    // All next checkboxes will be created with increasing ID.
    //

    // Create all buttons
    for(int nIndex = 0; nIndex < pData->nFlags && pData->Flags[nIndex].szFlagText != NULL; nIndex++, nIDCtrl++)
    {
        int nChecked = BST_UNCHECKED;

        if(hCheck == NULL)
        {
            hCheck = CreateWindowEx(dwExStyle,
                                    WC_BUTTON,
                                    pData->Flags[nIndex].szFlagText,
                                    dwStyle | WS_VISIBLE,
                                    nLeft,
                                    nTop,
                                    nWidth,
                                    nHeight,
                                    hDlg,
                                    (HMENU)(INT_PTR)nIDCtrl,
                                    g_hInst,
                                    NULL);

            SendMessage(hCheck, WM_SETFONT, (WPARAM)hFont, FALSE);
        }
        else
            SetWindowText(hCheck, pData->Flags[nIndex].szFlagText);

        // Check/uncheck the box
        if(pData->dwFlags & pData->Flags[nIndex].dwFlag)
            nChecked = BST_CHECKED;
        Button_SetCheck(hCheck, nChecked);

        // Get the next check box
        nTop += nHeight + 6;
        hCheck = NULL;
    }

    // Change the position of the buttons
    hWnd = GetDlgItem(hDlg, IDOK);
    GetWindowRect(hWnd, &rect);
    pt.x = rect.left;
    pt.y = rect.top;
    ScreenToClient(hDlg, &pt);
    SetWindowPos(hWnd, NULL, pt.x, nTop, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    nTop += (rect.bottom - rect.top) + 8;

    // Change the size of the entire dialog
    GetWindowBorders(hDlg, &borders);
    GetClientRect(hDlg, &rect);
    rect.bottom = nTop;
    nWidth  = borders.left + rect.right + borders.right;
    nHeight = borders.top + rect.bottom + borders.bottom;
    SetWindowPos(hDlg, NULL, 0, 0, nWidth, nHeight, SWP_NOMOVE | SWP_NOZORDER);

    CenterWindow(hDlg);
    return TRUE;
}

static int OnSaveDialog(HWND hDlg)
{
    TFlagDialogData * pData = (TFlagDialogData *)GetWindowLongPtr(hDlg, DWLP_USER);
    UINT nIDCtrl = IDC_CHECK1;
    HWND hCheck = GetDlgItem(hDlg, nIDCtrl);
    int nIndex = 0;

    pData->dwFlags = 0;
    while(hCheck != NULL)
    {
        DWORD_PTR dwStyle = (DWORD_PTR)GetWindowLongPtr(hCheck, GWL_STYLE);

        // If not a checkbox, do nothing
        if((dwStyle & 0x0F) != BS_AUTOCHECKBOX)
            break;

        if(Button_GetCheck(hCheck) == BST_CHECKED)
            pData->dwFlags |= pData->Flags[nIndex].dwFlag;

        // Move to the next checkbox
        hCheck = GetDlgItem(hDlg, ++nIDCtrl);
        nIndex++;
    }
    return TRUE;
}


static INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    // Dialog initialization
    if(uMsg == WM_INITDIALOG)
        return OnInitDialog(hDlg, lParam);

    if(uMsg == WM_COMMAND)
    {
        if(HIWORD(wParam) == BN_CLICKED)
        {
            switch(LOWORD(wParam))
            {
                case IDOK:
                    OnSaveDialog(hDlg);
                    // No break here !!

                case IDCANCEL:
                    EndDialog(hDlg, LOWORD(wParam));
                    break;
            }
        }
    }

    return FALSE;
}
                                 
INT_PTR FlagsDialog(TFlagDialogData * pData)
{
    return DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_FLAGS_DIALOG), pData->hParent, DialogProc, (LPARAM)pData);
}

⌨️ 快捷键说明

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