如何将c#编写的标准的dll,用于vb代码中

2024-11-26 14:55:14
推荐回答(2个)
回答1:

 1.首先在VB中选择ACTIVX DLL工程类伏镇氏型。如旅简图:

  

  2.然后修改工程名,class文件名,添加2个函数,如缺散下图:

  

  方便copy,代码如下:

  Public Function FncTestDLLForDotNet(instr As String) As Integer

  FncTestDLLForDotNet = 9

  End Function

  Public Function FncTestDLLForDotNet2(instr As String) As Integer

  FncTestDLLForDotNet2 = 99

  End Function

  3.然后从文件菜单中选择生成TestDLLProject1.dll,即可生成DLL。

  下面为第二部分:

  1.打开vs2005,选用语言c#,工程类型为一个window application即可。

  在form上添加一个按钮。如下图:

  

我加的: 把TestDLLProject1.dll拷贝到System32文件夹下

  2.在解决方案资源管理器中,右键点引用,在弹出的对话框中选择浏览,找到System32里的TestDLLProject1.dll,选中它,点确定。即可加入到引用中。如下图:

  

  添加完后,会看到在引用中已经增加了对这个dll的引用,如图:

  

  3,最后一步了编写按钮的代码,来调用这个vb的dll中的方法。

  双击form上的button,在button的click事件中加入如下代码:

  TestDLLProject1.TestClass1 tc = new TestDLLProject1.TestClass1();

  string ss = "kkkk";

  int i = tc.FncTestDLLForDotNet(ref ss);

  int i2 = tc.FncTestDLLForDotNet2(ref ss);

  MessageBox.Show(i.ToString() + " " + i2.ToString());

  好,按ctrl+shift+b,编译工程,再按f5运行,点击按钮,如下图,成功弹出结果。

  如图:

  

回答2:

这段代码可以将桌面或者窗口保存成图片,只需要添加两个按钮就可以了!详细代码如下~

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

namespace ScreenShot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnScreen_Click(object sender, EventArgs e)
{
int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏磨迅幕宽度
int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度

Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(bmSave);

g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);
g.Dispose();

SaveFile(bmSave);
}

private void btnWindow_Click(object sender, EventArgs e)
{
Graphics gSrc = this.CreateGraphics(); //创建窗体的Graphics对象
HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());

int width = this.Width-SystemInformation.FrameBorderSize.Width; //获取宽度
int height = this.Height-SystemInformation.FrameBorderSize.Height; //获取高度

const int SRCCOPY = 0xcc0020; //复制图块的光栅操瞎肢此作码

Bitmap bmSave = new Bitmap(width, height); //用于保存图片的位图对象
Graphics gSave = Graphics.FromImage(bmSave); //创建该位图的Graphics对象
HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄

BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);

gSrc.ReleaseHdc();
gSave.ReleaseHdc();

gSrc.Dispose();
gSave.Dispose();

SaveFile(bmSave);
}
private void SaveFile(Bitmap bmSave)
{
if (DialogResult.OK == saveFileDialog1.ShowDialog())
{
string fileName = saveFileDialog1.FileName;
if (1 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".bmp"))
{
fileName += "饥拦.bmp";
}
}
else if (2 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".jpg"))
{
fileName += ".jpg";
}
}
else if (3 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".png"))
{
fileName += ".png";
}
}
Save(bmSave, fileName);
}
}
private void Save(Bitmap bm,string fileName)
{
if (fileName.EndsWith(".bmp"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
else if (fileName.EndsWith(".jpg"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (fileName.EndsWith(".png"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
bm.Dispose();
}

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
}
}