DataGridView 隨視窗大小變動
將DataGridView放入Form中,調整Dock(選中間那個) = Fill即可。
DataGridView 列的Header加入文字
要讓Row的Header加入文字,只要輸入
DataGridView1.Rows[第幾列].HeaderCell.Value = 文字 即可。
隱藏行,列
DataGridView.Columns[第幾行].Visble = false; //行
DataGridView.Rows[第幾列].Visble = false; //列
小技巧,但有大功用喔!
End
2014年2月27日 星期四
[C#/win32 API] SendMessage() 與 PostMessage()
網路上比較少SendMessage()跟PostMessage資料
先給SendMessage範例
SendMessage會等到接收端check後才會繼續執行,而PostMessage則是丟入訊息queue,繼續做。
以下範例都要先加入 using System.Runtime.InteropServices;
SendMessage A端(控制端):
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.Runtime.InteropServices;
namespace TestA
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
uint MSG_SHOW = RegisterWindowMessage("Show Message");
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
IntPtr form_name = FindWindow(null, "要控制的視窗元件名稱(text,不是name)");//找B的IntPtr 用來代表指標或控制代碼
if (form_name != IntPtr.Zero)
{
try
{
int iNum = 9527;
SendMessage(form_name, MSG_SHOW, iNum, IntPtr.Zero);
}
catch (Exception)
{
}
}
}
}
}
PostMessage A端(呼叫端):
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.Runtime.InteropServices;
namespace postmessager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private void post()
{
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr ptr = FindWindow(null, "post messager B");
if (ptr != IntPtr.Zero)
{
int msg = 9527;
PostMessage(ptr, msg, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
先給SendMessage範例
以下範例都要先加入 using System.Runtime.InteropServices;
SendMessage A端(控制端):
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.Runtime.InteropServices;
namespace TestA
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
uint MSG_SHOW = RegisterWindowMessage("Show Message");
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
IntPtr form_name = FindWindow(null, "要控制的視窗元件名稱(text,不是name)");//找B的IntPtr 用來代表指標或控制代碼
if (form_name != IntPtr.Zero)
{
try
{
int iNum = 9527;
SendMessage(form_name, MSG_SHOW, iNum, IntPtr.Zero);
}
catch (Exception)
{
}
}
}
}
}
SendMessage B端(接收端):
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.Runtime.InteropServices;
namespace TestB
{
public partial class TestB_1 : Form
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
uint MSG_SHOW = RegisterWindowMessage("Show Message");
public TestB_1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == MSG_SHOW)
{
label1.Text = (string)m.WParam.ToString();
}
base.WndProc(ref m);
}
}
}
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.Runtime.InteropServices;
namespace postmessager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private void post()
{
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr ptr = FindWindow(null, "post messager B");
if (ptr != IntPtr.Zero)
{
int msg = 9527;
PostMessage(ptr, msg, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
PostMessage B端(接收端):
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.Runtime.InteropServices;
namespace PostmessagerB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 9527)//m.Msg == MSG_SHOW
{
//label1.Text = (string)m.WParam.ToString();
MessageBox.Show("9527");
}
base.WndProc(ref m);
}
}
}
至於為何可以使用SendMessage(),PostMessage(),FindWindow(),RegisterWindowMessage(),都是要先
import user32.dll (也就是[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 後面為參數)
然後接收端使用WndProc (WindowProcess,我個人理解縮寫函意)去接收。
大部分程式碼也是參考網路上的,但大家都抄來抄去,而且我有修改程式碼,所以就不註明出處了(因為也不知道原始是誰的),當然我的版本是比較精簡化。
End
End
2014年2月19日 星期三
[C#/winform] 主Form / 副 Form 如何製作+連結2個Form
在VS2012裡面按 專案-> 加入windows form
你設定的檔名ex : form2
回到主Form,在要顯示的地方加入
Form2 frm2(這名稱可以改) = new Form2();
frm2.showdialog(this); //show 副form
this.show(); //回到主form
即可。
你設定的檔名ex : form2
回到主Form,在要顯示的地方加入
Form2 frm2(這名稱可以改) = new Form2();
frm2.showdialog(this); //show 副form
this.show(); //回到主form
即可。
[C#/Error] 不一致的存取範圍: 欄位型別 ...... 比欄位 ..... 存取範圍低
通常初學者寫Class不會特別宣告型別
但如果你宣告
public static Class名稱;
public Form
{
....
....
}
Class 名稱
{
......
}
這樣就有問題了!! 因為基本設定 Class沒加任何型別 = internal(內部可用)
Public > internal 所以 造成 "不一致的存取範圍: 欄位型別 ...... 比欄位 ..... 存取範圍低"的Error!
public static Class名稱;
public Form
{
....
....
}
public Class 名稱
{
......
}
這樣才對
End
但如果你宣告
public static Class名稱;
public Form
{
....
....
}
Class 名稱
{
......
}
這樣就有問題了!! 因為基本設定 Class沒加任何型別 = internal(內部可用)
Public > internal 所以 造成 "不一致的存取範圍: 欄位型別 ...... 比欄位 ..... 存取範圍低"的Error!
public static Class名稱;
public Form
{
....
....
}
public Class 名稱
{
......
}
這樣才對
End
[C#] List的基本用法
在寫程式常常會遇到要動態陣列,C#也很貼心的做了一個功能----泛型List<T>
但我想主要不是用在這邊....沒關係,還是講解一下List的用法
一開始一定就是
List<任意型態> 名稱 = new List<跟前面一樣的任意型態>(); //new給它一個空間
然後就可以在迴圈裡面丟給它值(它可以任意變動長度)
丟值的方法為: 名稱.Add(值);
如果要從中取出值就跟陣列一樣
名稱[0] (陣列位置可以改,這邊只是範例)
如果要長度 :名稱.Count 就會跟平常的.Length一樣囉!
範例:
List<byte> modbus = new List<byte>();
for (int i = 0; i < 30; i++)
{
if (Modbus_save[i] == true) //這邊只是設一個條件而已
{
modbus.Add(Convert.ToByte(i)); //把i轉byte型態,存入List<T>
}
}
End
但我想主要不是用在這邊....沒關係,還是講解一下List的用法
一開始一定就是
List<任意型態> 名稱 = new List<跟前面一樣的任意型態>(); //new給它一個空間
然後就可以在迴圈裡面丟給它值(它可以任意變動長度)
丟值的方法為: 名稱.Add(值);
如果要從中取出值就跟陣列一樣
名稱[0] (陣列位置可以改,這邊只是範例)
如果要長度 :名稱.Count 就會跟平常的.Length一樣囉!
範例:
List<byte> modbus = new List<byte>();
for (int i = 0; i < 30; i++)
{
if (Modbus_save[i] == true) //這邊只是設一個條件而已
{
modbus.Add(Convert.ToByte(i)); //把i轉byte型態,存入List<T>
}
}
End
訂閱:
文章 (Atom)