2009年5月24日

[C#] - 使用熱鍵(HotKey)

這是一個設定應用程式的熱鍵簡單的model
利用win 32 API來達到熱鍵的效果
以下是程式的code,註解了幾個網站,供參考用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//使用DLLImport需宣告

namespace hotkey
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class HotKey//製作一個hotkey的class
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
//設定registerhotkey
//相關資料http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx

[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
//設定Unregisterhotkey
//相關資料http://msdn.microsoft.com/en-us/library/ms646327(VS.85).aspx

[Flags()]//設定
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
protected override void WndProc(ref Message m)
{
//驗證用
//相關資料http://msdn.microsoft.com/zh-tw/library/dd229215.aspx
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100:
this.button1.PerformClick();
//當case=100時,執行button1的內容
MessageBox.Show("100");
break;
case 101:
this.button2.PerformClick();
MessageBox.Show("101");
break;
} break;
}
base.WndProc(ref m);
}
private void Form1_Activated(object sender, EventArgs e)
{
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);
HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Shift, Keys.D);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("123");
}

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("123456");
}


private void Form1_Leave(object sender, EventArgs e)
{
//當form leave時,執行UnregisterHotKey指令
HotKey.UnregisterHotKey(Handle, 100);
HotKey.UnregisterHotKey(Handle, 101);
}
}
}


最後感謝kloer的大力幫忙,解釋了一些不懂的地方...

再附上幾個網址
http://blog.blueshop.com.tw/hammerchou/archive/2006/10/16/42432.aspx
http://msdn.microsoft.com/en-us/library/ms646279(VS.85).aspx

若有錯誤請幫忙勘正..3Q
完畢收工....

2009年5月13日

[C#] - try...catch簡介

try...catch的用意就是試試看,主要的功能是例外處理而這也是初學者應該要列為首要學習的事情之一

try...catch主要用的地方是用在處理可能會發生例外的位置上,

廢話不多說,下面有一個範例
try
{
string sqlstr = "SELECT account FROM [user] where account='" + account.Text + "' and password='" + pwd.Text + "';";
OleDbConnection con = basic_class.access_con();
OleDbDataReader rd = basic_class.sqlread(sqlstr);
if (rd.Read())
{
MessageBox.Show(rd["account"].ToString());
}
else
{
MessageBox.Show("帳號或密碼錯誤");
}
}
catch (Exception ex)
{
MessageBox.Show("與資料庫連線失敗" + ex.ToString());
}


簡單說明一下這段code,這段是在寫帳號密碼認證的code,

會先取得資料庫連線,這個時候,try就會展現出他的功用哩,

因為每次的連線不一定是成功的,所以必須要用try將連線的code包在裡面

假使連線出現錯誤,則由catch的code來執行例外處理,將相關的錯誤訊息秀出來,

try..catch小妙用是可以拿來當作簡單的偵錯用,是不賴的.

接下來來談談使用try..catch好不好?

大家一定會想try..catch是用來處理例外的,想必一定要花上一些時間,

對於程式執行將會產生速度上的影響,

在這邊分析一下:

1.確定不會出現try..catch則不使用try..catch
(這樣就不會有效能上的疑慮了)

2.使用的code可能會發生例外,則可以考慮使用try..catch
(如果不用造成程式當掉,這樣還談什麼效能呢...)

3.測試用,也就是刻意的捕捉出現的例外,EX:sql交易,這樣的話則必需要有try..catch的加入
(這樣不僅能找出問題,又能確保程式執行不易出錯)


P.S.上述資料有些是參考web上的說法,做了一點整理,再搭上自己寫code的一些經驗,
有錯請勘正,謝謝觀賞,收工...XD