天天看點

C# 如何使窗體位于最底層

在C#中如何使窗體位于最底層呢?這裡要用到winapi函數,我們要通過引用dll檔案的形式來進行操作,這裡的dll檔案是自己用vc6.0編譯的,我們無法在引用裡加入我們的dll檔案,隻能通過添加現有檔案的方法,我們把dll檔案放在根目錄裡。

下面是源代碼:

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;//使用Winapi

 //Author: abrahu

namespace SetWindowBackMost

{

    public partial class Form1 : Form

    {

        const int WM_WINDOWPOSCHANGING = 0x0046;//可以在vc6.0的winuser.h檔案中找到

        public Form1()

        {

            InitializeComponent();

        }

        [DllImport("../../WindowBackDEskTop_DLL.dll")]

        protected static extern void Setlparam(int Msg, IntPtr lParam);

        protected override void WndProc(ref Message m)

        {

            Setlparam(m.Msg, m.LParam);        

            base.WndProc(ref m);

        }       

    }

}

我們在vc6.0裡将下面的代碼編譯成DLL檔案。編譯源檔案.cpp的源碼如下:

#include <windows.h>

extern "C" __declspec(dllexport) void Setlparam(UINT Msg,LPARAM lParam){

        if (Msg==WM_WINDOWPOSCHANGING)

                     ((LPWINDOWPOS)lParam)->hwndInsertAfter=HWND_BOTTOM;

}

我們在C#檔案裡的[DllImport("../../WindowBackDEskTop_DLL.dll")]因為我們把dll檔案放在的根目錄是以是這個路徑,我們可以直接把它放在bin檔案夾的Debug裡面,就可以直接寫成[DllImport("WindowBackDEskTop_DLL.dll")]

繼續閱讀