Just starting, need some help please!
As you can already guess, I'm beginning to teach myself c#. I've worked in vb.Net for the past few years now so I'm no amateur.
The problem I'm receiving is one with many solutions.. In fact there's endless pages of problems that could cause this error, but maybe, just maybe I'm new and am doing something wrong in the first place.
What I'm doing is trying to replicate my dwm class for enabling composition, extending frame, and enabling blur behind.
No errors show at design time, but when I attempt to run the project I get this error...
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
and highlights this line...
Code:
DwmExtendFrameIntoClientArea(form.Handle, margins);
here is my entire class.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class dwm
{
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public Int32 Left;
public Int32 Right;
public Int32 Top;
public Int32 Bottom;
public Margins(Int32 left, Int32 right, Int32 top, Int32 bottom)
{
this.Left = left;
this.Right = right;
this.Top = top;
this.Bottom = bottom;
}
}
public struct Dwm_BlurBehind
{
public Dwm_BlurBehind_Flags Flags;
public Boolean Enabled;
public IntPtr RgnBlur;
public Boolean TransitionOnMaximized;
public Dwm_BlurBehind(Dwm_BlurBehind_Flags flags, Boolean enabled, IntPtr rgnblur, Boolean transitiononmaximized)
{
this.Flags = flags;
this.Enabled = enabled;
this.RgnBlur = rgnblur;
this.TransitionOnMaximized = transitiononmaximized;
}
}
public enum Dwm_BlurBehind_Flags { ENABLED = 1, BLURREGION = 2, TRANSITIONONMAXIMIZED = 4 }
[DllImport("dwmapi.dll")]
private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, Margins margins);
[DllImport("dwmapi.dll")]
private static extern Int32 DwmEnableComposition(Boolean enable);
[DllImport("dwmapi.dll")]
private static extern Int32 DwmIsCompositionEnabled(out Boolean enabled);
[DllImport("dwmapi.dll")]
private static extern Int32 DwmEnableBlurBehindWindow(IntPtr hWnd, Dwm_BlurBehind flags);
//determines if composition is currently enabled or not.
public Boolean Composition()
{
Boolean enabled = false;
if (Environment.OSVersion.Version.Major >= 6) //make sure the current version of windows supports composition.
{
Int32 result = 0;
result = DwmIsCompositionEnabled(out enabled);
}
return enabled; //return out composition enabled state.
}
public void CompositionState(Boolean enabled)
{
if (Environment.OSVersion.Version.Major >= 6) //make sure the current version of windows supports composition.
{
DwmEnableComposition(enabled);
}
}
public void ExtendFrameIntoClient(System.Windows.Forms.Form form, Int32 left = (-1), Int32 right = (-1), Int32 top = (-1), Int32 bottom = (-1))
{
if (form == null) { return; } //make sure our form is something.
if (Environment.OSVersion.Version.Major >= 6) //make sure the current version of windows supports composition.
{
if (Composition() == true) //composition is enabled.
{
Margins margins = new Margins(left, right, top, bottom);
DwmExtendFrameIntoClientArea(form.Handle, margins);
}
else
{
MessageBox.Show("Windows composition is not enabled. First enable composition then try again.");
}
}
}
public void BlurBehind(System.Windows.Forms.Form form, Boolean enabled)
{
if (form == null) { return; } //make sure our form is something.
if (Environment.OSVersion.Version.Major >= 6) //make sure the current version of windows supports composition.
{
Int32 result = 0;
Dwm_BlurBehind flags;
flags = new Dwm_BlurBehind(Dwm_BlurBehind_Flags.ENABLED | Dwm_BlurBehind_Flags.BLURREGION | Dwm_BlurBehind_Flags.TRANSITIONONMAXIMIZED, enabled, (IntPtr)0, true);
//DwmEnableBlurBehindWindow(obj.Handle, New DWMBLURBEHIND(DWM_BB.BLURREGION Or DWM_BB.TRANSITIONONMAXIMIZED Or DWM_BB.ENABLE, Enabled, CType(0, IntPtr), True))
result = DwmEnableBlurBehindWindow(form.Handle, flags);
}
}
}
}
thanks in advanced!
Re: Just starting, need some help please!
I'm going to update with some of my attempts, which unfortunately did not work..
In Margins, I reordered those to Left, Top, Right, Bottom(someone said this could work).
Second attempt I removed the Margins structure completely. Replaced that with System.Windows.Thickness.
Both of these did not solve the problem, and I'm still receiving the same exception on the same line.
Re: Just starting, need some help please!
Scratch that I solved it, but I would really like some explanation as to why...
all I had to do was to have the ref keyword for the Thickness param, why??!