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

SetScrollRange

関数
スクロールバーの最小・最大範囲を設定する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// USER32.dll
#include <windows.h>

BOOL SetScrollRange(
    HWND hWnd,
    SCROLLBAR_CONSTANTS nBar,
    INT nMinPos,
    INT nMaxPos,
    BOOL bRedraw
);

パラメーター

名前方向説明
hWndHWNDinnBar パラメーターの値に応じて、スクロールバーコントロール、または標準スクロールバーを持つウィンドウへのハンドルです。
nBarSCROLLBAR_CONSTANTSin

設定対象のスクロールバーを指定します。このパラメーターには次のいずれかの値を指定できます。

Value Meaning
SB_CTL
スクロールバーコントロールの範囲を設定します。hwnd パラメーターはスクロールバーコントロールへのハンドルでなければなりません。
SB_HORZ
ウィンドウの標準水平スクロールバーの範囲を設定します。
SB_VERT
ウィンドウの標準垂直スクロールバーの範囲を設定します。
nMinPosINTinスクロールの最小位置を指定します。
nMaxPosINTinスクロールの最大位置を指定します。
bRedrawBOOLin変更を反映するためにスクロールバーを再描画するかどうかを指定します。このパラメーターが TRUE の場合、スクロールバーは再描画されます。FALSE の場合、スクロールバーは再描画されません。

戻り値の型: BOOL

公式ドキュメント

SetScrollRange 関数は、指定したスクロールバーのスクロールボックスの最小位置と最大位置を設定します。

戻り値

型: BOOL

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには GetLastError を呼び出します。

解説(Remarks)

nMinPosnMaxPos を同じ値に設定することで、SetScrollRange を使用してスクロールバーを非表示にできます。アプリケーションは、スクロールバーメッセージの処理中に SetScrollRange 関数を呼び出してスクロールバーを非表示にしてはいけません。新しいアプリケーションでは、スクロールバーを非表示にするために ShowScrollBar 関数を使用してください。

SetScrollRange の呼び出しが SetScrollPos 関数の呼び出しの直後に行われる場合、スクロールバーが二重に描画されるのを防ぐために、SetScrollPosbRedraw パラメーターを 0 にする必要があります。

標準スクロールバーの既定の範囲は 0 から 100 です。スクロールバーコントロールの既定の範囲は空です(nMinPos パラメーターと nMaxPos パラメーターの値はどちらも 0 です)。nMinPos パラメーターと nMaxPos パラメーターで指定する値の差は、MAXLONG の値を超えてはいけません。

スクロールバーの位置を示すメッセージ WM_HSCROLLWM_VSCROLL は位置データが 16 ビットに制限されているため、これらのメッセージのみに位置データを依存するアプリケーションでは、SetScrollRange 関数の nMaxPos パラメーターの実用的な最大値は 65,535 になります。

ただし、SetScrollInfoSetScrollPosSetScrollRangeGetScrollInfoGetScrollPos、および GetScrollRange の各関数は 32 ビットのスクロールバー位置データをサポートしているため、WM_HSCROLL および WM_VSCROLL メッセージの 16 ビットの制限を回避する方法があります。この手法の説明については GetScrollInfo を参照してください。

nBar パラメーターが SB_CTL で、hWnd パラメーターで指定されたウィンドウがシステムスクロールバーコントロールでない場合、システムはスクロールバー情報を設定するために SBM_SETRANGE メッセージをウィンドウに送信します。これにより、SetScrollRange はスクロールバーを模倣するカスタムコントロールに対して動作できます。ウィンドウが SBM_SETRANGE メッセージを処理しない場合、SetScrollRange 関数は失敗します。

例については、所有者表示クリップボード形式の使用を参照してください。

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

各言語での呼び出し定義

// USER32.dll
#include <windows.h>

BOOL SetScrollRange(
    HWND hWnd,
    SCROLLBAR_CONSTANTS nBar,
    INT nMinPos,
    INT nMaxPos,
    BOOL bRedraw
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetScrollRange(
    IntPtr hWnd,   // HWND
    int nBar,   // SCROLLBAR_CONSTANTS
    int nMinPos,   // INT
    int nMaxPos,   // INT
    bool bRedraw   // BOOL
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetScrollRange(
    hWnd As IntPtr,   ' HWND
    nBar As Integer,   ' SCROLLBAR_CONSTANTS
    nMinPos As Integer,   ' INT
    nMaxPos As Integer,   ' INT
    bRedraw As Boolean   ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hWnd : HWND
' nBar : SCROLLBAR_CONSTANTS
' nMinPos : INT
' nMaxPos : INT
' bRedraw : BOOL
Declare PtrSafe Function SetScrollRange Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal nBar As Long, _
    ByVal nMinPos As Long, _
    ByVal nMaxPos As Long, _
    ByVal bRedraw As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetScrollRange = ctypes.windll.user32.SetScrollRange
SetScrollRange.restype = wintypes.BOOL
SetScrollRange.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    ctypes.c_int,  # nBar : SCROLLBAR_CONSTANTS
    ctypes.c_int,  # nMinPos : INT
    ctypes.c_int,  # nMaxPos : INT
    wintypes.BOOL,  # bRedraw : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetScrollRange = Fiddle::Function.new(
  lib['SetScrollRange'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_INT,  # nBar : SCROLLBAR_CONSTANTS
    Fiddle::TYPE_INT,  # nMinPos : INT
    Fiddle::TYPE_INT,  # nMaxPos : INT
    Fiddle::TYPE_INT,  # bRedraw : BOOL
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetScrollRange(
        hWnd: *mut core::ffi::c_void,  // HWND
        nBar: i32,  // SCROLLBAR_CONSTANTS
        nMinPos: i32,  // INT
        nMaxPos: i32,  // INT
        bRedraw: i32  // BOOL
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool SetScrollRange(IntPtr hWnd, int nBar, int nMinPos, int nMaxPos, bool bRedraw);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetScrollRange' -Namespace Win32 -PassThru
# $api::SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
#uselib "USER32.dll"
#func global SetScrollRange "SetScrollRange" sptr, sptr, sptr, sptr, sptr
; SetScrollRange hWnd, nBar, nMinPos, nMaxPos, bRedraw   ; 戻り値は stat
; hWnd : HWND -> "sptr"
; nBar : SCROLLBAR_CONSTANTS -> "sptr"
; nMinPos : INT -> "sptr"
; nMaxPos : INT -> "sptr"
; bRedraw : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetScrollRange "SetScrollRange" sptr, int, int, int, int
; res = SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
; hWnd : HWND -> "sptr"
; nBar : SCROLLBAR_CONSTANTS -> "int"
; nMinPos : INT -> "int"
; nMaxPos : INT -> "int"
; bRedraw : BOOL -> "int"
; BOOL SetScrollRange(HWND hWnd, SCROLLBAR_CONSTANTS nBar, INT nMinPos, INT nMaxPos, BOOL bRedraw)
#uselib "USER32.dll"
#cfunc global SetScrollRange "SetScrollRange" intptr, int, int, int, int
; res = SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
; hWnd : HWND -> "intptr"
; nBar : SCROLLBAR_CONSTANTS -> "int"
; nMinPos : INT -> "int"
; nMaxPos : INT -> "int"
; bRedraw : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetScrollRange = user32.NewProc("SetScrollRange")
)

// hWnd (HWND), nBar (SCROLLBAR_CONSTANTS), nMinPos (INT), nMaxPos (INT), bRedraw (BOOL)
r1, _, err := procSetScrollRange.Call(
	uintptr(hWnd),
	uintptr(nBar),
	uintptr(nMinPos),
	uintptr(nMaxPos),
	uintptr(bRedraw),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetScrollRange(
  hWnd: THandle;   // HWND
  nBar: Integer;   // SCROLLBAR_CONSTANTS
  nMinPos: Integer;   // INT
  nMaxPos: Integer;   // INT
  bRedraw: BOOL   // BOOL
): BOOL; stdcall;
  external 'USER32.dll' name 'SetScrollRange';
result := DllCall("USER32\SetScrollRange"
    , "Ptr", hWnd   ; HWND
    , "Int", nBar   ; SCROLLBAR_CONSTANTS
    , "Int", nMinPos   ; INT
    , "Int", nMaxPos   ; INT
    , "Int", bRedraw   ; BOOL
    , "Int")   ; return: BOOL
●SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw) = DLL("USER32.dll", "bool SetScrollRange(void*, int, int, int, bool)")
# 呼び出し: SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
# hWnd : HWND -> "void*"
# nBar : SCROLLBAR_CONSTANTS -> "int"
# nMinPos : INT -> "int"
# nMaxPos : INT -> "int"
# bRedraw : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn SetScrollRange(
    hWnd: ?*anyopaque, // HWND
    nBar: i32, // SCROLLBAR_CONSTANTS
    nMinPos: i32, // INT
    nMaxPos: i32, // INT
    bRedraw: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;
proc SetScrollRange(
    hWnd: pointer,  # HWND
    nBar: int32,  # SCROLLBAR_CONSTANTS
    nMinPos: int32,  # INT
    nMaxPos: int32,  # INT
    bRedraw: int32  # BOOL
): int32 {.importc: "SetScrollRange", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
int SetScrollRange(
    void* hWnd,   // HWND
    int nBar,   // SCROLLBAR_CONSTANTS
    int nMinPos,   // INT
    int nMaxPos,   // INT
    int bRedraw   // BOOL
);
ccall((:SetScrollRange, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Int32, Int32, Int32, Int32),
      hWnd, nBar, nMinPos, nMaxPos, bRedraw)
# hWnd : HWND -> Ptr{Cvoid}
# nBar : SCROLLBAR_CONSTANTS -> Int32
# nMinPos : INT -> Int32
# nMaxPos : INT -> Int32
# bRedraw : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetScrollRange(
    void* hWnd,
    int32_t nBar,
    int32_t nMinPos,
    int32_t nMaxPos,
    int32_t bRedraw);
]]
local user32 = ffi.load("user32")
-- user32.SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
-- hWnd : HWND
-- nBar : SCROLLBAR_CONSTANTS
-- nMinPos : INT
-- nMaxPos : INT
-- bRedraw : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetScrollRange = lib.func('__stdcall', 'SetScrollRange', 'int32_t', ['void *', 'int32_t', 'int32_t', 'int32_t', 'int32_t']);
// SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
// hWnd : HWND -> 'void *'
// nBar : SCROLLBAR_CONSTANTS -> 'int32_t'
// nMinPos : INT -> 'int32_t'
// nMaxPos : INT -> 'int32_t'
// bRedraw : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  SetScrollRange: { parameters: ["pointer", "i32", "i32", "i32", "i32"], result: "i32" },
});
// lib.symbols.SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw)
// hWnd : HWND -> "pointer"
// nBar : SCROLLBAR_CONSTANTS -> "i32"
// nMinPos : INT -> "i32"
// nMaxPos : INT -> "i32"
// bRedraw : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetScrollRange(
    void* hWnd,
    int32_t nBar,
    int32_t nMinPos,
    int32_t nMaxPos,
    int32_t bRedraw);
C, "USER32.dll");
// $ffi->SetScrollRange(hWnd, nBar, nMinPos, nMaxPos, bRedraw);
// hWnd : HWND
// nBar : SCROLLBAR_CONSTANTS
// nMinPos : INT
// nMaxPos : INT
// bRedraw : BOOL
// 構造体/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 User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class);
    boolean SetScrollRange(
        Pointer hWnd,   // HWND
        int nBar,   // SCROLLBAR_CONSTANTS
        int nMinPos,   // INT
        int nMaxPos,   // INT
        boolean bRedraw   // BOOL
    );
}
@[Link("user32")]
lib LibUSER32
  fun SetScrollRange = SetScrollRange(
    hWnd : Void*,   # HWND
    nBar : Int32,   # SCROLLBAR_CONSTANTS
    nMinPos : Int32,   # INT
    nMaxPos : Int32,   # INT
    bRedraw : Int32   # BOOL
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetScrollRangeNative = Int32 Function(Pointer<Void>, Int32, Int32, Int32, Int32);
typedef SetScrollRangeDart = int Function(Pointer<Void>, int, int, int, int);
final SetScrollRange = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetScrollRangeNative, SetScrollRangeDart>('SetScrollRange');
// hWnd : HWND -> Pointer<Void>
// nBar : SCROLLBAR_CONSTANTS -> Int32
// nMinPos : INT -> Int32
// nMaxPos : INT -> Int32
// bRedraw : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetScrollRange(
  hWnd: THandle;   // HWND
  nBar: Integer;   // SCROLLBAR_CONSTANTS
  nMinPos: Integer;   // INT
  nMaxPos: Integer;   // INT
  bRedraw: BOOL   // BOOL
): BOOL; stdcall;
  external 'USER32.dll' name 'SetScrollRange';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetScrollRange"
  c_SetScrollRange :: Ptr () -> Int32 -> Int32 -> Int32 -> CInt -> IO CInt
-- hWnd : HWND -> Ptr ()
-- nBar : SCROLLBAR_CONSTANTS -> Int32
-- nMinPos : INT -> Int32
-- nMaxPos : INT -> Int32
-- bRedraw : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setscrollrange =
  foreign "SetScrollRange"
    ((ptr void) @-> int32_t @-> int32_t @-> int32_t @-> int32_t @-> returning int32_t)
(* hWnd : HWND -> (ptr void) *)
(* nBar : SCROLLBAR_CONSTANTS -> int32_t *)
(* nMinPos : INT -> int32_t *)
(* nMaxPos : INT -> int32_t *)
(* bRedraw : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("SetScrollRange" set-scroll-range :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND
  (n-bar :int32)   ; SCROLLBAR_CONSTANTS
  (n-min-pos :int32)   ; INT
  (n-max-pos :int32)   ; INT
  (b-redraw :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetScrollRange = Win32::API::More->new('USER32',
    'BOOL SetScrollRange(HANDLE hWnd, int nBar, int nMinPos, int nMaxPos, BOOL bRedraw)');
# my $ret = $SetScrollRange->Call($hWnd, $nBar, $nMinPos, $nMaxPos, $bRedraw);
# hWnd : HWND -> HANDLE
# nBar : SCROLLBAR_CONSTANTS -> int
# nMinPos : INT -> int
# nMaxPos : INT -> int
# bRedraw : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型