섭섭한 개발일지

[VB] Visual Basic으로 PDF 병합 프로그램 만들기 (4 - 추가기능DragDrop) 본문

Project/PDF병합프로그램

[VB] Visual Basic으로 PDF 병합 프로그램 만들기 (4 - 추가기능DragDrop)

Seop 2023. 1. 16. 11:06

[PDF병합프로그램 설치파일 다운로드]

 

 

 

PDF 병합 프로그램에 DragDrop 기능을 추가하여 이용자의 편의성을 증대하려고 한다.

어렵지 않은 기능이므로 코드부터 보도록 하겠습니다.

 

Public Class PdfMerge
	Private PDFFunction As PDFFunction = New PDFFunction
	Private PDFValidator As PDFValidator = New PDFValidator
	Private Files As Dictionary(Of String, String) = New Dictionary(Of String, String)
	Private filePath As String = ""
	Private fileName As String = ""
	Private fileExtension As String = ""
	Private Const COMP_MSH As String = "COMPLETE"
    
    	Private Sub AddFileList_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AddFileList.DragEnter
		If e.Data.GetDataPresent(DataFormats.FileDrop) Then
			e.Effect = DragDropEffects.Copy
		End If
	End Sub
    
	Private Sub AddFileList_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AddFileList.DragDrop
		Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
		Dim fileInfo As IO.FileInfo = Nothing

		For index = 0 To files.Length - 1
			filePath = files(index)
			fileName = System.IO.Path.GetFileName(filePath)
			fileInfo = New IO.FileInfo(filePath)
			fileExtension = fileInfo.Extension

			If Not (PDFValidator.File_Compare(fileName)) Then ' 목록에 동일한 파일명이 존재할 경우 추가 불가
				Continue For
			End If

			If Not (PDFValidator.File_Ext_PDF(fileExtension)) Then ' PDF 형식의 파일인지 확인
				Continue For
			End If

			AddFileList_Add(fileName, filePath)
		Next
	End Sub
    
    	Private Sub AddFileList_Add(ByVal name As String, ByVal path As String)
		AddFileList.Items.Add(name)
		Files.Add(name, path)
		PDFFunction.List_Add_Item_Btn_Enabled()
		PDFFunction.List_Add_Two_Item_Btn_Enabled()
		PDFFunction.Notis_Add("[추가]", "파일을 추가하였습니다. " + name)
	End Sub
End Class

 

코드 내에서 사용하는 변수들은 모두 포함 시켰습니다.

Validator 와 Function은 기능과는 상관 없는 class이니 없는 코드라고 생각하고 보면 된다.

 

 

먼저 통상적으로 사용되는 DragDrop을 앱에 적용을 하고 이용자들의 사용 유도를 위해 아래의 범위에만 DragDrop 기능을 추가하려고 한다.

 

 

범위는 추가한 파일들을 목록으로 정리하는 영역이므로 딱 좋을 것 같다.

그럼 이제 DragDrop기능을 활성화 하기 위해서 에디터에서 ListBox의 AllowDrop 옵션을 True로 변경해줘야 한다.

 

 

이 기능을 켜준 뒤 실행을 시킨다면 ListBox에 파일을 Drag 해보면 반응이 보인다.

그럼 이제 Drop을 했을 때 발생하는 이벤트에 대한 코드를 보자

 

AddFileList_DragEnter (DragDrop 시 파일 복사)
	Private Sub AddFileList_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AddFileList.DragEnter
		If e.Data.GetDataPresent(DataFormats.FileDrop) Then
			e.Effect = DragDropEffects.Copy
		End If
	End Sub

Handles로 ListBox.DragEnter를 사용했다.

이는 DragDrop한 파일이 있으면 대상을 복사하는 기능을 해준다.

 

 

AddFileList_DragDrop (DragDrop에 대한 핵심 코드)
	Private Sub AddFileList_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AddFileList.DragDrop
		Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
		Dim fileInfo As IO.FileInfo = Nothing

		For index = 0 To files.Length - 1
			filePath = files(index)
			fileName = System.IO.Path.GetFileName(filePath)
			fileInfo = New IO.FileInfo(filePath)
			fileExtension = fileInfo.Extension

			If Not (PDFValidator.File_Compare(fileName)) Then ' 목록에 동일한 파일명이 존재할 경우 추가 불가
				Continue For
			End If

			If Not (PDFValidator.File_Ext_PDF(fileExtension)) Then ' PDF 형식의 파일인지 확인
				Continue For
			End If

			AddFileList_Add(fileName, filePath)
		Next
	End Sub

Handles에 ListBox.DragDrop 을 사용한다.

이용자가 ListBox 에 파일을 드랍할 경우 발생하는 이벤트이며 

이용자가 다수의 파일을 드랍할 경우 files에 배열로 데이터가 저장된다.

for 반복문을 사용하여 file들의 검증작업을 진행하고 문제가 있는 파일은 저장하지 않고 넘어가는 것으로 끝이다.

 

 

 

Comments