Category Archives: C#

C# programming change Screen Resolution

[sourcecode language=\”csharp\”]
enum DMDO
{
DEFAULT = 0,
D90 = 1,
D180 = 2,
D270 = 3
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct DEVMODE
{
public const int DM_DISPLAYFREQUENCY = 0x400000;
public const int DM_PELSWIDTH = 0x80000;
public const int DM_PELSHEIGHT = 0x100000;
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public DMDO dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
public static void SetScreenResolution(int width, int height)
{
long RetVal = 0;
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT;
RetVal = ChangeDisplaySettings(ref dm, 0);
}
[/sourcecode]

怎么跨进程得到IE的IHTMLDocument2接口

两种方法,
1. 利用.Net本身的ShellWindows接口和InternetExplorer接口,
[sourcecode language=\”csharp\”]
ShellWindows windows = new ShellWindowsClass();
IWebBrowser2 ie = null;
foreach (InternetExplorer tmpIE in windows)
{
if (tmpIE.FullName.EndsWith("iexplore.exe", true, null))
{
ie = tmpIE;
}
}
IHTMLDocument2 doc = ie.Document;
[/sourcecode]
2. 利用一个Windows消息,WM_HTML_GETOBJECT,调用Windows API ObjectFromLresult
[sourcecode language=\”csharp\”]
lMsg = SUIWinAPIs.RegisterWindowMessage("WM_HTML_GETOBJECT");
SUIWinAPIs.SendMessageTimeout(window.WindowHandle, lMsg, IntPtr.Zero, IntPtr.Zero, SUIWinAPIs.SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);
if (lResult != IntPtr.Zero)
{
htmlDocument = (IHTMLDocument2)SUIWinAPIs.ObjectFromLresult(lResult,typeof(IHTMLDocument).GUID, IntPtr.Zero);
}
[/sourcecode]