sdugeneral.c

来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 990 行 · 第 1/2 页

C
990
字号
                       );
            }
        }
}


// =========================================================================
void SDUTextSetItalic(
    HWND hWnd,
    int ctrlID,
    BOOL UseItalics
)
{
    HFONT hFont;
    LOGFONT lFont;

    // Set welcome title to bold
    hFont = (HFONT)SendMessage(
                GetDlgItem(hWnd, ctrlID),
                WM_GETFONT,
                0,
                0
               );
    if (hFont != NULL)
        {
        if (GetObject(hFont, sizeof(lFont), &lFont) != 0)
            {
            lFont.lfItalic = UseItalics;
            hFont = CreateFontIndirect(&lFont);
            SendMessage(
                        GetDlgItem(hWnd, ctrlID),
                        WM_SETFONT,
                        (WPARAM)hFont,
                        TRUE
                       );
            }
        }
}


// =========================================================================
void SDUTextSetUnderline(
    HWND hWnd,
    int ctrlID,
    BOOL UseUnderline
)
{
    HFONT hFont;
    LOGFONT lFont;

    // Set welcome title to bold
    hFont = (HFONT)SendMessage(
                GetDlgItem(hWnd, ctrlID),
                WM_GETFONT,
                0,
                0
               );
    if (hFont != NULL)
        {
        if (GetObject(hFont, sizeof(lFont), &lFont) != 0)
            {
            lFont.lfUnderline = UseUnderline;
            hFont = CreateFontIndirect(&lFont);
            SendMessage(
                        GetDlgItem(hWnd, ctrlID),
                        WM_SETFONT,
                        (WPARAM)hFont,
                        TRUE
                       );
            }
        }
}


// =========================================================================
// Draw text as per DrawText(...), but truncate the string displayed and
// append "..." onto it if it will not fit into the boundrys supplied
// This is implemented for systems where DT_WORD_ELLIPSIS and DT_PATH_ELLIPSIS
// are not supported (e.g. WinCE)
// Returns: The height of the text displayed
#define SDU_ELLIPSES  TEXT("...")
int SDUDrawTextEllipses(
    HDC hdc,
    WCHAR* Text,
    int TextLength,
    LPRECT TextRect,
    UINT uFormat
)
{
    int retval;
    SIZE textSize;
    int bufferSize;
    WCHAR* buffer;
    int useTextLength;

    retval = 0;

    // +1 for NULL terminator
    bufferSize = (wcslen(Text) + wcslen(SDU_ELLIPSES) + 1) * sizeof(*buffer);
    buffer = (WCHAR*)malloc(bufferSize);
    if (buffer != NULL)
        {
        useTextLength = TextLength;
        memset(buffer, 0, bufferSize);
        wcsncpy(
                  buffer, 
                  Text,
                  useTextLength
                 );
/*
        wcsncpy_s(
                  buffer, 
                  (bufferSize / sizeof(*buffer)),
                  Text,
                  useTextLength
                 );
*/

        GetTextExtentPoint32(
                             hdc, 
                             buffer,
                             wcslen(buffer),
                             &textSize
                            );
        if (textSize.cx > (TextRect->right - TextRect->left))
            {
            wcscat(buffer, SDU_ELLIPSES);
//            wcscat_s(buffer, (bufferSize / sizeof(WORD)), SDU_ELLIPSES);
            GetTextExtentPoint32(
                                 hdc, 
                                 buffer,
                                 wcslen(buffer),
                                 &textSize
                                );
            }

        while (
               (textSize.cx > (TextRect->right - TextRect->left)) &&
               (useTextLength > 0)
              )
            {
            useTextLength--;
            memset(buffer, 0, bufferSize);
            wcsncpy(
                      buffer, 
                      Text, 
                      useTextLength
                     );
            wcscat(buffer, SDU_ELLIPSES);
/*
            wcsncpy_s(
                      buffer, 
                      (bufferSize / sizeof(WORD)), 
                      Text, 
                      useTextLength
                     );
            wcscat_s(buffer, (bufferSize / sizeof(WORD)), SDU_ELLIPSES);
*/
            GetTextExtentPoint32(
                                 hdc, 
                                 buffer,
                                 wcslen(buffer),
                                 &textSize
                                );
            }

        retval = DrawText(
                          hdc,
                          buffer,
                          wcslen(buffer),
                          TextRect,
                          uFormat
                         );

        free(buffer);
        }

    return retval;
}


// =========================================================================
// Set listview style; one of:
//   LVS_ICON      - Large icons with text underneath them. Bit like Windows
//                   desktop icons in layout
//   LVS_REPORT    - Grid with header. Subitems displayed as separate columns.
//                   This is the only one which can do full row select
//   LVS_SMALLICON - Just a list, no header. Only first item (no subitems)
//                   displayed
//   LVS_LIST      - Just a list, no header. Only first item (no subitems)
//                   displayed
// To get the listview to display a report view with large icons, supply
// normal sized (i.e. 32x32) icons as the listview's "small icons" imagelist
void SDUSetListViewType(HWND hWnd, DWORD newStyle)
{
    DWORD style;

    style = GetWindowLong(hWnd, GWL_STYLE);

    if((style & LVS_TYPEMASK) != newStyle)
        {
        SetWindowLong(
                      hWnd, 
                      GWL_STYLE, 
                      ((style & ~LVS_TYPEMASK) | newStyle)
                     );
        } 

}


// =========================================================================
// SetNotUnset - Set to TRUE to set the style specified, FALSE to remove it
void SDUSetWndStyle(HWND hWnd, BOOL SetNotUnset, DWORD Style)
{
    DWORD wndStyle;

    wndStyle = GetWindowLong(hWnd, GWL_STYLE);

    if (SetNotUnset)
        {
        wndStyle |= Style;
        }
    else
        {
        wndStyle &= ~Style;
        }

    SetWindowLong(
                  hWnd, 
                  GWL_STYLE, 
                  wndStyle
                 );
}


// =========================================================================
// The one MS missed out... Get the length of a listview items's text in
// characters
int SDUListView_GetItemTextLength(HWND hWnd, int item, int subItem)
{
    LVITEM listItem;
    WCHAR* buffer;
    int testCharCount;
    int v;

    memset(&listItem, 0, sizeof(listItem));
    listItem.iItem = item;
    listItem.iSubItem = subItem;
    listItem.mask = LVIF_TEXT;

    testCharCount = 5;
    buffer = (WCHAR*)malloc((testCharCount + 1) * sizeof(*buffer));
    listItem.pszText = buffer;
    listItem.cchTextMax = testCharCount;
    v = SendMessage(hWnd, LVM_GETITEMTEXT, item, (LPARAM)&listItem);

    testCharCount = 1;
    while (v >= (testCharCount - 1))
        {
        testCharCount += 5;
        buffer = (WCHAR*)realloc(buffer, (testCharCount + 1) * sizeof(*buffer));
        listItem.pszText = buffer;
        listItem.cchTextMax = testCharCount;
        v = SendMessage(hWnd, LVM_GETITEMTEXT, item, (LPARAM)&listItem);
        }

    if (buffer != NULL)
        {
        free(buffer);
        }

    return v; // listItem.cchTextMax;
}


// =========================================================================
// Tokenize a command line and it's parameters
// Analagous to strtok(...) in it's operation
WCHAR* _SDUParamTok(WCHAR* cmdLine)
{
    static WCHAR* ptr;
    WCHAR* start;
    WCHAR* end;
    WCHAR* tmp;
    WCHAR endOfParamChar;

    if (cmdLine != NULL)
        {
        ptr = cmdLine;
        }

    if (ptr == NULL)
        {
        return NULL;
        }

    start = NULL;
    tmp = ptr;
    while (*tmp != 0)
        {
        if (*tmp != ' ')
            {
            start = tmp;
            break;
            }

        tmp++;
        }

    if (start == NULL)
        {
        ptr = NULL;
        return NULL;
        }

    endOfParamChar = ' ';
    if (*start == '"')
        {
        start = &(start[1]);
        endOfParamChar = '"';    
        }

    end = start;
    tmp = start;
    while (
           (*end != 0) &&
           (*end != endOfParamChar)
           )
        {
        end++;
        }

    if (*end == 0)
        {
        ptr = NULL;
        }
    else
        {
        ptr = &(end[1]);
        }

    if (
        (start == end) &&
        (endOfParamChar != '"')
       )
        {
        ptr = NULL;
        start = NULL;
        }

    *end = 0;

    return start;
}


// =========================================================================
// Return the value of the command line parameter indicated by the specified
// index (zero based index)
// IMPORTANT: It is the *callers* responsibility to free off the value returned
// Returns NULL on failure
WCHAR* SDUCommandLineParameter_Idx(const int parameterIdx)
{
    WCHAR* origCmdLine;
    WCHAR* useCmdLine;
    WCHAR* currParam;
    int currParamIdx;
    WCHAR* retval;

    retval = NULL;

    origCmdLine = GetCommandLine();
    // +1 to include NULL terminator
    useCmdLine = calloc((wcslen(origCmdLine) + 1), sizeof(*useCmdLine));
    if (useCmdLine != NULL)
        {
        wcscpy(useCmdLine, origCmdLine);

        currParamIdx = 0;
        currParam = _SDUParamTok(useCmdLine);
        while (
               (currParamIdx < parameterIdx) &&
               (currParam != NULL)
              )
            {
            currParamIdx++;
            currParam = _SDUParamTok(NULL);
            }

        if (currParam != NULL)
            {
            retval = calloc((wcslen(currParam) + 1), sizeof(*retval));
            if (retval != NULL)
                {
                wcscpy(retval, currParam);
                }
            }

        free(useCmdLine);
        }

    return retval;
}

// =========================================================================
// Returns TRUE if the specified command line switch could be found, otherwise
// FALSE
BOOL SDUCommandLineSwitch(const WCHAR* parameter)
{
    BOOL retval;

    retval = FALSE;

    if (SDUCommandLineSwitchNumber(parameter) >= 0)
        {
        retval = TRUE;
        }

    return retval;
}



// =========================================================================
// Return the value of the specified command line parameter "-<parameter>
// value"
// IMPORTANT: It is the *callers* responsibility to free off the value returned
// Returns NULL on failure
WCHAR* SDUCommandLineParameter_Name(const WCHAR* parameter)
{
    WCHAR* retval;
    int switchParamIdx;

    retval = NULL;

    switchParamIdx = SDUCommandLineSwitchNumber(parameter);
    if (switchParamIdx >= 0)
        {
        retval = SDUCommandLineParameter_Idx(switchParamIdx+1);
        }

    return retval;
}


// =========================================================================
// Returns the parameter number in the command line of the specified parameter.
// Returns -1 on failure
int SDUCommandLineSwitchNumber(const WCHAR* parameter)
{
    int retval;
    WCHAR* currParam;
    int currParamIdx;

    retval = -1;

    currParamIdx = 0;
    currParam = SDUCommandLineParameter_Idx(currParamIdx);
    while (currParam != NULL)
        {
        // >2 to allow 
        if (wcslen(currParam) > 1)
            {
            if (
                (currParam[0] == '/') ||
                (currParam[0] == '-')
               )
                {
                if (wcscmp(parameter, &(currParam[1])) == 0)
                    {
                    retval = currParamIdx;
                    break;
                    }
                }
            }

        free(currParam);                    
        currParamIdx++;
        currParam = SDUCommandLineParameter_Idx(currParamIdx);
        }

    if (currParam != NULL)
        {
        free(currParam);
        }

    return retval;
}


// =========================================================================
// =========================================================================

⌨️ 快捷键说明

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