Win32 API 日本語リファレンス
ホームUI.Shell › PathCompactPathW

PathCompactPathW

関数
指定のピクセル幅に収まるよう、パス文字列を省略記号で短縮する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// SHLWAPI.dll  (Unicode / -W)
#include <windows.h>

BOOL PathCompactPathW(
    HDC hDC,   // optional
    LPWSTR pszPath,
    DWORD dx
);

パラメーター

名前方向説明
hDCHDCinoptionalフォントメトリックスに使用するデバイスコンテキストへのハンドル。この値は NULL でもかまいません。
pszPathLPWSTRinout変更対象のパスを格納した、長さ MAX_PATH の null 終端文字列へのポインター。戻り時には、このバッファーに変更後の文字列が格納されます。
dxDWORDin文字列が収まるべき幅(ピクセル単位)。

戻り値の型: BOOL

公式ドキュメント

パスのコンポーネントを省略記号(...)に置き換えることで、ファイルパスを指定されたピクセル幅に収まるように切り詰めます。(Unicode)

戻り値

型: BOOL

パスが指定された幅に正常に圧縮された場合は TRUE を返します。失敗した場合、またはパスのベース部分が指定された幅に収まらない場合は FALSE を返します。

解説(Remarks)

この関数は、hDC で現在選択されているフォントを使用してテキストの幅を計算します。この関数は、省略記号(...)を前に付けたベースとなるファイル名より先にはパスを圧縮しません。

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

HDC hdc;  /* display DC handle to current font metrics */ 

void main( void )
{
// String path name 1.
char buffer_1[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr1;
lpStr1 = buffer_1;

// String path name 2.
char buffer_2[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr2;
lpStr2 = buffer_2;

// String path name 3.
char buffer_3[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr3;
lpStr3 = buffer_3;

// String path name 4.
char buffer_4[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr4;
lpStr4 = buffer_4;

// Variable to get the return from "PathCompactPath".
int retval;

cout << "The un-truncated path is                " << lpStr1 << endl;

retval = PathCompactPath(hdc,lpStr1,125);
cout << "The truncated path at 125 pixels is :   " << lpStr1 << endl;

retval = PathCompactPath(hdc,lpStr2,120);
cout << "The truncated path at 120 pixels is :   " << lpStr2 << endl;

retval = PathCompactPath(hdc,lpStr3,110);
cout << "The truncated path at 110 pixels is :   " << lpStr3 << endl;

retval = PathCompactPath(hdc,lpStr4,25);
cout << "The truncated path at  25 pixels is :   " << lpStr4 << endl;
}

OUTPUT:
===========
The un-truncated path is                C:\path1\path2\sample.txt
The truncated path at 125 pixels is :   C:\path1\...\sample.txt
The truncated path at 120 pixels is :   C:\pat...\sample.txt
The truncated path at 110 pixels is :   C:\p...\sample.txt
The truncated path at  25 pixels is :   ...\sample.txt
メモ

shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして PathCompactPath を定義しています。エンコーディング中立のエイリアスと、エンコーディング中立でないコードを混在して使用すると、コンパイルエラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHLWAPI.dll  (Unicode / -W)
#include <windows.h>

BOOL PathCompactPathW(
    HDC hDC,   // optional
    LPWSTR pszPath,
    DWORD dx
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool PathCompactPathW(
    IntPtr hDC,   // HDC optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath,   // LPWSTR in/out
    uint dx   // DWORD
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PathCompactPathW(
    hDC As IntPtr,   ' HDC optional
    <MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder,   ' LPWSTR in/out
    dx As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hDC : HDC optional
' pszPath : LPWSTR in/out
' dx : DWORD
Declare PtrSafe Function PathCompactPathW Lib "shlwapi" ( _
    ByVal hDC As LongPtr, _
    ByVal pszPath As LongPtr, _
    ByVal dx As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PathCompactPathW = ctypes.windll.shlwapi.PathCompactPathW
PathCompactPathW.restype = wintypes.BOOL
PathCompactPathW.argtypes = [
    wintypes.HANDLE,  # hDC : HDC optional
    wintypes.LPWSTR,  # pszPath : LPWSTR in/out
    wintypes.DWORD,  # dx : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
PathCompactPathW = Fiddle::Function.new(
  lib['PathCompactPathW'],
  [
    Fiddle::TYPE_VOIDP,  # hDC : HDC optional
    Fiddle::TYPE_VOIDP,  # pszPath : LPWSTR in/out
    -Fiddle::TYPE_INT,  # dx : DWORD
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shlwapi")]
extern "system" {
    fn PathCompactPathW(
        hDC: *mut core::ffi::c_void,  // HDC optional
        pszPath: *mut u16,  // LPWSTR in/out
        dx: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern bool PathCompactPathW(IntPtr hDC, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, uint dx);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_PathCompactPathW' -Namespace Win32 -PassThru
# $api::PathCompactPathW(hDC, pszPath, dx)
#uselib "SHLWAPI.dll"
#func global PathCompactPathW "PathCompactPathW" wptr, wptr, wptr
; PathCompactPathW hDC, varptr(pszPath), dx   ; 戻り値は stat
; hDC : HDC optional -> "wptr"
; pszPath : LPWSTR in/out -> "wptr"
; dx : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global PathCompactPathW "PathCompactPathW" sptr, var, int
; res = PathCompactPathW(hDC, pszPath, dx)
; hDC : HDC optional -> "sptr"
; pszPath : LPWSTR in/out -> "var"
; dx : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL PathCompactPathW(HDC hDC, LPWSTR pszPath, DWORD dx)
#uselib "SHLWAPI.dll"
#cfunc global PathCompactPathW "PathCompactPathW" intptr, var, int
; res = PathCompactPathW(hDC, pszPath, dx)
; hDC : HDC optional -> "intptr"
; pszPath : LPWSTR in/out -> "var"
; dx : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procPathCompactPathW = shlwapi.NewProc("PathCompactPathW")
)

// hDC (HDC optional), pszPath (LPWSTR in/out), dx (DWORD)
r1, _, err := procPathCompactPathW.Call(
	uintptr(hDC),
	uintptr(pszPath),
	uintptr(dx),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function PathCompactPathW(
  hDC: THandle;   // HDC optional
  pszPath: PWideChar;   // LPWSTR in/out
  dx: DWORD   // DWORD
): BOOL; stdcall;
  external 'SHLWAPI.dll' name 'PathCompactPathW';
result := DllCall("SHLWAPI\PathCompactPathW"
    , "Ptr", hDC   ; HDC optional
    , "Ptr", pszPath   ; LPWSTR in/out
    , "UInt", dx   ; DWORD
    , "Int")   ; return: BOOL
●PathCompactPathW(hDC, pszPath, dx) = DLL("SHLWAPI.dll", "bool PathCompactPathW(void*, char*, dword)")
# 呼び出し: PathCompactPathW(hDC, pszPath, dx)
# hDC : HDC optional -> "void*"
# pszPath : LPWSTR in/out -> "char*"
# dx : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "shlwapi" fn PathCompactPathW(
    hDC: ?*anyopaque, // HDC optional
    pszPath: [*c]u16, // LPWSTR in/out
    dx: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc PathCompactPathW(
    hDC: pointer,  # HDC optional
    pszPath: ptr uint16,  # LPWSTR in/out
    dx: uint32  # DWORD
): int32 {.importc: "PathCompactPathW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shlwapi");
extern(Windows)
int PathCompactPathW(
    void* hDC,   // HDC optional
    wchar* pszPath,   // LPWSTR in/out
    uint dx   // DWORD
);
ccall((:PathCompactPathW, "SHLWAPI.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt16}, UInt32),
      hDC, pszPath, dx)
# hDC : HDC optional -> Ptr{Cvoid}
# pszPath : LPWSTR in/out -> Ptr{UInt16}
# dx : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t PathCompactPathW(
    void* hDC,
    uint16_t* pszPath,
    uint32_t dx);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.PathCompactPathW(hDC, pszPath, dx)
-- hDC : HDC optional
-- pszPath : LPWSTR in/out
-- dx : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const PathCompactPathW = lib.func('__stdcall', 'PathCompactPathW', 'int32_t', ['void *', 'uint16_t *', 'uint32_t']);
// PathCompactPathW(hDC, pszPath, dx)
// hDC : HDC optional -> 'void *'
// pszPath : LPWSTR in/out -> 'uint16_t *'
// dx : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  PathCompactPathW: { parameters: ["pointer", "buffer", "u32"], result: "i32" },
});
// lib.symbols.PathCompactPathW(hDC, pszPath, dx)
// hDC : HDC optional -> "pointer"
// pszPath : LPWSTR in/out -> "buffer"
// dx : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t PathCompactPathW(
    void* hDC,
    uint16_t* pszPath,
    uint32_t dx);
C, "SHLWAPI.dll");
// $ffi->PathCompactPathW(hDC, pszPath, dx);
// hDC : HDC optional
// pszPath : LPWSTR in/out
// dx : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Shlwapi extends StdCallLibrary {
    Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.UNICODE_OPTIONS);
    boolean PathCompactPathW(
        Pointer hDC,   // HDC optional
        char[] pszPath,   // LPWSTR in/out
        int dx   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shlwapi")]
lib LibSHLWAPI
  fun PathCompactPathW = PathCompactPathW(
    hDC : Void*,   # HDC optional
    pszPath : UInt16*,   # LPWSTR in/out
    dx : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef PathCompactPathWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef PathCompactPathWDart = int Function(Pointer<Void>, Pointer<Utf16>, int);
final PathCompactPathW = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<PathCompactPathWNative, PathCompactPathWDart>('PathCompactPathW');
// hDC : HDC optional -> Pointer<Void>
// pszPath : LPWSTR in/out -> Pointer<Utf16>
// dx : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PathCompactPathW(
  hDC: THandle;   // HDC optional
  pszPath: PWideChar;   // LPWSTR in/out
  dx: DWORD   // DWORD
): BOOL; stdcall;
  external 'SHLWAPI.dll' name 'PathCompactPathW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PathCompactPathW"
  c_PathCompactPathW :: Ptr () -> CWString -> Word32 -> IO CInt
-- hDC : HDC optional -> Ptr ()
-- pszPath : LPWSTR in/out -> CWString
-- dx : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pathcompactpathw =
  foreign "PathCompactPathW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* hDC : HDC optional -> (ptr void) *)
(* pszPath : LPWSTR in/out -> (ptr uint16_t) *)
(* dx : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("PathCompactPathW" path-compact-path-w :convention :stdcall) :int32
  (h-dc :pointer)   ; HDC optional
  (psz-path :pointer)   ; LPWSTR in/out
  (dx :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PathCompactPathW = Win32::API::More->new('SHLWAPI',
    'BOOL PathCompactPathW(HANDLE hDC, LPWSTR pszPath, DWORD dx)');
# my $ret = $PathCompactPathW->Call($hDC, $pszPath, $dx);
# hDC : HDC optional -> HANDLE
# pszPath : LPWSTR in/out -> LPWSTR
# dx : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API