jiing's c# collection

Monday, September 05, 2005

Screen Capture

REF:http://0rz.net/3a0Gm


新聞群組: microsoft.public.dotnet.csharp.general
寄件人: "Per" - 尋找此作者的訊息
日期: Wed, 5 Jan 2005 08:35:38 +0100
當地時間: 2005年1月5日(星期三) 下午3時35分
主旨: Screen capture
回覆作者 | 轉寄 | 列印 | 顯示個別留言 | 顯示原始檔 | 舉報濫用行為

Hi all,
This may be a newbie question, but I can't find the answer anywhere.
I want to make a capture of the screen, or a part of the screen, that
contains the main portion of the form.
Detecting the screen that contain the main portion of the form is pretty
straight forward, but how do I capture what I need in an Image object?
Another thing, how do you make the program move the mouse pointer and
simulate clicks?

Best regards


Andrew Arace 1月17日 下午10時21分 顯示選項
新聞群組: microsoft.public.dotnet.csharp.general
寄件人: "Andrew Arace" - 尋找此作者的訊息
日期: 17 Jan 2005 06:21:20 -0800
當地時間: 2005年1月17日(星期一) 下午10時21分
主旨: Re: Screen capture
回覆作者 | 轉寄 | 列印 | 顯示個別留言 | 顯示原始檔 | 舉報濫用行為

I can't help you on the mouse pointer click simulation,
but i can provide you with some screen capture code:

public class ImagingUtils {

//imports the GDI BitBlt function that enables the background
of the window
//to be captured
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);

[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static int ReleaseDC(System.IntPtr hWnd,
System.IntPtr hDC); //modified to include hWnd

///


/// Returns a full desktop screencapture
///

/// A Bitmap object capture of the entire
desktop.

public static Image GetScreenCapture() {
System.IntPtr desktopDC = GetDC(System.IntPtr.Zero);
Bitmap bm = new
Bitmap(System.Windows.Forms.SystemInformation.VirtualScreen.Width,

System.Windows.Forms.SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width,bm.Height, desktopDC, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(System.IntPtr.Zero, desktopDC);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;

}

///
/// Returns a capture of the control's current graphics.
///

/// The winforms control to capture
/// A Bitmap object of the captured graphics
public static Image
GetControlImageCapture(System.Windows.Forms.Control ctrl) {
System.IntPtr ctrlCapture = GetDC(ctrl.Handle);
Bitmap bm = new Bitmap(ctrl.Width,
ctrl.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width, bm.Height, ctrlCapture, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(ctrl.Handle, ctrlCapture);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;
}

}

Friday, August 12, 2005

.Net code to Detect When an External Drive is Mounted?

REF: http://0rz.net/530xD

Here you go. I know that this code works for CD removal/insertion. I
haven't tested it for other devices, but it really ought to work.

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Whatever {

public class HookEventArgs : EventArgs {
public int HookCode;
public IntPtr wParam;
public IntPtr lParam;

}

// Hook Types
public enum HookType : int {
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14

}

///


/// Summary description for Win32Hooks.
///

public class Win32Hook {

public delegate int HookProc(int code, IntPtr wParam, IntPtr
lParam);

protected IntPtr m_hhook = IntPtr.Zero;
protected HookProc m_filterFunc = null;
protected HookType m_hookType;

public delegate void HookEventHandler(object sender, HookEventArgs
e);

public event HookEventHandler HookInvoked;
protected void OnHookInvoked(HookEventArgs e) {
if (HookInvoked != null) {
HookInvoked(this, e);

}
}

public Win32Hook(HookType hook) {
m_hookType = hook;
m_filterFunc = new HookProc(this.CoreHookProc);
}

public Win32Hook(HookType hook, HookProc func) {
m_hookType = hook;
m_filterFunc = func;

}

public int CoreHookProc(int code, IntPtr wParam, IntPtr lParam) {

if (code < 0) {
return CallNextHookEx(m_hhook, code, wParam, lParam);

}

HookEventArgs e = new HookEventArgs();
e.HookCode = code;
e.wParam = wParam;
e.lParam = lParam;
OnHookInvoked(e);

return CallNextHookEx(m_hhook, code, wParam, lParam);

} // CoreHookProc

public void Install() {

m_hhook = SetWindowsHookEx(m_hookType, m_filterFunc, IntPtr.Zero,
(int) AppDomain.GetCurrentThreadId());

}

public void Uninstall() {
UnhookWindowsHookEx(m_hhook);

}

#region Win32 Imports
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code,
HookProc func, IntPtr hInstance, int threadID);

[DllImport("user32.dll")]
protected static extern int UnhookWindowsHookEx(IntPtr hhook);

[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook, int code,
IntPtr wParam, IntPtr lParam);
#endregion

}

public class DeviceChangeEventArgs {
public string drive;
public DirectoryInfo driveInfo;
public IntPtr eventHwnd;

}

public class DeviceChangeHook : Win32Hook {
const int WM_DEVICECHANGE = 0x0219;
private static IntPtr _forHWnd;
public static IntPtr ForHWnd {
get {
return DeviceChangeHook._forHWnd;

}

set {
DeviceChangeHook._forHWnd = value;

}
}

protected enum DbtHookActions {
DeviceArrival = 0x8000,
DeviceRemoveComplete = 0x8004

}

[StructLayout(LayoutKind.Sequential)]
protected class DevBroadcastVolume {
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
public short dbcv_flags;

}

// class cwp maps the unmanaged Win32 struct CWPSTRUCT that is
declared in
// winuser.h
// See
http://msdn.microsoft.com/libr ary/default.asp?url=/library/e n-us/winu...
[StructLayout(LayoutKind.Sequential)]
protected class cwp {
public IntPtr lParam;
public int wParam;
public ushort message;
public IntPtr hwnd;

}

public delegate void DeviceChangeEventHandler(object sender,
DeviceChangeEventArgs dce);
public event DeviceChangeEventHandler DeviceArrived;
public event DeviceChangeEventHandler DeviceRemoved;

static DeviceChangeHook() {
DeviceChangeHook._forHWnd = IntPtr.Zero;

}

public DeviceChangeHook() : base(HookType.WH_CALLWNDPROC) {
this.HookInvoked += new HookEventHandler(GetMessageHookInvoked);

}

public DeviceChangeHook(HookProc func) :
base(HookType.WH_CALLWNDPROC, func) {
this.HookInvoked += new HookEventHandler(GetMessageHookInvoked);

}

private void GetMessageHookInvoked(object sender, HookEventArgs he)
{

cwp eventMessage = new cwp();
eventMessage = (cwp) Marshal.PtrToStructure(he.lParam,
eventMessage.GetType());
if ((eventMessage.message == 0x0219) && (eventMessage.hwnd ==
DeviceChangeHook._forHWnd)) {
DbtHookActions action = (DbtHookActions) eventMessage.wParam;
DevBroadcastVolume eventInfo = new DevBroadcastVolume();
eventInfo = (DevBroadcastVolume)
Marshal.PtrToStructure(eventMessage.lParam, eventInfo.GetType());

DeviceChangeEventArgs changeArgs = new DeviceChangeEventArgs();

switch (action) {
case DbtHookActions.DeviceArrival:
changeArgs.drive =
decodeDriveLetter(eventInfo.dbcv_unitmask);
changeArgs.driveInfo = new DirectoryInfo(changeArgs.drive +
":\\");
changeArgs.eventHwnd = eventMessage.hwnd;
DeviceArrived(this, changeArgs);
break;
case DbtHookActions.DeviceRemoveComplete:
changeArgs.drive =
decodeDriveLetter(eventInfo.dbcv_unitmask);
changeArgs.driveInfo = null;
changeArgs.eventHwnd = eventMessage.hwnd;
DeviceRemoved(this, changeArgs);
break;
default:
break;

} // switch
}
} // GetMessageHookInvoked

protected string decodeDriveLetter(int unitMask) {

char driveLetter;
for (driveLetter = 'A'; driveLetter <= 'Z'; ++driveLetter) {
if ((unitMask & 0x1) == 0x1)
break;
unitMask = unitMask >> 1;

}

if (driveLetter > 'Z') // Catch any unitMask failure
driveLetter = '0';

return driveLetter.ToString();

screen capture

Ref: http://0rz.net/e70yC

I can't help you on the mouse pointer click simulation,
but i can provide you with some screen capture code:

public class ImagingUtils {

//imports the GDI BitBlt function that enables the background
of the window
//to be captured
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);

[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static int ReleaseDC(System.IntPtr hWnd,
System.IntPtr hDC); //modified to include hWnd

///


/// Returns a full desktop screencapture
///

/// A Bitmap object capture of the entire
desktop.

public static Image GetScreenCapture() {
System.IntPtr desktopDC = GetDC(System.IntPtr.Zero);
Bitmap bm = new
Bitmap(System.Windows.Forms.SystemInformation.VirtualScreen.Width,

System.Windows.Forms.SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width,bm.Height, desktopDC, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(System.IntPtr.Zero, desktopDC);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;

}

///
/// Returns a capture of the control's current graphics.
///

/// The winforms control to capture
/// A Bitmap object of the captured graphics
public static Image
GetControlImageCapture(System.Windows.Forms.Control ctrl) {
System.IntPtr ctrlCapture = GetDC(ctrl.Handle);
Bitmap bm = new Bitmap(ctrl.Width,
ctrl.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width, bm.Height, ctrlCapture, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(ctrl.Handle, ctrlCapture);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;
}

}

Sunday, May 22, 2005

Re: 取用別人的網頁

作者: chavy (chavy) 看板: C_Sharp
標題: Re: 取用別人的網頁
時間: Sun May 22 17:15:01 2005

※ 引述《ganymade (呀)》之銘言:
: 我想寫一個web application
: 用來處理一些棒球的計算問題
: 可是在那之前
: 我建資料庫需要抓很多CPBL的資料
: 例如在下面這一頁
: http://www.cpbl.com.tw/personal_Rec/
: Per_year_Hit.asp?Pno=EC1&qYear=2001&team=E01
: 縮一下網址:http://tinyurl.com/chrc4
: 因為要抓所有球員的一壘安打、二壘安打.........
: 很多很多
: 都是在這頁裡
: 那有沒有比較簡單的方法
: 看上面的那網址(沒縮的)
: 至少同一個球員,只要改Year=2001 2002
: 就可以抓了
: 不過我的問題是卡在
: 怎麼樣去抓取那個網頁裡的資料呢?
可以用System.Net.WebRequest類別來抓
詳細用法請參考SDK文件

內容抓回來之後再使用System.Text.RegularExpressions.Regex類別處理
透過RegularExpression把你需要的資料擷取出來

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.117.76.207
推 tomex:作法如po者所言,標準程序。  60.248.89.46 05/23