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
訂閱:
文章 (Atom)