C#制作ActiveX控件在网页上使用.doc_第1页
C#制作ActiveX控件在网页上使用.doc_第2页
C#制作ActiveX控件在网页上使用.doc_第3页
C#制作ActiveX控件在网页上使用.doc_第4页
C#制作ActiveX控件在网页上使用.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#制作ActiveX控件并在Web中使用C#制作ActiveX控件,一般来说并不推荐这样做。因为还要为客户安装对应的.Net Framework版本。现在一个示例实现将手写板的签字图片复制到剪贴板,从剪贴板的图片中对图片进行按比例缩放,并保存为 C:oaSignDpi.jpg。1.在Visual Studio 2010中,新建项目Windows窗体控件库。命名为SaveClipImage2. 在SaveClipImage项目中 新建一个接口IObjectSafety. 这个接口内容最好不要变更。接口IObjectSafety具体代码如下:using System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Text;namespace SaveClipImage ComImport, GuidAttribute(CB5BDC81-93C1-11CF-8F20-00805F2CD064) InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown) public interface IObjectSafety PreserveSig int GetInterfaceSafetyOptions(ref Guid riid, MarshalAs(UnmanagedType.U4) ref int pdwSupportedOptions, MarshalAs(UnmanagedType.U4) ref int pdwEnabledOptions); PreserveSig() int SetInterfaceSafetyOptions(ref Guid riid, MarshalAs(UnmanagedType.U4) int dwOptionSetMask, MarshalAs(UnmanagedType.U4) int dwEnabledOptions); 3.在默认的UserControl1控件上增加一个PictureBox,设置可见性级别为Public.详细代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Runtime.InteropServices;using System.Text;using System.Windows.Forms;using System.Management;namespace SaveClipImage Guid(6F151B97-B972-4112-BEB1-754173BAA80C), /(这个Guid必须要和制作安装文件时的Guid号码一样)ProgId(_20131017.UserControl1), ComVisible(true) /(控件必须添加的内容) public partial class UserControl1 : UserControl, IObjectSafety public string fileName = ; System.Runtime.InteropServices.DllImport(user32) private static extern IntPtr SetClipboardViewer(IntPtr hwnd); System.Runtime.InteropServices.DllImport(user32) private static extern IntPtr ChangeClipboardChain(IntPtr hwnd, IntPtr hWndNext); System.Runtime.InteropServices.DllImport(user32) private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); const int WM_DRAWCLIPBOARD = 0x308; const int WM_CHANGECBCHAIN = 0x30D; public IntPtr NextClipHwnd;/下一个句柄 public UserControl1() InitializeComponent(); pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; Clipboard.Clear(); /获得观察链中下一个窗口句柄 NextClipHwnd = SetClipboardViewer(this.Handle); public bool ThumbnailCallback() return false; / / 按比例缩放图片 / / 原图片 / 保存缩放后的图片路径 / 标准高度 如果大于标准高度,则按比例缩放 public bool ReducedImage(Image ResourceImage, string newImagePath, int StandardHeight) Image ReducedImage = null; try Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); int ImageWidth; int ImageHeight; if (ResourceImage.Height StandardHeight) /如果图片高度大于标准高度,按比例缩放 ImageWidth = (int)(double)StandardHeight * ResourceImage.Width / ResourceImage.Height); ImageHeight = StandardHeight; else /如果没有大于标准高度 则保持不变 ImageWidth = ResourceImage.Width; ImageHeight = ResourceImage.Height;/ResourceImage.HorizontalResolution ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); if (System.IO.File.Exists(newImagePath) try System.IO.File.Delete(newImagePath); catch ReducedImage.Save(newImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); ReducedImage.Dispose(); ResourceImage.Dispose(); return true; catch (Exception ex) MessageBox.Show(ex.Message, 错误); if (ResourceImage != null) ResourceImage.Dispose(); if (ReducedImage != null) ReducedImage.Dispose(); return false; private void button1_Click()/(object sender, EventArgs e) Image img = Clipboard.GetImage();/获得剪贴板的图片 if (img = null) /MessageBox.Show(没有找到签名图片,请用手写板签名., 错误); fileName = ; /pictureBox1.Image.Dispose(); /pictureBox1.Image = null; return; string saveFileName = C:oaSign.jpg; /if (System.IO.File.Exists(saveFileName) / / try / / System.IO.File.Delete(saveFileName); / / catch / if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); pictureBox1.Image = null; if (ReducedImage(img, saveFileName, 40) /Clipboard.Clear(); try Image srcImg = Image.FromFile(saveFileName); Bitmap bmp = new Bitmap(srcImg); int xDpi = 96; int yDpi = 96; GetSystemDpi(ref xDpi, ref yDpi); bmp.SetResolution(xDpi, yDpi);/设置水平 和 垂直DPI都是96 大多数显示器默认 /srcImg.Dispose(); string fileDpi96 = C:oaSignDpi.jpg; if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); if (System.IO.File.Exists(fileDpi96) try System.IO.File.Delete(fileDpi96); catch bmp.Save(fileDpi96, System.Drawing.Imaging.ImageFormat.Jpeg); /Image displayImg = Image.FromFile(fileDpi96); pictureBox1.Image = Image.FromFile(fileDpi96); bmp.Dispose(); /Application.DoEvents(); /displayImg.Dispose(); fileName = fileDpi96; srcImg.Dispose(); img.Dispose(); /if (pictureBox1.Image != null) / / pictureBox1.Image.Dispose(); / catch (Exception ex) if (img != null) img.Dispose(); MessageBox.Show(ex.Message); fileName = ; else /Clipboard.Clear(); fileName = ; /pictureBox1.Image.Dispose(); /pictureBox1.Image = null; / / 使用WMI对象获取系统的DPI / / / public void GetSystemDpi(ref int xDpi, ref int yDpi) using (ManagementClass mc = new ManagementClass(Win32_DesktopMonitor) using (ManagementObjectCollection moc = mc.GetInstances() int PixelsPerXLogicalInch = 0; / dpi for x int PixelsPerYLogicalInch = 0; / dpi for y foreach (ManagementObject each in moc) PixelsPerXLogicalInch = int.Parse(each.PropertiesPixelsPerXLogicalInch.Value.ToString(); PixelsPerYLogicalInch = int.Parse(each.PropertiesPixelsPerYLogicalInch.Value.ToString(); xDpi = PixelsPerXLogicalInch; yDpi = PixelsPerYLogicalInch; /MessageBox.Show(xDpi.ToString() + , + yDpi); protected override void WndProc(ref Message m) switch (m.Msg) case WM_DRAWCLIPBOARD: /将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口 SendMessage(NextClipHwnd, m.Msg, m.WParam, m.LParam); IDataObject iData = Clipboard.GetDataObject(); /检测图像 if (iData.GetDataPresent(DataFormats.Bitmap) /pictureBox1.Image = Clipboard.GetImage(); button1_Click(); /CopyToClipboard(); break; default: base.WndProc(ref m); break; private const string _IID_IDispatch = 00020400-0000-0000-C000-000000000046; private const string _IID_IDispatchEx = a6ef9860-c720-11d0-9337-00a0c90dcaa9; private const string _IID_IPersistStorage = 0000010A-0000-0000-C000-000000000046; private const string _IID_IPersistStream = 00000109-0000-0000-C000-000000000046; private const string _IID_IPersistPropertyBag = 37D84F60-42CB-11CE-8135-00AA004BB851; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; private const int E_FAIL = unchecked(int)0x80004005); private const int E_NOINTERFACE = unchecked(int)0x80004002); private bool _fSafeForScripting = true; private bool _fSafeForInitializing = true; #region IObjectSafety 成员 public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) int Rslt = E_FAIL; string strGUID = riid.ToString(B); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) case _IID_IDispatch: case _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting = true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing = true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; return Rslt; public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) int Rslt = E_FAIL; string strGUID = riid.ToString(B); switch (strGUID) case _IID_IDispatch: case _IID_IDispatchEx: if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE_FOR_UNTRUSTED_CALLER) & (_fSafeForScripting = true) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE_FOR_UNTRUSTED_DATA) & (_fSafeForInitializing = true) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; return Rslt; #endregion 4. 在解决方案下,新建项目安装于部署 命名为ClipImage在ClipImage中添加项目输出。整个项目的内容解决方案菜单如下:设置SaveClipImage.tlb的Register属性为vsdrfCOM.将ClipImage生成。生成如下两个文件:5. 制作cab下载这个程序包,可以从微软下载,地址:/download/platformsdk/cab/2.0/w98nt42kmexp/en-us/cabsdk.exe 将cabsdk解压后在cabsdk的BIN目录下,复制ClipImage.msi到这个目录下。增加3个文件(全部用记事本打开,更新后保存):installer.infcab.ddfmakecab.bat这三个文件的具体内容:installer.inf的具体内容:Setup Hookshook1=hook1hook1run=msiexec /i %EXTRACT_DIR%ClipImage.msi /qnVersionSigna

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论