方法1:
//直接砍 Process
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
if (p.Length > 1)
{
for (int i = 0; i < p.Length; i++)
{
p[i].Kill();
}
}
方法2:
//宣告
[DllImport("kernel32.dll", EntryPoint = "CreateMutexA", SetLastError = true)]
public static extern IntPtr CreateMutex(IntPtr securityAttributes, bool initialOwner, string name);
[DllImport("kernel32.dll", EntryPoint = "ReleaseMutex", SetLastError = true)]
public static extern IntPtr ReleaseMutex(IntPtr handle);
public static bool IsMutexDataExist(string name)
{
bool returnValue = false;
int ERROR_ALREADY_EXISTS = 183;
IntPtr handle = CreateMutex(IntPtr.Zero, true, name);
if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
{
returnValue = true;
}
ReleaseMutex(handle);
return returnValue;
}
//使用方法
if (IsMutexDataExist("BetWorldAuthServer") == true)
{
//MessageBox.Show("已經在執行中", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
Environment.Exit(0);
}
2012年1月18日 星期三
2011年12月21日 星期三
[C#] 控制 WinForm 顯示於延伸視窗(延伸顯示器)、雙螢幕
有個需求是 必須要指定某個 WinForm 開啟於 延伸視窗
那個該如何讓 程式 開啟的時候 直接顯示於 延伸顯示器呢?
請在 Form1.Designer.cs 內追加 即可。
DesktopLocation 裡面的 X 的值 請務必大於 本機顯示器的 X 解析度.
標籤:
C#
2011年12月20日 星期二
[C#] 使用 ZedGraph Chart 來製作圖表(開源控件)
由於工作單位裡面老舊電腦比較多(還有一些是 Windows 2000)
所以 Net.Framework 只能使用到 2.0 版本。
需求是 必須產生即時線圖,比較過幾種開源控件,最終採用 ZedGraph
完整專案下載
使用效果
//首先要加入參考 ZedGraph
GraphPane myPane = null;
myPane = zg1.GraphPane;
myPane.CurveList.Clear();
myPane.GraphObjList.Clear();
//資料來源 處理
int[] x = { 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
int[] y = { 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70};
//資料填充
PointPairList list = new PointPairList();
list.Clear();
for (int i = 0; i < x.Length; i++)
{
list.Add(x[i], y[i]);
}
//圖形建立
BarItem myCurve = myPane.AddBar("作業 OK品 實積", list, Color.Blue); //建立長條圖
//BarItem.CreateBarLabels(myPane, false, "f0"); //每個長條圖 都顯示個別數據
//介面屬性處理
myPane.Title.IsVisible = false; //主標題是否 顯示
//myPane.Title.Text = "Emp"; //主標題
myPane.Title.FontSpec.FontColor = Color.Green;
myPane.XAxis.Title.IsVisible = false; //X軸 是否顯示 title
//myPane.XAxis.Title.Text = "Hr"; //X 軸 Title
//myPane.XAxis.Title.FontSpec.Size = 40; //X 軸 Title 文字Size
string[] xx = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
myPane.XAxis.Scale.TextLabels = xx; //X軸刻度資料來源
myPane.XAxis.Type = AxisType.Text; //X軸刻度資料型態
myPane.XAxis.Scale.FontSpec.Size = 25;
//myPane.XAxis.MajorGrid.IsVisible = true; //顯示格點
//myPane.XAxis.MajorGrid.Color = Color.Pink; //格點顏色
myPane.YAxis.Title.Text = "Pcs"; //Y 軸的 Title
myPane.YAxis.Title.FontSpec.Size = 25;
myPane.YAxis.Scale.FontSpec.Size = 25;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.Color = Color.Black;
//myPane.YAxis.Scale.MajorStep = 10; //Y軸資料 間隔
//myPane.YAxis.Scale.Max = 120; //Y軸 最大值
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, -45F); //Chart 填色
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), -45F); //外部 填色
myPane.Legend.Position = ZedGraph.LegendPos.InsideTopRight; //圖例放置位置
myPane.Legend.FontSpec.Size = 20;
//畫面產生
zg1.AxisChange();
//重要! 若資料有變更時 畫面重整
zg1.RestoreScale(myPane);
#相關網站:
1.)Think專欄 介紹一些常用函數。
2.)oayx 一些使用說明及資源。
3.)Codeproject 開源Project
所以 Net.Framework 只能使用到 2.0 版本。
需求是 必須產生即時線圖,比較過幾種開源控件,最終採用 ZedGraph
完整專案下載
使用效果
//首先要加入參考 ZedGraph
GraphPane myPane = null;
myPane = zg1.GraphPane;
myPane.CurveList.Clear();
myPane.GraphObjList.Clear();
//資料來源 處理
int[] x = { 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
int[] y = { 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70};
//資料填充
PointPairList list = new PointPairList();
list.Clear();
for (int i = 0; i < x.Length; i++)
{
list.Add(x[i], y[i]);
}
//圖形建立
BarItem myCurve = myPane.AddBar("作業 OK品 實積", list, Color.Blue); //建立長條圖
//BarItem.CreateBarLabels(myPane, false, "f0"); //每個長條圖 都顯示個別數據
//介面屬性處理
myPane.Title.IsVisible = false; //主標題是否 顯示
//myPane.Title.Text = "Emp"; //主標題
myPane.Title.FontSpec.FontColor = Color.Green;
myPane.XAxis.Title.IsVisible = false; //X軸 是否顯示 title
//myPane.XAxis.Title.Text = "Hr"; //X 軸 Title
//myPane.XAxis.Title.FontSpec.Size = 40; //X 軸 Title 文字Size
string[] xx = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
myPane.XAxis.Scale.TextLabels = xx; //X軸刻度資料來源
myPane.XAxis.Type = AxisType.Text; //X軸刻度資料型態
myPane.XAxis.Scale.FontSpec.Size = 25;
//myPane.XAxis.MajorGrid.IsVisible = true; //顯示格點
//myPane.XAxis.MajorGrid.Color = Color.Pink; //格點顏色
myPane.YAxis.Title.Text = "Pcs"; //Y 軸的 Title
myPane.YAxis.Title.FontSpec.Size = 25;
myPane.YAxis.Scale.FontSpec.Size = 25;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.Color = Color.Black;
//myPane.YAxis.Scale.MajorStep = 10; //Y軸資料 間隔
//myPane.YAxis.Scale.Max = 120; //Y軸 最大值
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, -45F); //Chart 填色
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), -45F); //外部 填色
myPane.Legend.Position = ZedGraph.LegendPos.InsideTopRight; //圖例放置位置
myPane.Legend.FontSpec.Size = 20;
//畫面產生
zg1.AxisChange();
//重要! 若資料有變更時 畫面重整
zg1.RestoreScale(myPane);
#相關網站:
1.)Think專欄 介紹一些常用函數。
2.)oayx 一些使用說明及資源。
3.)Codeproject 開源Project
標籤:
C#
[C#] 取得電腦名稱、變更電腦名稱、重新開機
因為要管理的電腦太多,雖然已經使用 IP、Terminal 來管理
但是還是常常找不到 電腦再哪裡? 所以準備使用 電腦名稱來管理。
完整原始碼下載
//取得電腦名稱
System.Windows.Forms.SystemInformation.ComputerName
//變更電腦名稱
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);
[DllImport("kernel32.dll")]
static extern bool SetComputerNameEx(_COMPUTER_NAME_FORMAT iType, string lpComputerName);
enum _COMPUTER_NAME_FORMAT
{
ComputerNameNetBIOS,
ComputerNameDnsHostname,
ComputerNameDnsDomain,
ComputerNameDnsFullyQualified,
ComputerNamePhysicalNetBIOS,
ComputerNamePhysicalDnsHostname,
ComputerNamePhysicalDnsDomain,
ComputerNamePhysicalDnsFullyQualified,
ComputerNameMax
};
private void button1_Click(object sender, EventArgs e)
{
string MachineName = txt_New_PCName.Text.Trim();
if (MachineName.Length == 0)
{
button1.Text = "NG";
return;
}
bool succeeded = SetComputerName(MachineName);
bool succeeded2 = SetComputerNameEx(_COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, MachineName);
if (succeeded && succeeded2)
{
button1.Text = "OK,請重開機!";
btn_Restrate.Visible = true;
}
else
{
button1.Text="NG";
}
}
//重新開機
private void btn_Restrate_Click(object sender, EventArgs e)
{
try
{
ManagementClass mmc = new ManagementClass("Win32_OperatingSystem");
mmc.Scope.Options.EnablePrivileges = true; //取得作業系統使用者權限
foreach (ManagementObject mo in mmc.GetInstances())
{
mo.InvokeMethod("Reboot", null, null); //呼叫一般重新啟動
}
mmc.Dispose();
}
catch (Exception Q)
{
MessageBox.Show(Q.Message);
}
}
但是還是常常找不到 電腦再哪裡? 所以準備使用 電腦名稱來管理。
完整原始碼下載
//取得電腦名稱
System.Windows.Forms.SystemInformation.ComputerName
//變更電腦名稱
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);
[DllImport("kernel32.dll")]
static extern bool SetComputerNameEx(_COMPUTER_NAME_FORMAT iType, string lpComputerName);
enum _COMPUTER_NAME_FORMAT
{
ComputerNameNetBIOS,
ComputerNameDnsHostname,
ComputerNameDnsDomain,
ComputerNameDnsFullyQualified,
ComputerNamePhysicalNetBIOS,
ComputerNamePhysicalDnsHostname,
ComputerNamePhysicalDnsDomain,
ComputerNamePhysicalDnsFullyQualified,
ComputerNameMax
};
private void button1_Click(object sender, EventArgs e)
{
string MachineName = txt_New_PCName.Text.Trim();
if (MachineName.Length == 0)
{
button1.Text = "NG";
return;
}
bool succeeded = SetComputerName(MachineName);
bool succeeded2 = SetComputerNameEx(_COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, MachineName);
if (succeeded && succeeded2)
{
button1.Text = "OK,請重開機!";
btn_Restrate.Visible = true;
}
else
{
button1.Text="NG";
}
}
//重新開機
private void btn_Restrate_Click(object sender, EventArgs e)
{
try
{
ManagementClass mmc = new ManagementClass("Win32_OperatingSystem");
mmc.Scope.Options.EnablePrivileges = true; //取得作業系統使用者權限
foreach (ManagementObject mo in mmc.GetInstances())
{
mo.InvokeMethod("Reboot", null, null); //呼叫一般重新啟動
}
mmc.Dispose();
}
catch (Exception Q)
{
MessageBox.Show(Q.Message);
}
}
標籤:
C#
2011年12月1日 星期四
[Other] 有趣的走迷宮數學目 ...
轉貼自 G+ 老蒙
一個數學題目,從 2011 進去 ,2012 出來
規則:可以轉圈、可以走重複走過的路,唯一的限制條件,不可以 倒著 回頭走..
找到答案了嗎?
答案:
2011 +7 ÷2 +7 ÷2 +7 -5 ×3 ÷2 +7 ÷2 +7 ×3 -5 ÷2 +7 ÷2 +7 -5 ×3 -5 ×3 ÷2 +7 -5 ×3 ÷2 +7 -5 = 2012
這個變態題目 據說 是來自 Holiday Puzzles 2011 ,作者是 Erich Friedman
一個數學題目,從 2011 進去 ,2012 出來
規則:可以轉圈、可以走重複走過的路,唯一的限制條件,不可以 倒著 回頭走..
找到答案了嗎?
答案:
2011 +7 ÷2 +7 ÷2 +7 -5 ×3 ÷2 +7 ÷2 +7 ×3 -5 ÷2 +7 ÷2 +7 -5 ×3 -5 ×3 ÷2 +7 -5 ×3 ÷2 +7 -5 = 2012
這個變態題目 據說 是來自 Holiday Puzzles 2011 ,作者是 Erich Friedman
標籤:
Other
訂閱:
文章 (Atom)




