Share via


轉換字串

[與此頁面 MCI相關聯的功能是舊版功能。 它已被 MediaPlayer 取代MediaPlayer已針對Windows 10和Windows 11進行優化。 Microsoft 強烈建議新程式碼盡可能使用 MediaPlayer 而非 MCI。 Microsoft 建議盡可能重寫使用舊版 API 的現有程式碼,以使用新的 API。]

當您使用 mciSendString 函式時,使用 命令傳遞的所有值和所有傳回值都是文字字串,因此您的應用程式需要轉換常式,才能從變數轉譯為字串或再次轉換。 下列範例會擷取來源矩形,並將傳回的字串轉換成矩形座標。

BOOL GetSourceRect(LPTSTR lpstrAlias, LPRECT lprc) 
{ 
    TCHAR achRetBuff[128]; 
    TCHAR achCommandBuff[128]; 

    int result;
    MCIERROR err;
 
    // Build the command string. 
    result = _stprintf_s(
        achCommandBuff, 
        TEXT("where %s source"), 
        lpstrAlias); 

    if (result == -1)
    {
        return FALSE;
    }
    
    // Clear the RECT.
    SetRectEmpty(lprc);
 
    // Send the MCI command. 
    err = mciSendString(
        achCommandBuff, 
        achRetBuff, 
        sizeof(achRetBuff), 
        NULL);

    if (err != 0)
    {
        return FALSE;
    }
        
    // The rectangle is returned as "x y dx dy". 
    // Translate the string into the RECT structure. 

    // Warning: This example omits error checking
    // for the _tcstok_s and _tstoi functions.
    TCHAR *p; 
    TCHAR *context;

    // Left.
    p = _tcstok_s(achRetBuff, TEXT(" "), &context);
    lprc->left = _tstoi(p);

    // Top.
    p = _tcstok_s(NULL, TEXT(" "), &context);
    lprc->top = _tstoi(p);

    // Right.
    p = _tcstok_s(NULL, TEXT(" "), &context);
    lprc->right = _tstoi(p);
    
    // Bottom.
    p = _tcstok_s(NULL, TEXT(" "), &context);
    lprc->bottom = _tstoi(p);

    return TRUE;
}
 

注意

RECT 結構在 MCI 中處理的方式與 Windows 的其他部分不同;在 MCI 中, 成員包含矩形的寬度, 而底部 成員則包含其高度。 在字串介面中,矩形會指定為 X1Y1X2Y2。 座標 X1Y1 會指定矩形的左上角,而 座標 X2Y2 則指定寬度和高度。