2010年9月29日 星期三

[C#] 監控USB插拔

       
        // 常數宣告

        //USB
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;
        public const int DBT_CONFIGCHANGED = 0x0018;
        public const int DBT_CUSTOMEVENT = 0x8006;
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;
        public const int DBT_DEVNODES_CHANGED = 0x0007;
        public const int DBT_QUERYCHANGECONFIG = 0x0017;
        public const int DBT_USERDEFINED = 0xFFFF;
        //雙擊滑鼠左鍵
        public const int WM_MOUSE_DOUBLE_CHICK = 0x0203;



        //USB 監控
        protected override  void WndProc(ref System.Windows.Forms.Message m)
        {
        
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case WM_DEVICECHANGE:
                            break;
                        case DBT_DEVICEARRIVAL://USB IN
                            DriveInfo[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    MessageBox.Show("U盤已插入,盤符為:" + drive.Name.ToString());
                                    break;
                                }
                            }
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            break;
                        case DBT_CONFIGCHANGED:
                            break;
                        case DBT_CUSTOMEVENT:
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //USB OUT
                            MessageBox.Show("You Flash ....Bye");
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            break;
                        case DBT_DEVNODES_CHANGED:
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            break;
                        case DBT_USERDEFINED:
                            break;
                        default:
                            break;
                    }
                }
            
                //Mouse
                if (m.Msg == WM_MOUSE_DOUBLE_CHICK)
                {
                    MessageBox.Show("別在雙擊滑鼠左鍵了!");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }

2010年9月28日 星期二

[C#] enum 列舉 補充 - 與 Switch 結合用法

1.) 先宣告 enum

        enum enum_FUNCTION
        {
            CES,
            AOI,
            FET,
            FVT,
            OQC,
            PAK
        }

2.) 然後  new 起來

enum_FUNCTION FC_Function =new enum_FUNCTION();

3.) 然後在 Visual Studio 裡面,按右鍵 選擇 插入程式碼片段  選擇 switch ,
     這時候會顯示下面畫面。

  switch (switch_on)
 {
               default:
 }


4.) 在 switch_on 的地方 ,打入 New 起來的 enum 就會自動展開喔。
     
           switch (FC_Function)
           {
               case enum_FUNCTION.CES:
                   break;
               case enum_FUNCTION.AOI:
                   break;
               case enum_FUNCTION.FET:
                   break;
               case enum_FUNCTION.FVT:
                   break;
               case enum_FUNCTION.OQC:
                   break;
               case enum_FUNCTION.PAK:
                   break;
               default:
                   break;
           }

[C#] 播放音效

//播放音效
        public void PLAY_WAV_MUSIC(string Path)
        {
            if (Path.Length == 0 || Path.IndexOf(".wav") == -1)
            {
                return;
            }
            System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
            sp.SoundLocation = Path;
            sp.Play();
        }

2010年9月20日 星期一

[WCF] 使用說明

第一次使用,做個紀錄

在 IService1.cs 裡面
    public interface IService1
    {
        [OperationContract]
        string KM_GetData(int value);
        [OperationContract]
        string KM_EMP(string ID);
    }

在 Service1.cs  裡面
    public class Service1 : IService1
    {
        public string KM_GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        public string KM_EMP(string ID)
        {
            string a = ID.ToString().Trim();
            return "HI, IAM KUoming -"+a;
        }
    }

發佈到 IIS 以後,就可以使用了,在 使用專案裡面,先加入 服務參考

        private void button1_Click(object sender, EventArgs e)
        {
            KM_WCF.Service1Client WCF = new WindowsFormsApplication1.KM_WCF.Service1Client();
            MessageBox.Show(WCF.KM_GetData(9999));
            MessageBox.Show(WCF.KM_EMP("zxcvbnm,"));
            WCF.Close();
        }

2010年9月15日 星期三

[Silverlight] 畫面佈局

  • Grid - 基本上是一種表格,用以將物件放置在資料列和資料行中。
  • StackPanel - 用以將物件放置在另一個物件的旁邊,或是放在另一個物件的上方。
  • Canvas - 用於絕對位置 (而且從 Silverlight 1.0 開始就未變更)
    <Grid x:Name="LayoutRoot" Background="Black" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="450"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="500"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
       
        <Canvas Grid.Column="0" Grid.Row="0">
            <TextBox x:Name="mytextbox" FontSize="20" Text="Hello World" Height="120"
              Width="200" Canvas.Left="50" Canvas.Top="10"></TextBox>
            <Button Click="Button_Click" Content="按我!" Height="120" Width="200"
              FontSize="26" Canvas.Left="50" Canvas.Top="150"></Button>
            <ToggleButton x:Name="bt1" Content="QQ" Height="120" Width="200" Canvas.Left="50"
              Canvas.Top="300" Click="bt1_Click" IsThreeState="True"></ToggleButton>
            <ScrollViewer Background="Blue" Height="120" Width="300"    
            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible"
            Canvas.Left="280" Canvas.Top="10">
            <TextBlock TextWrapping="Wrap" Text="1234567890 嘿嘿!!          測試!!" 
           FontSize="20" FontWeight="light" HorizontalAlignment="Right" IsHitTestVisible="True"
           LineStackingStrategy="MaxHeight"></TextBlock>
            </ScrollViewer>
            <Rectangle Fill="AliceBlue" Height="60" Width="60" Canvas.Left="280"
            Canvas.Top="150"></Rectangle>
            <Rectangle Fill="BurlyWood" Height="60" Width="60" Canvas.Left="360"
            Canvas.Top="200"></Rectangle>
        </Canvas>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
            <TextBlock Text="UserName:" VerticalAlignment="Center" Foreground="White"></TextBlock>
            <TextBox Width="200" Height="30" HorizontalAlignment="Left"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1">
            <TextBlock Text="Password:" VerticalAlignment="Center" Foreground="White"></TextBlock>
            <TextBox Width="200" Height="30" HorizontalAlignment="Left"></TextBox>
        </StackPanel>
        <Canvas Grid.Column="1" Grid.Row="0">
            <Button x:Name="bt2" Click="bt2_Click" Content="全螢幕" Height="30" Width="150" Canvas.Left="50" Canvas.Top="10"></Button>
        </Canvas>
    </Grid>

[C#] 查詢 IP 資訊

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            IPHostEntry hostInfo;
            hostInfo = Dns.Resolve(textBox1.Text);
            listBox1.Items.Add("Host Name:" + hostInfo.HostName.ToString());

            foreach (IPAddress ipaddr in hostInfo.AddressList)
            {
                listBox1.Items.Add("IP Address:" + ipaddr.ToString());
            }

            foreach (String alias in hostInfo.Aliases)
            {
                listBox1.Items.Add("Alias:" + alias.ToString());
            }

        }

寫真:

2010年9月14日 星期二

[Auto Macro Recorder] Auto Macro Recorder 基本用法

最近被一些 「重複」的事情搞到很煩,上網找了好幾套 可以 利用先錄製好的巨集
然後重複執行,下面這套還蠻方便的。

怕忘記,先放一些上來註解一下

//Auto Macro Recoder , 副檔名用 scp

//註解
/* xxxxxxx

//延遲(ms)
delay(0)

//按按鍵(KEY)
KB_CLK(win)

//移動滑鼠到(x,y)
M_MV(514,431)

//開啟檔案(檔按名稱 文字檔檔名) , 可以不要檔名
RUN(notepad tp1.txt)

//開啟檔案
OPEN(notepad)

//功能組合按鍵 shift - p
KD(shift)
delay(100)
KD(p)
delay(100)
KU(p)
delay(100)
KU(shift)
delay(100)

//輸入 string
Input(today is )

//得到日期 並輸入
GetDate()
delay(1000)
Input(VARSTR)
delay(1000)

//得到時間
GetTime()

//從暫存貼上
paste()

2010年9月10日 星期五

[SQL] 字串多行..

剛剛爬文時,突然看到,發現 我也寫了 2 年多的 C# ,也不知道可以多行字串。

原本寫法:
SQL="SELECT "+
"* "+
"FROM TABLE ";

發現只要加上 @
SQL = @"SELECT
*
FROM TABLE";

真是太神奇了!

[C#] HASHTABLE

宣告
System.Collections.Hashtable HT = new System.Collections.Hashtable();


資料
HT.Add(101, "CM4021-1-1-L");
HT.Add(102, "CM4021-1-1-R");
HT.Add(103, "CM4021-1-2-L");
HT.Add(104, "CM4021-1-2-R");


實做
string SLOT = "";
foreach (DictionaryEntry KK in HT)
{
if (KK.Value.ToString() == MC + "-" + L1[0] + "-" + L1[2] + "-" + L1[5].Trim())
{
SLOT = KK.Key.ToString();
}
}

排序
//Sort
ArrayList sorter = new ArrayList();
sorter.AddRange(PDLINE1.Keys);
sorter.Sort();

[SQL] 尋找前100筆資料

  • 在 SQL 裡面,找前 100 筆資料:
SELECT TOP 100 * FROM TABLE......

  • 在 ORACLE 是這樣
SELECT * FROM TABLE WHERE ROWNUM < 100 ....

2010年9月9日 星期四

[SQL] 查詢TOP 100 最差的指令

這是從書上看來的,主要 是要查詢 SQL Server 最花費時間的指令, 用來做效率改善的參考。 SELECT TOP 20 TOTAL_WORKER_TIME,PH.text FROM sys.dm_exec_query_stats AS QS CROSS APPLY sys.dm_exec_sql_text(QS.PLAN_HANDLE) AS PH ORDER BY 1 DESC; GO