본문 바로가기
[ Web ]/ASP.NET

Smart Client (PrintPreviewDialog와 OpenFileDialog의 사용)

by 관이119 2012. 9. 12.
출처 .NET 2005 | 꼬부기
원문 http://blog.naver.com/esct/20010134878

제목

Smart Client (PrintPreviewDialog와 OpenFileDialog의 사용)

작성자

김경균

업데이트

2005년 2월 18일

라이센스

출처만 밝혀주신다면 어디든지 가져가실수 있습니다.

단, 본 문서를 임의로 수정하여 배포하지는 말아주십시오.

Smart Client (PrintPreviewDialog와 OpenFileDialog의 사용)

스마트 클라이언트란 말 그대로 똑똑한 클라이언트라 할 수 있다.
기존의 ActiveX컨트롤과 비슷한 기능을 하지만 그보다 훨씬 간단히 접근 할 수 있다. 물론 그렇게 만만한 놈은 아니다.
지금부터 스마트 클라이언트를 이용하여 웹브라우저에서 인쇄 및 파일 업로드 기능을 작성해 보도록 하자.

기존의 <input type=file>태그를 통하여 파일업로드를 할 경우 기본 컨트롤의 모양을 보기 좋게 바꾸기는 상당히 어려운 점이 많이 있었다.

하지만 스마트 클라이언트를 이용하면 간단히 이미지를 클릭할때 OpenFileDialog를 띄울 수 있다.

그럼 예제를 시작해 보도록 하자!!

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace RichWinForm
{
//외부 스크립트의 호출을 위한 인터페이스
public interface IPrintControlCOMIncomming
{
void show(string oData);
string showDialog();
}

//외부 스크립트에서 public메서드를 호출하기위한
[ClassInterface(ClassInterfaceType.None)]

//유저컨트롤과 인터페이스(외부의 스크립트에서 public메서드 호출을 위해)를 상속받는다.
public class Print: System.Windows.Forms.UserControl, IPrintControlCOMIncomming
{
private System.Windows.Forms.Button btnFile;
System.Windows.Forms.TextBox textBox1;
System.Windows.Forms.Button button1;

//미리보기 다이얼로그 인스턴스 생성
PrintPreviewDialog ppd = new System.Windows.Forms.PrintPreviewDialog();

//파일선택 다이얼로그
OpenFileDialog ofd = new OpenFileDialog();

//미리보기의 문서 인스턴스 생성
PrintDocument pd = new PrintDocument();

//초기화
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnFile = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.Font = new System.Drawing.Font("돋움", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "인쇄하기(윈폼)";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 56);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// btnFile
//
this.btnFile.BackColor = System.Drawing.SystemColors.Control;
this.btnFile.Font = new System.Drawing.Font("돋움", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.btnFile.Location = new System.Drawing.Point(112, 104);
this.btnFile.Name = "btnFile";
this.btnFile.Size = new System.Drawing.Size(120, 23);
this.btnFile.TabIndex = 3;
this.btnFile.Text = "파일 업로드(윈폼)";
this.btnFile.Click += new System.EventHandler(this.btnFile_Click);
//
// Print
//
this.BackColor = System.Drawing.SystemColors.ControlLight;
this.Controls.Add(this.btnFile);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Print";
this.Size = new System.Drawing.Size(248, 176);
this.ResumeLayout(false);

}

//생성자
public Print()
{
InitializeComponent();
this.pd.PrintPage += new PrintPageEventHandler(this.OnPrintPage);
}

//페이지를 설정하는 이벤트 메서드
private void OnPrintPage(object obj, PrintPageEventArgs e)
{
string text = "앗싸!!! 된다...";
System.Drawing.Font printFont = new System.Drawing.Font("돋움", 12, System.Drawing.FontStyle.Regular);

e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 0, 0);
}

//윈폼 버튼 클릭시
private void button1_Click(object sender, System.EventArgs e)
{
//문서의 이름 설정
pd.DocumentName="tt";
//미리보기 다이얼로그에 문서 지정
ppd.Document = pd;
//다이얼로그 출력
ppd.ShowDialog();
}

#region IPrintControlCOMIncomming 멤버

//외부 스크립트 이벤트에 의하여 호줄 되는 메서드
public void show(string oData)
{
//외부에서 데이터를 가져와서
//버튼의 이름으로 설정
textBox1.Text = oData;
//문서의 이름 설정
pd.DocumentName="tt";
//미리보기 다이얼로그에 문서 지정
ppd.Document = pd;
//다이얼로그 출력
ppd.ShowDialog();
}

//웹폼 버튼 선택 시 파일 경로 출력
public string showDialog()
{
if(ofd.ShowDialog() == DialogResult.OK)
{
return ofd.FileName;
}
return null;
}
#endregion

//윈폼 버튼 선택 시 파일 경로 출력
private void btnFile_Click(object sender, System.EventArgs e)
{
if(ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
}
}
}
}

dll을 호출하는 HTML 코드

기존의 ActiveX와 동일하게 <OBJECT>태그를 통하여 dll을 사용한다.

<%@ Page language="c#" Codebehind="MyForm.aspx.cs" AutoEventWireup="false" Inherits="RichWinForm.WebForm1" %>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
function printClick()
{
MyForm.PrintPrev.show("외부에서 들어온 데이터 입니다.");
}
function openFile()
{
Output.innerText = MyForm.PrintPrev.showDialog();
}
</script>
<script for="Slider" event="Scroll (source,args)" language="javascript">
Output.innerText = MyForm.Slider.Value;
</script>
</HEAD>
<body>
<form id="MyForm" method="post" runat="server">
<OBJECT id="PrintPrev" height="200" width="300" classid="http:Print.dll#RichWinForm.Print"
VIEWASTEXT>
</OBJECT>
<br>
<br>
웹 버튼으로 인쇄해보자 : <input type="button" value="미리보기" onclick="printClick()">
<br>
<br>
웹 버튼으로 파일업로드 : <input type="button" value="파일찾기" onclick="openFile()">
<asp:Button id="btnSave" runat="server" Text="저장"></asp:Button></form>
<span id="Output" runat="server">0</span>

</body>
</HTML
>

 

댓글