|
why using old win32 with .net and c#...? get rid of those old gdi handles...
|
|
|
|
|
because you cant update a layered window using a gdi+ bitmap....
|
|
|
|
|
How would I get this working with VB.Net 2005?
|
|
|
|
|
Class:
<br />
Imports System<br />
Imports System.Drawing<br />
Imports System.Drawing.Imaging<br />
Imports System.Windows.Forms<br />
Imports System.Runtime.InteropServices<br />
'Translation from C# by Dj Den4ik<br />
Public Class PerPixelAlphaForm<br />
'Implements Windows.Forms.IWin32Window<br />
<br />
Public Structure ARGB<br />
Public Blue As Byte<br />
Public Green As Byte<br />
Public Red As Byte<br />
Public Alpha As Byte<br />
End Structure<br />
<br />
Public Structure BLENDFUNCTION<br />
Public BlendOp As Byte<br />
Public BlendFlags As Byte<br />
Public SourceConstantAlpha As Byte<br />
Public AlphaFormat As Byte<br />
End Structure<br />
<br />
Public Const ULW_COLORKEY As Int32 = &H1<br />
Public Const ULW_ALPHA As Int32 = &H2<br />
Public Const ULW_OPAQUE As Int32 = &H4<br />
<br />
Public Const AC_SRC_OVER As Byte = &H0<br />
Public Const AC_SRC_ALPHA As Byte = &H1<br />
<br />
Public Declare Function UpdateLayeredWindow Lib "user32" Alias "UpdateLayeredWindow" (ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point, ByVal crKey As Int32, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Int32) As Boolean<br />
Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hWnd As IntPtr) As IntPtr<br />
Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer<br />
Public Declare Function CreateCompatibleDC Lib "gdi32.dll" Alias "CreateCompatibleDC" (ByVal hDC As IntPtr) As IntPtr<br />
Public Declare Function DeleteDC Lib "gdi32.dll" Alias "DeleteDC" (ByVal hDC As IntPtr) As Boolean<br />
Public Declare Function SelectObject Lib "gdi32.dll" Alias "SelectObject" (ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr<br />
Public Declare Function DeleteObject Lib "gdi32.dll" Alias "DeleteObject" (ByVal hObject As IntPtr) As Boolean<br />
<br />
Public Sub SetBitmap(ByVal bitmap As Bitmap, ByVal opacity As Byte, ByVal frm As Form)<br />
If bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then Throw New ApplicationException("The bitmap must be 32ppp with alpha-channel.")<br />
<br />
' The ideia of this is very simple,<br />
' 1. Create a compatible DC with screen;<br />
' 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;<br />
' 3. Call the UpdateLayeredWindow.<br />
<br />
Dim screenDc As IntPtr = GetDC(IntPtr.Zero)<br />
Dim memDc As IntPtr = CreateCompatibleDC(screenDc)<br />
Dim hBitmap As IntPtr = IntPtr.Zero<br />
Dim oldBitmap As IntPtr = IntPtr.Zero<br />
<br />
Try<br />
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)) ' grab a GDI handle from this GDI+ bitmap<br />
oldBitmap = SelectObject(memDc, hBitmap)<br />
Dim size As New Size(bitmap.Width, bitmap.Height)<br />
Dim pointSource As New Point(0, 0)<br />
Dim topPos As New Point(frm.Left, frm.Top)<br />
Dim blend As New BLENDFUNCTION<br />
blend.BlendOp = AC_SRC_OVER<br />
blend.BlendFlags = 0<br />
blend.SourceConstantAlpha = opacity<br />
blend.AlphaFormat = AC_SRC_ALPHA<br />
UpdateLayeredWindow(frm.Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, ULW_ALPHA)<br />
Finally<br />
ReleaseDC(IntPtr.Zero, screenDc)<br />
If hBitmap <> IntPtr.Zero Then<br />
SelectObject(memDc, oldBitmap)<br />
DeleteObject(hBitmap)<br />
End If<br />
DeleteDC(memDc)<br />
<br />
End Try<br />
End Sub<br />
Form:
<br />
Dim ppaf As New PerPixelAlphaForm<br />
Dim bmp As New Bitmap("D:\Audio\FL Studio 7\Artwork\FL Studio Producer Edition\Title_.png")<br />
<br />
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams<br />
Get<br />
Dim SecPerm As New Security.Permissions.SecurityPermission(Security.Permissions.PermissionState.Unrestricted)<br />
SecPerm.Demand()<br />
<br />
' Extend the CreateParams property of the Button class.<br />
Dim cp As System.Windows.Forms.CreateParams = MyBase.CreateParams<br />
' Update the button Style.<br />
cp.ExStyle = &H80000<br />
<br />
Return cp<br />
End Get<br />
End Property<br />
<br />
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None<br />
Me.TopMost = True<br />
For Each ctrl As Control In Me.Controls<br />
ctrl.DrawToBitmap(bmp, ctrl.Bounds)<br />
Next<br />
Me.Region = New Region()<br />
ppaf.SetBitmap(bmp, 240, Me)<br />
End Sub<br />
<br />
<br />
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)<br />
<br />
Const WM_MOUSEMOVE As Int32 = &H200<br />
Const WM_NCLBUTTONDOWN As Int32 = &HA1<br />
Const HTCAPTION As Int32 = 2<br />
<br />
'// ????????????? WM_MOUSEMOVE <br />
If m.Msg = WM_MOUSEMOVE Then<br />
<br />
'
MyBase.Capture = False<br />
<br />
'// ??????? ???? ????????? <br />
Dim message As New Message<br />
With message<br />
.HWnd = Me.Handle<br />
.Msg = WM_NCLBUTTONDOWN<br />
.WParam = HTCAPTION<br />
.LParam = 0&<br />
End With<br />
<br />
'
MyBase.WndProc(message)<br />
End If<br />
<br />
MyBase.WndProc(m)<br />
<br />
End Sub<br />
|
|
|
|
|
|
personally, however, I'd drop VB.NET and move to C# like most eventually do ... including me
|
|
|
|
|
Simply the BEST!! Many thanks, Rui!!!
|
|
|
|
|
Add some controls to overlay transparent form and avoid refreshing it's content with common Refresh()/Invalidate() methods,
use UpdateLayeredWindow() instead. For correct controls rendering add code like
Bitmap temp_bmp = new Bitmap(bitmap);
foreach (Control ctrl in this.Controls)
{
ctrl.DrawToBitmap(temp_bmp, ctrl.Bounds);
}
hBitmap = temp_bmp.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
to the PerPixelAlphaForm::SetBitmap() method. Every System.Windows.Forms controlhas method DrawToBitmap().
Actually, i used this way to render simple controls - labels, i don't know would it work with more complex
user controls, but i can't see why it should not
|
|
|
|
|
Thanks, I was wondering about this.
Works perfectly
Edit: Ok, I take that back sorta.. The actual clickable region is MUCH smaller than the visible control (a button in this case)...
modified on Sunday, August 24, 2008 2:37 PM
|
|
|
|
|
Thanks, this is just what I needed to complete my design. It appeared that the bitmap was not getting refreshed each time the procedure was called. The actual Form.BackgroundImage needs to be present underneath so the region cut out from your controls, e.g. so this background image can bleed through around the controls.
In order to achieve transparency of the form controls, just override the controls in question and make sure that the regions are set to the way you need along with the ctrl.BackColor = Color.Transparent.
Without spending too much time on this, ensure that the 'bitmap' you are using originated from the 'this.BackgroundImage' build-in Form property.
<br />
public void SetBitmap(Bitmap bitmap, byte opacity = 255) {<br />
}<br />
So just I just changed all references outside this function and used the pre-existing container for the Bitmap/Image with one already build into the Form control itself.
So what you get is something like this: <substitued class="" name="" mainform="" from="" perpixelalphaform="">
partial class MainForm: Form
{
Bitmap bkgrnd = null;
...
public MainForm()
{
InitializeComponent();
bkgrnd = lib.LoadBitmapResource(2005);
this.BackgroundImage = bkgrnd;
this.CenterToScreen();
}
...
private void RefreshFormTimer_Tick(object sender, EventArgs e)
{
if (bkgrnd != null)
SetBitmap(bkgrnd);
}
-Latency
modified on Monday, January 24, 2011 5:52 AM
|
|
|
|
|
I want mesure a length of a line by using pixel.Can u send me C# coding for that
fbgsd asgtdfg
|
|
|
|
|
I've compiled the source by using the bat file but I'm still not able to get the Images to show up. What am I missing?
|
|
|
|
|
Check point 1...'Drag & Drop an image file from windows...", just drag one of the images in the sample zip onto the compiled window or call the following when the application is loaded:
SetBitmap(New Bitmap("full path to your image.png"))
Cheers
|
|
|
|
|
(Sorry for the poor English)
Hi,
I got a strange problem.
In my form i add something like :
[DllImport("user32.dll")]<br />
private static extern int SetParent(IntPtr hWndChild, int hwndNewParent);<br />
<br />
[DllImport("user32.dll", EntryPoint="FindWindowA")]<br />
private static extern int FindWindow(string lpClassName, string lpWindowName);<br />
<br />
private int GetDesktopWnd() {<br />
return FindWindow("Progman", null);<br />
}<br />
<br />
private void button1_click(object sender, System.EventArgs e) {<br />
SetParent(pngForm_.Handle, GetDesktopWnd);<br />
}
So when I click button1 my pngForm_ is now drawed on the desktop.
This worked great with 1 monitor.. But now I'm dual-monitoring and I got a bug when I try to move the pngForm_ when it's on the desktop. By example I hit the form when it's on monitor 1, and the form go on monitor 2 at the same position, and after it's impossible to move it.
I have made a lot of test, and i thinks it's due to the cp.ExStyle. In Fact when I remove the WS_EX_LAYERED it works..
Any idea to repair that ?
-- modified at 8:51 Saturday 7th October, 2006
|
|
|
|
|
Isn't it easier to use Forms.Opacity?
|
|
|
|
|
Đm, moebiusproject đúng là thằng ngu chẳng hiểu cái đ*'o gì cả!
|
|
|
|
|
No....because that won't give you an alphablended gui. it will just have 1 level of transparency
|
|
|
|
|
Hi,
This is some great code, I pissed around for ages trying to get this working before I stumbled across this page.
Now, it works just fine, the only thing is that forms descended from a PerPixelAlphaForm don't work in the VS2005 designer, you just get:
One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
Error creating window handle.
This doesn't stop the project from building or running though, but it's a pain not being able to use the designer.
Any ideas?
Thanks!
|
|
|
|
|
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if(Site != null)
{
if(!Site.DesignMode)
{
cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
}
}
else
{
cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
}
return cp;
}
}
This will let you view the form in design mode. Not much use though as i havn't wokred out how to add controls yet.
|
|
|
|
|
try to recompile sources, then designer should handle it. If not try using VS 2008 Express, in my case it works fine.
|
|
|
|
|
Someone said "make the main form's Owner the shadow form",but I don't kown how to do this,Please help me!
thanks a lot!
|
|
|
|
|
I can't add Controls.Please help me.
my MSN:wangsongqing2003@hotmail.com Mail:wang.songqing@staples.sh.cn
|
|
|
|
|
Hi dear
Can you recognition coordinate of circle in bitmap?
i searching a algorithm for do this task but i cann't find
I want search a sample picture in another picture .
for example I give a circle picture to program then program search this picture in another picture ,if find get me coordinate of circle picture.
Thank
|
|
|
|
|
I was wondering if there was a way to get the same functionality using direct X. I see how you can use alpha blending for 3d objects, but how would you do it for a 2d png image and the desktop?
#teve
|
|
|
|
|
Since you called it "Form" (PerPixelAlphaForm) shouldn't we be able to add controls to it like labels and stuff?
Right now its only use seems to be the display of pictures over the desktop with full alpha support which is good, but rather limiting.
Is there any other project which implements full alpha support as a form background?
Luís Ferrão
|
|
|
|