'2009/12'에 해당되는 글 7건
- 2009/12/27 C#을 이용한 LPT1 포트 제어 자료 - 시작시점...
- 2009/12/23 MD5 샘플 코드
- 2009/12/16 C# WebBrowser의 다른 사용법 (WebBrowser, Bitmap)
- 2009/12/15 C# USB 인식 심플한 코드
- 2009/12/15 C# Thread, delegate, Invoke 사용예
- 2009/12/07 WebBrowser 처리방식.. 참.. (1)
- 2009/12/01 [MSSQL] 문자일 일부분 특정문자(**)로 치환
참고자료 : http://www.codeproject.com/KB/cs/control_e_appliances.aspx
1. hwinterface.ocx 를 regsvr32 를 통해서 등록한다.
2. Visual Studio 의 도구 상자에서 ocx 를 등록한다.
- Hwinterface Control의 activex 컨트롤이 생긴다.
3. 해당 폼으로 가져와서 인스턴스를 생성한다.
4. 나머지 코드는 참고 자료의 링크에 존재 하는 파일을 참고 하기 바람
--선언--
using System.Security.Cryptography;
--코드--
#region MD5 메소드
static string getMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// Verify a hash against a string.
static bool verifyMd5Hash(string input, string hash)
{
// Hash the input.
string hashOfInput = getMd5Hash(input);
// Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
#endregion
#region MD5 테스트 블록
private void button1_Click(object sender, EventArgs e)
{
// 암호화 문자열을 가져온다.
string convmd5 = getMd5Hash(textBox1.Text);
// 암호화된 내용을 출력한다.
textBox2.Text = convmd5;
}
private void button2_Click(object sender, EventArgs e)
{
// 해당 문자열을 가져와서 암호화된 내용과 비교 한다.
if (verifyMd5Hash(textBox3.Text, textBox2.Text) == true)
{
MessageBox.Show("맞습니다.");
}
else
{
MessageBox.Show("틀립니다.");
}
}
#endregion
WebBrowser를 사용해야 되는 부분이 생겨서
많이 자료를 보고 해봤지만 TopMost라는 특성 때문에 Winform, WPF 모두
원하는 효과를 낼수가 없더라구요.
그래서 여러가지 보고 테스트도 해봤지만.
어떤건 속도가 너무 느리더라구요.
아래 코드는 완전 해결한건 아니지만,
이것도 조금은 접근이 된거 같아요.
목표점은
- 스레드를 통한 백그라운드 처리를 통해서 다른 UI의 동작을 원할하게 처리
- 모자이크 처리로 화면에 출력함으로 해서 조금은 이펙트를 중점으로 한다.
아래의 코드는 일부 접근된 코드를 우선 올려 봅니다.
-- Code --
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
WebBrowser web = new WebBrowser();
public Form1()
{
InitializeComponent();
web.Width = 1000;
web.Height = 1000;
web.ScrollBarsEnabled = false;
web.ScriptErrorsSuppressed = true;
web.Navigate("http://dev.iamgsi.com/googlemap");
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
//while (web.ReadyState != WebBrowserReadyState.Complete)
// System.Windows.Forms.Application.DoEvents();
//System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
int width = web.Document.Body.ScrollRectangle.Width;
int height = web.Document.Body.ScrollRectangle.Height;
web.Width = width;
web.Height = height;
System.Drawing.Bitmap bmp = new Bitmap(width, height);
web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
this.pictureBox1.Width = width;
this.pictureBox1.Height = height;
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
}
this.pictureBox1.Image = null;
this.pictureBox1.Image = bmp;
}
private void button2_Click(object sender, EventArgs e)
{
web.Document.InvokeScript("MoveAddress", new object[] { "서울" });
}
private void button3_Click(object sender, EventArgs e)
{
web.Document.InvokeScript("MoveAddress", new object[] { "거창" });
}
private void button4_Click(object sender, EventArgs e)
{
web.Document.InvokeScript("MoveAddress", new object[] { "광주" });
}
private void timer1_Tick(object sender, EventArgs e)
{
if (web.ReadyState != WebBrowserReadyState.Complete)
return;
int width = web.Document.Body.ScrollRectangle.Width;
int height = web.Document.Body.ScrollRectangle.Height;
web.Width = width;
web.Height = height;
System.Drawing.Bitmap bmp = new Bitmap(width, height);
web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
this.pictureBox1.Width = width;
this.pictureBox1.Height = height;
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
}
this.pictureBox1.Image = null;
this.pictureBox1.Image = bmp;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
bool bFind = false;
// USB 상태 체크
DriveInfo [] diArray = DriveInfo.GetDrives();
foreach (DriveInfo di in diArray)
{
if (di.IsReady == true && di.DriveType == DriveType.Removable)
{
bFind = true;
break;
}
}
label1.Text = (bFind == true) ? "존재합니다." : "없습니다.";
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
}
public partial class ucPanel : UserControl
{
public ucPanel()
{
InitializeComponent();
//
backgroundWorker1.RunWorkerAsync();
}
static public Font ChangeFontSize2(Font font, float fontSize, GraphicsUnit unit)
{
if (font != null)
{
float currentSize = font.Size;
if (currentSize != fontSize)
{
font = new Font(font.Name, fontSize,
font.Style, unit,
font.GdiCharSet, font.GdiVerticalFont);
}
}
return font;
}
public delegate void OnAddNode(string title, int x, int y);
public void AddNode(string title, int x, int y)
{
if (this.InvokeRequired)
{
this.Invoke(new OnAddNode(this.AddNode), new object[] { title, x, y });
return;
}
// 객체 추가
Label lbl2 = new Label();
lbl2.AutoSize = true;
lbl2.Text = title;
lbl2.Font = ChangeFontSize2(lbl2.Font, lbl2.Font.Size * 2, GraphicsUnit.Pixel);
lbl2.Left = x;
lbl2.Top = y;
this.Controls.Add(lbl2);
ucNode ucn = new ucNode();
ucn.Left = x;
ucn.Top = y;
this.Controls.Add(ucn);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int breakCount = 0;
Random rand = new Random();
while (true)
{
//
this.Invoke(new OnAddNode(this.AddNode), new object[] { "test", rand.Next(10, 738), rand.Next(10, 460) });
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
//
breakCount++;
if (breakCount > 100)
{
break;
}
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
2일 정도 자료를 찾아 보고 했지만.
WebBrowser 는 왜 WPF의 렌더링 방식과 차이가 있는지 모르겠다.
왜냐하면 WebBrowser 위에 다른걸 그리고 싶었지만.
되지 않더라..
그래서 자료를 보다 보니. BitmapSource 라는걸 사용해서 WPF의 렌더링 방식에
맞출수 있다는걸 알았다.
뭐 용어는 잘 모르겠고 암튼 이런 방식인게다. 쩝..
산넘어 산인거지..
우선 이걸 해결 하고 나니 몇가지 더 테스트 할게 남아 있다.
실제 WebBrowser는 숨겨 놓고 Image 같은데 실시간으로 뿌리면서 드로잉 해야 한다는 것과
숨겨 놓은 웹의 Mouse, Keyboard 등의 접근을 또 어떻게 처리 해야 하느냔데..
암튼 더 고민해봐야 할듯 하다.
아래 자료는 최종 코드...
-- Source --
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="1000" xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration">
<Grid>
<WebBrowser x:Name="webbrowser" Source="http://www.naver.com" Margin="0,0,437,199" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,104,307,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button>
<Image HorizontalAlignment="Right" Margin="0,178,18,21" Name="image1" Stretch="Fill" Width="399" />
</Grid>
</Window>
..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
BitmapSource bitmapsource = null;
[DllImport("user32.dll", SetLastError = true)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
public Window1()
{
InitializeComponent();
}
BitmapSource GetScreenInt()
{
Bitmap bm = new Bitmap((int)300, (int)300);
IntPtr hBitmap = bm.GetHbitmap();
Graphics g = Graphics.FromImage(bm);
PrintWindow(this.webbrowser.Handle, g.GetHdc(), 0);
g.ReleaseHdc();
g.Flush();
BitmapSource src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
src.Freeze();
bm.Dispose();
bm = null;
DeleteObject(hBitmap);
return src;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.image1.Source = null;
this.image1.Source = GetScreenInt();
}
}
}
from TBL_TEST_DB
textvalue의 값이 12345 라고 하면 123** 로 치환한다.

Prev
Rss Feed