c# - Transparent background form -
i have c# visual form wrapper custom background image (png), if launch app, looking nice, if i'll go move window - troubles.
// import using system; using system.collections.generic; using system.text; using system.drawing; using system.windows.forms; using system.componentmodel; using system.drawing.drawing2d; // window control class ztwindow : form { // variables private point m_mouseposition; private image m_background; // initilization /* default constructor */ public ztwindow() { initialize(); } /* initilization constructor */ public ztwindow(string title) { initialize(); text = title; } /* default destructor */ ~ztwindow() {} /* initilization */ private void initialize() { setstyle(controlstyles.doublebuffer, true); // m_background = image.fromfile(@"template\background.png", true); // width = m_background.width; height = m_background.height; autoscalemode = autoscalemode.font; formborderstyle = formborderstyle.none; centertoscreen(); // setupevents(); } // events /* initilization events */ private void setupevents() { mousedown += onmousedown; mousemove += onmousemove; } /* mouse down */ private void onmousedown(object sender, mouseeventargs event) { // change mouse state & setup position if (event.button == mousebuttons.left) { m_mouseposition = new point(event.x, event.y); } } /* mouse move */ private void onmousemove(object sender, mouseeventargs event) { // change window location if (event.button == mousebuttons.left) { location = new point(location.x + (event.x - m_mouseposition.x), location.y + (event.y - m_mouseposition.y)); } } // override /* paint event */ protected override void onpaint(painteventargs event) { event.graphics.drawimage(m_background, 0, 0, m_background.width, m_background.height); base.onpaint(event); } /* paint background event */ protected override void onpaintbackground(painteventargs event) { // not use background //base.onpaintbackground(event); } }
how make transparent image on background or fix painting update?
update thank @bauss link information (http://www.codeproject.com/articles/1822/per-pixel-alpha-blend-in-c), it's , working fine.
completed version: window.cs
// import using system; using system.collections.generic; using system.text; using system.drawing; using system.windows.forms; using system.componentmodel; using system.drawing.imaging; // window control class ztwindow : form { // variables private point m_mouseposition; private image m_background; private picturebox m_picture = new picturebox(); // initilization /* default constructor */ public ztwindow() { initialize(); } /* initilization constructor */ public ztwindow(string title) { initialize(); text = title; } /* default destructor */ ~ztwindow() {} /* initilization */ private void initialize() { m_background = image.fromfile(@"template\background.png", true); // width = m_background.width; height = m_background.height; autoscalemode = autoscalemode.font; formborderstyle = formborderstyle.none; centertoscreen(); // setupevents(); setupbackground(m_background, 255); } // events /* initilization events */ private void setupevents() { mousedown += onmousedown; mousemove += onmousemove; } /* mouse down */ private void onmousedown(object sender, mouseeventargs event) { // change mouse state & setup position if (event.button == mousebuttons.left) { m_mouseposition = new point(event.x, event.y); } } /* mouse move */ private void onmousemove(object sender, mouseeventargs event) { // change window location if (event.button == mousebuttons.left) { location = new point(location.x + (event.x - m_mouseposition.x), location.y + (event.y - m_mouseposition.y)); } } // override /* object create property */ protected override createparams createparams { { createparams tmpinstance = base.createparams; tmpinstance.exstyle |= 0x00080000; // ws_ex_layered return tmpinstance; } } // helpers /* setup alpha blend background (copyright © 2002-2004 rui godinho lopes ) */ public void setupbackground(image backgroundimage, byte opacity) { bitmap bitmap = (bitmap)backgroundimage; // if (bitmap.pixelformat != pixelformat.format32bppargb) { throw new applicationexception("the bitmap must 32ppp alpha-channel."); } // intptr screendc = win32.getdc(intptr.zero); intptr memdc = win32.createcompatibledc(screendc); intptr hbitmap = intptr.zero; intptr oldbitmap = intptr.zero; // try { hbitmap = bitmap.gethbitmap(color.fromargb(0)); // grab gdi handle gdi+ bitmap oldbitmap = win32.selectobject(memdc, hbitmap); win32.size size = new win32.size(bitmap.width, bitmap.height); win32.point pointsource = new win32.point(0, 0); win32.point toppos = new win32.point(left, top); win32.blendfunction blend = new win32.blendfunction(); blend.blendop = win32.ac_src_over; blend.blendflags = 0; blend.sourceconstantalpha = opacity; blend.alphaformat = win32.ac_src_alpha; win32.updatelayeredwindow(handle, screendc, ref toppos, ref size, memdc, ref pointsource, 0, ref blend, win32.ulw_alpha); } { win32.releasedc(intptr.zero, screendc); if (hbitmap != intptr.zero) { win32.selectobject(memdc, oldbitmap); win32.deleteobject(hbitmap); } win32.deletedc(memdc); } } }
win32.cs
// // copyright © 2002-2004 rui godinho lopes // rights reserved. // // source file(s) may redistributed unmodified means // providing not sold profit without authors expressed // written consent, , providing notice , authors name // , copyright notices remain intact. // // use of software in source or binary forms, or without // modification, must include, in user documentation ("about" box , // printed documentation) , internal comments code, notices // end user follows: // // "portions copyright © 2002-2004 rui godinho lopes" // // email letting me know using nice well. // that's not ask considering amount of work went // this. // // software provided "as is", without warranty of kind, // express or implied. use @ yout own risk. author accepts no // liability data damage/loss product may cause. // using system; using system.drawing; using system.drawing.imaging; using system.windows.forms; using system.runtime.interopservices; // class exposes needed win32 gdi functions. class win32 { public enum bool { false = 0, true }; [structlayout(layoutkind.sequential)] public struct point { public int32 x; public int32 y; public point(int32 x, int32 y) { this.x = x; this.y = y; } } [structlayout(layoutkind.sequential)] public struct size { public int32 cx; public int32 cy; public size(int32 cx, int32 cy) { this.cx = cx; this.cy = cy; } } [structlayout(layoutkind.sequential, pack = 1)] struct argb { public byte blue; public byte green; public byte red; public byte alpha; } [structlayout(layoutkind.sequential, pack = 1)] public struct blendfunction { public byte blendop; public byte blendflags; public byte sourceconstantalpha; public byte alphaformat; } public const int32 ulw_colorkey = 0x00000001; public const int32 ulw_alpha = 0x00000002; public const int32 ulw_opaque = 0x00000004; public const byte ac_src_over = 0x00; public const byte ac_src_alpha = 0x01; [dllimport("user32.dll", exactspelling = true, setlasterror = true)] public static extern bool updatelayeredwindow(intptr hwnd, intptr hdcdst, ref point pptdst, ref size psize, intptr hdcsrc, ref point pprsrc, int32 crkey, ref blendfunction pblend, int32 dwflags); [dllimport("user32.dll", exactspelling = true, setlasterror = true)] public static extern intptr getdc(intptr hwnd); [dllimport("user32.dll", exactspelling = true)] public static extern int releasedc(intptr hwnd, intptr hdc); [dllimport("gdi32.dll", exactspelling = true, setlasterror = true)] public static extern intptr createcompatibledc(intptr hdc); [dllimport("gdi32.dll", exactspelling = true, setlasterror = true)] public static extern bool deletedc(intptr hdc); [dllimport("gdi32.dll", exactspelling = true)] public static extern intptr selectobject(intptr hdc, intptr hobject); [dllimport("gdi32.dll", exactspelling = true, setlasterror = true)] public static extern bool deleteobject(intptr hobject); }
Comments
Post a Comment