Skip to content

Instantly share code, notes, and snippets.

@KOZ60
Last active May 20, 2024 02:27
Show Gist options
  • Save KOZ60/e0fd845843298a0a49eb45efe63bc74c to your computer and use it in GitHub Desktop.
Save KOZ60/e0fd845843298a0a49eb45efe63bc74c to your computer and use it in GitHub Desktop.
LayeredWindow

Windows 8 から子ウインドウがレイヤードウインドウになれるので、アプリケーションマニュフェストを追加して

  <!-- Windows 8 -->
  <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

のコメントアウトを解除すると、簡単に透過できます。

Imports System.Runtime.InteropServices

Public Class Form1

    Private Const WS_EX_LAYERED = &H80000
    Private Const GWL_EXSTYLE = -20
    Private Enum LWA
        COLORKEY = &H1
        ALPHA = &H2
    End Enum

    Private Sub PictureBox_HandleCreated(sender As Object, e As EventArgs) _
                Handles PictureBox1.HandleCreated,
                        PictureBox2.HandleCreated
        Dim pic = DirectCast(sender, PictureBox)
        pic.Image = My.Resources.megane_hikaru_woman
        pic.SizeMode = PictureBoxSizeMode.Zoom
        pic.BackColor = Color.Gray
        Dim dwStyle = GetWindowLong(pic.Handle, GWL_EXSTYLE)
        dwStyle = dwStyle Or WS_EX_LAYERED
        SetWindowLong(pic.Handle, GWL_EXSTYLE, dwStyle)
        SetLayeredWindowAttributes(
                pic.Handle, ColorTranslator.ToWin32(pic.BackColor),
                0, LWA.COLORKEY)
    End Sub

    <DllImport("user32.dll")>
    Private Shared Function GetWindowLong(
                hWnd As IntPtr, nIndex As Integer) As Integer
    End Function

    <DllImport("user32.dll")>
    Private Shared Function SetWindowLong(
                hWnd As IntPtr, nIndex As Integer,
                dwNewLong As Integer) As Integer
    End Function

    <DllImport("user32.dll")>
    Private Shared Function SetLayeredWindowAttributes(
                hWnd As IntPtr, crKey As Integer,
                bAlpha As Byte, dwFlags As LWA) As Boolean
    End Function

End Class

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment