/// <summary> /// レイヤード ウィンドウを設定します。 /// </summary> /// <param name="srcBitmap">表示する画像</param> public void SetLayeredWindow(Bitmap srcBitmap) { // デバイスコンテキストを取得 var screenDc = IntPtr.Zero; var memDc = IntPtr.Zero; var hBitmap = IntPtr.Zero; var hOldBitmap = IntPtr.Zero; try { screenDc = API.GetDC(IntPtr.Zero); memDc = API.CreateCompatibleDC(screenDc); hBitmap = srcBitmap.GetHbitmap(Color.FromArgb(0)); hOldBitmap = API.SelectObject(memDc, hBitmap); // BLENDFUNCTION を初期化 var blend = new API.BLENDFUNCTION() { BlendOp = API.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = API.AC_SRC_ALPHA, }; // レイヤードウィンドウを更新 Size = new Size(srcBitmap.Width, srcBitmap.Height); var pptDst = new Point(Left, Top); var psize = new Size(Width, Height); var pptSrc = new Point(0, 0); // 更新 API.UpdateLayeredWindow( Handle, screenDc, ref pptDst, ref psize, memDc, ref pptSrc, 0, ref blend, API.ULW_ALPHA); } finally { if (screenDc != IntPtr.Zero) { API.ReleaseDC(IntPtr.Zero, screenDc); } if (hBitmap != IntPtr.Zero) { API.SelectObject(memDc, hOldBitmap); API.DeleteObject(hBitmap); } if (memDc != IntPtr.Zero) { API.DeleteDC(memDc); } } }
//The usual SINGLE-COLOR TransKey method for making the background transparent has failed due to random white pixels which are not removed //because their colors are slightly off the TransKey. //So an alternate way of drawing on form is utilised here using API calls #region CUSTOM PAINT METHODS ---------------------------------------------- //Updates the Form's display using API calls static public void UpdateFormDisplay(Image backgroundImage, Form canvas) { IntPtr screenDc = API.GetDC(IntPtr.Zero); IntPtr memDc = API.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { //Display-image using (Bitmap bmp = new Bitmap(backgroundImage)) { hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent oldBitmap = API.SelectObject(memDc, hBitmap); //Display-rectangle Size size = bmp.Size; Point pointSource = new Point(0, 0); Point topPos = new Point(canvas.Left, canvas.Top); //Set up blending options API.BLENDFUNCTION blend = new API.BLENDFUNCTION(); blend.BlendOp = API.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = API.AC_SRC_ALPHA; //Draw image API.UpdateLayeredWindow(canvas.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, API.ULW_ALPHA); //Clean-up bmp.Dispose(); API.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { API.SelectObject(memDc, oldBitmap); API.DeleteObject(hBitmap); } API.DeleteDC(memDc); } } catch (Exception) { } }
//Updates the Form's display using API calls public static void UpdateFormDisplay(Image backgroundImage, Form canvas) { IntPtr screenDc = API.GetDC(IntPtr.Zero); IntPtr memDc = API.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { //Display-image using (Bitmap bmp = new Bitmap(backgroundImage)) { hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent oldBitmap = API.SelectObject(memDc, hBitmap); //Display-rectangle Size size = bmp.Size; Point pointSource = new Point(0, 0); Point topPos = new Point(canvas.Left, canvas.Top); //Set up blending options API.BLENDFUNCTION blend = new API.BLENDFUNCTION(); blend.BlendOp = API.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = API.AC_SRC_ALPHA; //Draw image API.UpdateLayeredWindow(canvas.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, API.ULW_ALPHA); //Clean-up bmp.Dispose(); API.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { API.SelectObject(memDc, oldBitmap); API.DeleteObject(hBitmap); } API.DeleteDC(memDc); } } catch (Exception) { } }
public void UpdateFormDisplay(Image backgroundImage) { IntPtr screenDc = API.GetDC(IntPtr.Zero); IntPtr memDc = API.CreateCompatibleDC(screenDc); IntPtr hBitmap; IntPtr oldBitmap; //Display-image Bitmap bmp = new Bitmap(backgroundImage); Graphics g = Graphics.FromImage(bmp); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; for (int i = 0; i < Controls.Count; i++) { if (!Controls[i].Visible) { continue; } Bitmap bmmp = new Bitmap(Controls[i].Width, Controls[i].Height); Controls[i].DrawToBitmap(bmmp, Controls[i].ClientRectangle); g.DrawImage(bmmp, Controls[i].Location); } PointF mousepos = new PointF(MousePosition.X - Left, MousePosition.Y - Top); for (int i = 0; i < 10; i++) { if (i + 1 == Program.curDay) { g.FillRectangle(new SolidBrush(Color.FromArgb(150, 120, 120, 120)), 110 * i, 0, 120, 590); } for (int j = 0; j < 7; j++) { Brush pencolourl = Brushes.Aqua; PointF curloc = new PointF(110 * i + 10, 82 * j + 10); RectangleF containerF = new RectangleF(curloc.X, curloc.Y, 100, 80); int alpha = 255; if (Program.TimetableList.ContainsKey((i + 1) + j.ToString())) { Period curp = Program.TimetableList[(i + 1) + j.ToString()]; pencolourl = new SolidBrush(Color.FromArgb(alpha, Program.ColorRef[curp.ClassCode])); Brush textbrush = new SolidBrush(Color.FromArgb(alpha, Color.Black)); g.FillRectangle(pencolourl, containerF); g.DrawString(curp.Room, SystemFonts.DefaultFont, textbrush, curloc.X + 60, curloc.Y + 65); g.DrawString(curp.ClassCode, SystemFonts.DefaultFont, textbrush, curloc.X, curloc.Y + 65); g.DrawString(curp.SchoolStaffCode, SystemFonts.DefaultFont, textbrush, curloc.X, curloc.Y + 55); g.DrawString(curp.ClassDescription, SystemFonts.DefaultFont, textbrush, new RectangleF(curloc.X, curloc.Y + 1, 98, 50)); textbrush.Dispose(); } if (mouseclick.X != -10000 && containerF.Contains(mouseclick) && Program.TimetableList.ContainsKey((i + 1) + j.ToString())) { mouseclick = new PointF(-10000, 0); colorbox = true; colorDialog1.Color = Program.ColorRef[Program.TimetableList[(i + 1) + j.ToString()].ClassCode]; var results = colorDialog1.ShowDialog(); if (results == DialogResult.OK) { Program.ColorRef[Program.TimetableList[(i + 1) + j.ToString()].ClassCode] = colorDialog1.Color; } colorbox = false; } pencolourl.Dispose(); } } using (Font bigfont = new Font("Trebuchet MS", dx * 18)) { g.DrawString("Term " + Program.SettingsData.Curterm, bigfont, Brushes.White, 1098, 5); } mouseclick = new PointF(-10000, 0); hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent oldBitmap = API.SelectObject(memDc, hBitmap); //Display-rectangle Size size = bmp.Size; Point pointSource = new Point(0, 0); Point topPos = new Point(Left, Top); //Set up blending options API.BLENDFUNCTION blend = new API.BLENDFUNCTION(); blend.BlendOp = API.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = Convert.ToByte(255); blend.AlphaFormat = API.AC_SRC_ALPHA; API.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, API.ULW_ALPHA); //Clean-up g.Dispose(); bmp.Dispose(); API.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { API.SelectObject(memDc, oldBitmap); API.DeleteObject(hBitmap); } API.DeleteDC(memDc); }