### Build Python-WinSDK Package Source: https://github.com/pywinrt/python-winsdk/blob/main/CONTRIBUTING.md Steps to build the Python-WinSDK package using Python's build tools. It involves installing the 'build' package and then executing the build process. ```shell py -m pip install build ``` ```shell py -m build ``` -------------------------------- ### Clone Python-WinSDK Repository Source: https://github.com/pywinrt/python-winsdk/blob/main/CONTRIBUTING.md Instructions to clone the Python-WinSDK project from GitHub using Git. This is the first step to obtain the project's source code. ```shell git clone https://github.com/pywinrt/python-winsdk cd python-winsdk ``` -------------------------------- ### Create Debug Build Source: https://github.com/pywinrt/python-winsdk/blob/main/CONTRIBUTING.md This snippet shows how to create a debug build for the Python-WinSDK project. It requires a specific debug executable for Python (`python_d.exe`) to be installed. ```powershell ./build.ps1 ``` -------------------------------- ### Use winsdk.system.Array Sequence Protocol Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/winsdk.rst Examples illustrating how to use the Python sequence protocol with winsdk.system.Array objects, such as getting the size, accessing elements, and iterating. ```Python # get the number of elements in the array size = len(a1) # get the first element of the array item = a1[0] # get the last element of the array item = a1[-1] # iterate all items of the array for item in a1: ... # test for element in array if item in a1: ... ``` -------------------------------- ### Create winsdk.system.Array Instances Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/winsdk.rst Examples demonstrating how to create instances of the winsdk.system.Array class with different initializers, including specifying element type and providing initial values. ```Python from winsdk import Array from winsdk.windows.foundation import Point # array of 10 32-bit unsigned integers. a1 = Array("I", 10) # array of 3 points with initial values a2 = Array(Point, [Point(1, 1), Point(2, 2), Point(3, 3)]) ``` -------------------------------- ### Install Python Windows SDK Source: https://github.com/pywinrt/python-winsdk/blob/main/README.md Instructions for installing the winsdk package using common Python package managers like Poetry, Pipenv, and Pip. ```bash poetry add winsdk ``` ```bash pipenv install winsdk ``` ```bash pip install winsdk ``` -------------------------------- ### Update Generated Files Source: https://github.com/pywinrt/python-winsdk/blob/main/CONTRIBUTING.md This command updates the generated files within the Python-WinSDK project, specifically the `cppwint` and `pywinrt` directories. These directories should not be edited manually. ```powershell ./generate.ps1 ``` -------------------------------- ### CMake: Configure pywinrt Python Extension Build Source: https://github.com/pywinrt/python-winsdk/blob/main/CMakeLists.txt This CMake script configures the build process for the pywinrt Python extension. It sets C++ standards, compiler options for MSVC and MinGW, finds the Python 3 development module, and defines the output library as a Python MODULE. It also specifies precompiled headers and installation paths. ```CMake cmake_minimum_required(VERSION 3.24) cmake_policy(SET CMP0091 NEW) project(winsdk LANGUAGES CXX) file(GLOB sources "${CMAKE_CURRENT_SOURCE_DIR}/pywinrt/${CMAKE_PROJECT_NAME}/src/*.cpp") set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") if (MSVC) # TODO: re-enable /GL and /LTCG when we get a bigger build machine add_compile_options(/bigobj /GR- /permissive- /MP) string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # TODO: re-enable /GL and /LTCG when we get a bigger build machine # string(APPEND CMAKE_MODULE_LINKER_FLAGS " /LTCG:STATUS") if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "19.20.27404.0") string(APPEND CMAKE_CXX_FLAGS " /d2FH4") endif() else() add_compile_options(-Werror) if (MINGW) add_definitions(-D_WIN32_WINNT=_WIN32_WINNT_WIN10) # fixes "file too big" add_compile_options(-Wa,-mbig-obj) endif() endif() find_package(Python3 REQUIRED COMPONENTS Development.Module) Python3_add_library (_winrt MODULE ${sources}) set_target_properties(_winrt PROPERTIES LIBRARY_OUTPUT_NAME_DEBUG _winrt_d) target_precompile_headers(_winrt PRIVATE ${headers}) target_include_directories(_winrt PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/cppwinrt" "${CMAKE_CURRENT_SOURCE_DIR}/pywinrt/${CMAKE_PROJECT_NAME}/src") # speed up builds by precompiling commonly used headers target_precompile_headers(_winrt PRIVATE "pywinrt/winsdk/src/py.Windows.Foundation.h" "pywinrt/winsdk/src/py.Windows.Storage.Streams.h" ) # needed because including windows.h affects winrt set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/pywinrt/winsdk/src/runtime.cpp" PROPERTY SKIP_PRECOMPILE_HEADERS ON) # needed because including Shobjidl.h affects winrt set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/pywinrt/winsdk/src/_winrt.cpp" PROPERTY SKIP_PRECOMPILE_HEADERS ON) # needed because including unknwn.h affects winrt set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/pywinrt/winsdk/src/py.Windows.Graphics.Capture.Interop.cpp" PROPERTY SKIP_PRECOMPILE_HEADERS ON) install(TARGETS _winrt DESTINATION "pywinrt/${CMAKE_PROJECT_NAME}") ``` -------------------------------- ### winsdk.system.Object API Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/winsdk.rst Documentation for the base WinRT System.Object type, which is the root of all WinRT runtime objects and cannot be instantiated directly. ```APIDOC .. class:: Object A wrapper around the WinRT ``System.Object`` type. This is the base type of all WinRT runtime objects and cannot be instantiated directly. This type currently has no members. ``` -------------------------------- ### Create Graphics Capture Item for Monitor Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/windows/graphics.capture.interop.rst Targets a monitor for the creation of a graphics capture item. Takes a monitor handle (HMONITOR) as input and returns a GraphicsCaptureItem. ```APIDOC create_for_monitor(monitor) Targets a monitor(s) for the creation of a graphics capture item. Parameters: monitor (int): The monitor handle (HMONITOR) that represents the monitor to capture. Returns: winsdk.windows.graphics.capture.GraphicsCaptureItem: A graphics capture item. See Also: https://learn.microsoft.com/windows/win32/api/windows.graphics.capture.interop/nf-windows-graphics-capture-interop-igraphicscaptureiteminterop-createformonitor ``` -------------------------------- ### Create Graphics Capture Item for Window Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/windows/graphics.capture.interop.rst Targets a single window for the creation of a graphics capture item. Takes a window handle (HWND) as input and returns a GraphicsCaptureItem. ```APIDOC create_for_window(window) Targets a single window for the creation of a graphics capture item. Parameters: window (int): The window handle (HWND) that represents the window to capture. Returns: winsdk.windows.graphics.capture.GraphicsCaptureItem: A graphics capture item. See Also: https://learn.microsoft.com/windows/win32/api/windows.graphics.capture.interop/nf-windows-graphics-capture-interop-igraphicscaptureiteminterop-createforwindow ``` -------------------------------- ### winsdk.system.Array API Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/api/winsdk.rst Documentation for the WinRT System.Array type, which wraps WinRT arrays and implements the Python sequence protocol. ```APIDOC .. class:: Array(type, [initializer, ] /) A wrapper around the WinRT ``System.Array`` type. This type implements the Python sequence protocol. :param type: The type to use for elements of the array. This can be a WinRT type or a format string for fundamental types. :type type: str or type :param initializer: An optional iterator of values to use to initialize the array. If an integer value is given, an empty array of that size will be initialized. For value types, any object supporting the CPython buffer protocol with the correct layout can be used as an initializer. :type initializer: int or iter or buffer Sequence protocol methods: - len(array): Returns the number of elements. - array[index]: Accesses an element by index. - for item in array: Iterates over elements. - item in array: Checks for element existence. ``` -------------------------------- ### Importing WinRT Namespaces Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Demonstrates how to import members from a WinRT namespace or the namespace module itself in Python. ```python # import members of a namespace from winsdk.windows.foundation import Uri # or import the namespace module itself import winsdk.windows.foundation as wf ``` -------------------------------- ### Accessing Struct Fields Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Shows how to create a WinRT struct object and access/modify its fields using Python attributes, adhering to PEP8 naming conventions. ```python from winsdk.windows.foundation import Point point = Point(1, 2) x = point.x point.x = 3 ``` -------------------------------- ### Using WinRT IClosable Types as Python Context Managers Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Types implementing the WinRT IClosable interface can be used as Python context managers, similar to C#'s 'using' statement with IDisposable. This ensures resources are properly managed and released. ```python from winsdk.windows.foundation import MemoryBuffer with MemoryBuffer(256) as buf: # Use the buffer object here pass ``` -------------------------------- ### Accessing WinRT Static Properties in Python Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Static properties in WinRT are represented as class attributes in Python, accessed via the type object itself rather than an instance. Type hints use typing.ClassVar. Older beta versions used get_name()/put_name() methods. ```python from winsdk.windows.foundation import GuidHelper empty_uuid = GuidHelper.empty ``` -------------------------------- ### WinRT DateTime to Python datetime Conversion Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Windows.Foundation.DateTime is converted to Python's datetime.datetime. WinRT uses 100ns resolution and UTC, while Python's datetime has microsecond resolution and can be timezone-aware or naive. Precision loss may occur, and naive datetimes are assumed local. ```python from datetime import datetime, timezone, timedelta # Example: set notification to expire 10 seconds from now # toaster.expiration_time = datetime.now(timezone.utc) + timedelta(seconds=10) ``` -------------------------------- ### Accessing WinRT Properties in Python Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst WinRT properties are exposed as Python descriptor-like attributes. Properties with getters can be read, and those with setters can be modified. Property names follow the lower_case_with_underscores convention. Deleting properties is not permitted. ```python from winsdk.windows.foundation import Uri uri = Uri("https://example.com") print(uri.scheme_name) ``` -------------------------------- ### WinRT TimeSpan to Python timedelta Conversion Source: https://github.com/pywinrt/python-winsdk/blob/main/docs/types.rst Windows.Foundation.TimeSpan is converted to Python's datetime.timedelta. WinRT uses 100ns resolution, while Python's timedelta uses microseconds, leading to potential minor precision loss. WinRT serializes time spans as 100s of nanoseconds. ```python from datetime import timedelta # Example: create a timedelta object # duration = timedelta(days=1, hours=5) # winrt_timespan = convert_to_winrt_timespan(duration) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.