주어진 코드는 윈도우 폼을 사용하여 사용자 지정 닫기 버튼이 있는 풀스크린 애플리케이션을 만드는 예시입니다.
Public Class fMain
' 윈도우 API 함수 선언
<DllImport("user32.dll")>
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean
End Function
' SetWindowPos 함수의 플래그 열거형
<Flags>
Private Enum SetWindowPosFlags As UInteger
SynchronousWindowPosition = &H4000
DeferErase = &H2000
DrawFrame = &H20
FrameChanged = &H20
HideWindow = &H80
DoNotActivate = &H10
DoNotCopyBits = &H100
IgnoreMove = &H2
DoNotChangeOwnerZOrder = &H200
DoNotRedraw = &H8
DoNotReposition = &H200
DoNotSendChangingEvent = &H400
IgnoreResize = &H1
IgnoreZOrder = &H4
ShowWindow = &H40
End Enum
' 사용자 지정 닫기 버튼 PictureBox
Private cmd_close As PictureBox
' 폼이 로드될 때 발생하는 이벤트 처리기
Private Sub fMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 사용자 지정 닫기 버튼 생성
cmd_close = New PictureBox
cmd_close.Image = My.Resources.ResourceManager.GetObject("close") ' 리소스에서 이미지 가져오기
cmd_close.Size = New Size(17, 17) ' 크기 설정
cmd_close.SizeMode = PictureBoxSizeMode.Zoom ' 이미지 크기 조정 방법 설정
Me.Controls.Add(cmd_close) ' 폼에 PictureBox 추가
' 사용자 지정 닫기 버튼 클릭 이벤트 핸들러 추가
AddHandler cmd_close.Click, Sub(xs As Object, xe As EventArgs)
Application.Exit() ' 애플리케이션 종료
End Sub
' 폼 설정
Me.FormBorderStyle = FormBorderStyle.None ' 테두리 스타일 없음
Dim window As IntPtr = FindWindow("Shell_traywnd", "") ' 작업 표시줄 찾기
SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.HideWindow) ' 작업 표시줄 숨기기
Me.WindowState = FormWindowState.Maximized ' 최대화
Me.TopMost = True ' 항상 위에 표시
End Sub
' 폼이 Dispose될 때 발생하는 이벤트 처리기
Private Sub MyBase_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
Dim window As IntPtr = FindWindow("Shell_traywnd", "") ' 작업 표시줄 찾기
SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.ShowWindow) ' 작업 표시줄 다시 표시
End Sub
' 폼 크기 조정 이벤트 처리기
Private Sub fMain_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Dim S As Size = Me.ClientRectangle.Size ' 클라이언트 영역 크기 가져오기
If cmd_close IsNot Nothing Then cmd_close.Location = New Point(S.Width - cmd_close.Width - 15, 15) ' 사용자 지정 닫기 버튼 위치 설정
End Sub
End Class
사용자 지정 닫기 버튼을 클릭하면 애플리케이션이 종료됩니다. 폼이 로드되면 작업 표시줄이 숨겨지고, 폼이 닫힐 때 작업 표시줄이 다시 표시됩니다.
사용자 지정 닫기 버튼은 폼의 우측 상단에 표시됩니다.