1. 程式人生 > >C# WinForm 兩個窗體之間傳送訊息

C# WinForm 兩個窗體之間傳送訊息

1、FormTest.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsTest
{
    public partial class FormTest : Form
    {
        const int BM_CLICK = 0xF5;

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);

        public FormTest()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            FormSub formSub = new FormSub();
            formSub.Show();            
            base.OnLoad(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);            
        }



        private void SendMessage(string pcMessage, string pcReceiveForm)
        {
            int WINDOW_HANDLER = FindWindow(null, pcReceiveForm);
            if (WINDOW_HANDLER != 0)
            {
                SendMessage(WINDOW_HANDLER, BM_CLICK, 0, 0);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SendMessage("娃哈哈", "子窗體");
        }
    }
}


2、FormSub.cs

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 WindowsFormsTest
{
    public partial class FormSub : Form
    {
        public FormSub()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            this.Hide();
        }

        protected override void WndProc(ref Message m)
        {
            const int BM_CLICK = 0xF5;

            if (m.Msg == BM_CLICK)
            {
                MessageBox.Show("子窗體收到事件"); 
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }
}


修改FormSub窗體的標題為“子窗體”,這裡在啟動FormTest窗體的時候啟動FormSub窗體再隱藏,用來後臺做訊息接受的程式。

相關推薦

C# WinForm 窗體之間傳送訊息

1、FormTest.cs using System; using System.Collections.Generic; using System.ComponentModel; using Sys

c# 計算日期之間相差的天數

    策劃要求有每日任務,因為對時間不瞭解,所以查了一下資料,終於完成了要求,現把程式碼記錄在下面。 算不上轉載,更算不上原創的一點想法吧         public vo

VS程式設計,C#求時間之間相差了多少天、多少小時、多少分鐘的一種方法

有時需要計算兩個時間點之間的差值,求相差了多少天,多少小時等。這裡提供了C#的一種方法。  1、使用 TimeSpan 來儲存時間差 DateTime t1 = Date

NetBeans中窗體之間如何傳值

   舉一個簡單的例子:如上圖,在jTextField1、jTextField2中各輸入一個值,點選“確定”按鈕,在child窗體的jTextField3中獲取jTextField1的值 一:通

C整數之間所有整數和問題

問題: Write a program that (1) inputs two integers (integer1 and integer 2) (2) prints sum of all integers between integer1 and integer2 (3) U

winform窗體的切換

就是點選一個窗體的button開啟一個新的窗體,舊窗體隱藏之後在關閉。點選新窗體的button回到舊窗體,新窗體隱藏之後在關閉。 可以用父子窗體實現。現在介紹另一種簡單方法: 舊窗體button程式碼

c++計算時間之間的時間間隔

// 現在的日期  c++時間是從1900年1月1日開始_time_1.tm_year = startTime / (10000000000)-1900;_time_1.tm_mon = startTime/ (100000000) % 100-1;_time_1.tm_mday = startTime/ (

C++實現字串之間的Levenshtein Distance(編輯距離)

1.什麼是Levenshtein Distance Levenshtein Distance,又稱編輯距離,指的是兩個字串之間,由一個轉換成另一個所需的最少編輯操作次數。許可的編輯操作包括將一個字元替

C#獲取日期之間的間隔

ram 開發 過程 以及 urn 操作 news .html date 在應用程序開發的過程中,很多時候涉及到時間的操作,例如時間的大小比較以及兩個時間的間隔。此文就簡單介紹下C#語言如何獲取兩個時間之前的間隔。在C#中獲取兩個時間之間的間隔需要使用到TimeSpan類,該

Visual Studio 中窗體WinForm之間相互傳值的方法

編寫WinowsForm應用程式時,實現兩個窗體之間相互傳遞值的方法其實很簡單。以下用一個例子說明:在名為FormMain主窗體執行過程中利用名為FormInfo窗體,獲取使用者輸入資訊,並將這些資訊返回給FormMain 1. FormMain和FormInfo 點選“

計算日期之間的天數差C++/java

new abs ear == 關於 ostream logs col 多少 1--Java 分析:調用java中Calendar類 int days(Date date1,Date date2){   Calendar cal = new Calendar.getInst

WinForm實現同時讓窗體有激活效果的特效實例

extern mes tex 地址 zone name static cor 語音 本文實例講述了WinForm實現同時讓兩個窗體有激活效果的特效。主要采用windows api實現一個窗體激活的時候給另外一個發消息。分享給大家供大家參考。 具體實現方法如下:using

C#實體之間相同屬性的映射

per sta pos ping nbsp info != 相同 urn public static R Mapping<R, T>(T model) { R result = Activator.CreateIn

C++ 計算任意日期之間的天數

gpo start plain efault ediff include win sel put C++寫的一個計算兩個日期之間天數的小程序: [cpp] view plain copy #include <Windows.h>

C#實踐問題:如何在LINQ to Entities 查詢中,計算日期之間相差的天數?

在LINQ to Entities 查詢中,使用常規日期格式轉換函式Convert.ToDateTime()以及日期比較函式TimeSpan.Subtract().Days,都很麻煩而且會出錯:LINQ to Entities不能識別方法。。。 那麼要想在LINQ to Entities 查詢中

c# 子類改變父類資訊的問題,窗體類傳值的問題

固定一個窗體在另一個窗體中首先需要設定父窗體IsMdiContainer屬性為True,然後在Load事件中加入以下語句: NewContact newConFrm = new NewContact(this); newConFrm.NewGroup = CreateGroup;

基於專案 之間的RabbitMQ 傳送和接收,並呼叫郵件介面,傳送郵件

專案原始碼 下載 第一個專案:qucik4j 下載地址:https://github.com/ZhangHLong/quick4j 作用: 定時監控指定服務埠是否異常,發生異常,會發送MQ訊息 通知email-server服務。

C#中求日期之間的天數、小時數、分鐘數

private void button1_Click(object sender, EventArgs e)        {            DateTime d1 = new DateTime(2004, 1, 1, 01, 36, 05);           

C#中Form視窗之間的傳值(父->子)(子->父)

首先定義兩個Form,一個為Form1,一個為Form2,其中Form1作為父視窗,Form2作為子視窗 1、父視窗傳值給子視窗       Form1中程式碼:         public Form1()         {             Initialize

Linux:使用多執行緒程式設計和訊息佇列,實現程序之間的聊天

思路: 一個檔案:建立一個執行緒和主函式,或者建立兩個執行緒主函式呼叫(我用這種)。 建立兩個訊息佇列, 一共兩個檔案,兩個佇列,四個程序 a.c    一個程序寫(訊息型別為1)   ---->>佇列     一個程序讀(訊息型別為2) b.c   一