国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# 中如何定義和接收消息?

2024-07-21 02:20:09
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。c# 中如何定義和接收消息?

wason

業余學習成果: 終于把自定義消息給搞定,好事多多分享!

在c#中目前我還沒有找到發送消息的類成員函數,所以只能采用通過調用win 32 api 的 sendmessage() 函數實現。由于 sendmessage的參數中需要得到窗體的句柄(handler) ,所以又要調用另一個api findwindow(), 兩者配合使用,達到在不同窗體之間的消息發送和接收功能。

另外一個要點是,需要通過重寫(override) 窗體的 defwndproc() 過程來接收自定義的消息。defwndproc 的重寫:

protected override void defwndproc(ref system.windows.forms.message m)
{
switch(m.msg)
{
case ...:
break;
default:
base.defwndproc(ref m);
break;
}
}


下面是我的c#實踐例程。
------------------------------------
/////////////////////////////////////////
///file name: note.cs
///
public class note
{
//聲明 api 函數

[dllimport("user32.dll",entrypoint="sendmessage")]
private static extern int sendmessage(
int hwnd, // handle to destination window
int msg, // message
int wparam, // first message parameter
int lparam // second message parameter
);
[dllimport("user32.dll",entrypoint="findwindow")]
private static extern int findwindow(string lpclassname,string
lpwindowname);
//定義消息常數
public const int user = 0x500;
public const int test = user + 1;


//向窗體發送消息的函數

private void sendmsgtomainform(int msg)
{
int window_handler = findwindow(null,@"note pad");
if(window_handler == 0)
{
throw new exception("could not find main window!");
}
sendmessage(window_handler,msg,100,200);
}
}


/////////////////////////////////////////
/// file name : form1.cs
/// 接收消息的窗體
///

public class form1 : system.windows.forms.form
{
public form1()
{
//
// required for windows form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
//
}
/// 重寫窗體的消息處理函數
protected override void defwndproc(ref system.windows.forms.message m)
{
switch(m.msg)
{
//接收自定義消息 user,并顯示其參數
case note.user:
string message = string.format ("received message!
parameters are :{0},{1}",m.wparam ,m.lparam);
messagebox.show (message);
break;
default:
base.defwndproc(ref m);
break;
}
//console.writeline(m.lparam);
}

--

wilson wei

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江都市| 衡南县| 冕宁县| 康平县| 化德县| 新密市| 海兴县| 九江市| 富民县| 交城县| 怀安县| 正宁县| 河东区| 凤阳县| 汽车| 祥云县| 湘阴县| 松溪县| 岳池县| 凉城县| 当阳市| 麻阳| 蓬莱市| 乌鲁木齐县| 蓝田县| 孝昌县| 库车县| 乌鲁木齐县| 稷山县| 临泉县| 临湘市| 内乡县| 东阳市| 安西县| 黄冈市| 台江县| 山东| 五台县| 微山县| 赤壁市| 荥阳市|