主题:Help me!C#使用API函数MessageBoxIndirect我无力继续走下去了
using System;
using System.Runtime.InteropServices;//API要用的
using System.Resources;//使用资源文件要用的
using System.Reflection;//Assembly类装这里面
using System.Drawing;
namespace API版MessageBox
{
class Program
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MSGBOXPARAMS
{
public uint cbSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public string lpszText; //匈牙利命名法lpsz==long point string Zero
public string lpszCaption;
public uint dwStyle;
public Icon lpszIcon;
public int dwContextHelpId;
public string lpfnMsgBoxCallback;
public uint dwLanguageId;
};
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern void MessageBoxIndirect(ref MSGBOXPARAMS MsgBp);//自己设定参数
internal const int MB_YESNO = 0x00000004;
internal const int MB_USERICON = 0x00000080;
static int Main()
{
ResourceManager rm = new ResourceManager("API版MessageBox.myIconRes", Assembly.GetExecutingAssembly());
MSGBOXPARAMS MsgBp = new MSGBOXPARAMS();
MsgBp.hInstance = IntPtr.Zero;//不能直接写0(int 不能转化为IntPtr),更不能写null
MsgBp.hwndOwner = IntPtr.Zero;
MsgBp.lpszText = "一定要成功啊MessageBoxIndirect我的ICON";
MsgBp.lpszCaption = "Hi";
MsgBp.dwStyle = MB_USERICON | MB_YESNO;
[color=FF0000]MsgBp.lpszIcon =(Icon)rm.GetObject("Pake");[/color]
MsgBp.dwContextHelpId = 0;
MsgBp.lpfnMsgBoxCallback = null;
MsgBp.dwLanguageId = SUBLANG_ENGLISH_US;
MsgBp.cbSize = (uint)Marshal.SizeOf(MsgBp);
MessageBoxIndirect(ref MsgBp);
return 0;
}
}
}
问题一:[color=FF0000]MsgBp.lpszIcon =(Icon)rm.GetObject("Pake");[/color]查API函数手册上lpszIcon是个指向资源文件Icon的指针,而我记得API指针应该对应C#里的string,但是string怎么能做ICON用呢?
问题二:还是那行报错[color=FF0000]Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Drawing.Icon.[/color]执行后ICON根本就没有显示.还有就是我把ICON改成Image能运行但是执行后ICON根本就没有显示
制作资源文件也有点麻烦.我找了张KaKaxi.ico
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Resources;
namespace 把忍犬帕克做成资源文件
{
class Program
{
static void Main(string[] args)
{//把KaKaxi.ico先拖入该.\Bin\Debug\下运行会生成一个资源文件
ResourceWriter rw = new ResourceWriter("myIconRes.resources");//将资源文件存入myIconRes.resources文件中
[color=FF0000]Image Img = Image.FromFile("KaKaxi.ico");[/color]
rw.AddResource("Pake",Img);
rw.Generate(); //以系统默认的format保存所有的资源
rw.Close();
}
}
}
问题三:本来我想把这行[color=FF0000]Image Img = Image.FromFile("KaKaxi.ico");[/color]改成Icon myIcon=Icon.From(****);//可是Icon只有From句柄没有FromFile.所以上面用(Icon)会报错,我左右为难啊?
using System.Runtime.InteropServices;//API要用的
using System.Resources;//使用资源文件要用的
using System.Reflection;//Assembly类装这里面
using System.Drawing;
namespace API版MessageBox
{
class Program
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MSGBOXPARAMS
{
public uint cbSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public string lpszText; //匈牙利命名法lpsz==long point string Zero
public string lpszCaption;
public uint dwStyle;
public Icon lpszIcon;
public int dwContextHelpId;
public string lpfnMsgBoxCallback;
public uint dwLanguageId;
};
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern void MessageBoxIndirect(ref MSGBOXPARAMS MsgBp);//自己设定参数
internal const int MB_YESNO = 0x00000004;
internal const int MB_USERICON = 0x00000080;
static int Main()
{
ResourceManager rm = new ResourceManager("API版MessageBox.myIconRes", Assembly.GetExecutingAssembly());
MSGBOXPARAMS MsgBp = new MSGBOXPARAMS();
MsgBp.hInstance = IntPtr.Zero;//不能直接写0(int 不能转化为IntPtr),更不能写null
MsgBp.hwndOwner = IntPtr.Zero;
MsgBp.lpszText = "一定要成功啊MessageBoxIndirect我的ICON";
MsgBp.lpszCaption = "Hi";
MsgBp.dwStyle = MB_USERICON | MB_YESNO;
[color=FF0000]MsgBp.lpszIcon =(Icon)rm.GetObject("Pake");[/color]
MsgBp.dwContextHelpId = 0;
MsgBp.lpfnMsgBoxCallback = null;
MsgBp.dwLanguageId = SUBLANG_ENGLISH_US;
MsgBp.cbSize = (uint)Marshal.SizeOf(MsgBp);
MessageBoxIndirect(ref MsgBp);
return 0;
}
}
}
问题一:[color=FF0000]MsgBp.lpszIcon =(Icon)rm.GetObject("Pake");[/color]查API函数手册上lpszIcon是个指向资源文件Icon的指针,而我记得API指针应该对应C#里的string,但是string怎么能做ICON用呢?
问题二:还是那行报错[color=FF0000]Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Drawing.Icon.[/color]执行后ICON根本就没有显示.还有就是我把ICON改成Image能运行但是执行后ICON根本就没有显示
制作资源文件也有点麻烦.我找了张KaKaxi.ico
C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Resources;
namespace 把忍犬帕克做成资源文件
{
class Program
{
static void Main(string[] args)
{//把KaKaxi.ico先拖入该.\Bin\Debug\下运行会生成一个资源文件
ResourceWriter rw = new ResourceWriter("myIconRes.resources");//将资源文件存入myIconRes.resources文件中
[color=FF0000]Image Img = Image.FromFile("KaKaxi.ico");[/color]
rw.AddResource("Pake",Img);
rw.Generate(); //以系统默认的format保存所有的资源
rw.Close();
}
}
}
问题三:本来我想把这行[color=FF0000]Image Img = Image.FromFile("KaKaxi.ico");[/color]改成Icon myIcon=Icon.From(****);//可是Icon只有From句柄没有FromFile.所以上面用(Icon)会报错,我左右为难啊?