I am struggling to get my form to have a transparent background in vb.net
Currently in the form New I set
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true)
But still the form shows up as having the default grey background
Can anyone help??
EDIT: I need the controls on the form to be visible so I don't think setting the opacity to 0 will work
EDIT: I tried the transparency key solution but it doesn't work. I have a circular image with a black background. OnPaint I set the transparency key to the img pixel at 0,0, this then leaves me with circular image (which I want ) It hides the black background but I am still left with the default grey rectangle of the form.
below is the code I have -
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Timer1.Start()
End Sub
Private Sub frmWoll_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)
img.MakeTransparent(img.GetPixel(2, 2))
Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
Use TransparencyKey for transparent form.
eg.
TransparencyKey = Color.Red
Button1.BackColor = Color.Red
Now run the form you will find that the button1 has a hole in it.
So using this method you can create a mask image in paint for which part has to be transparent and apply that image to form and voila the form is now transparent.
Edit:
Sorry for late reply.
Following is your code modified to suit your requirement
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)
'img.MakeTransparent(img.GetPixel(2, 2))
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
Set Form's TransparencyKey color property same as form's Background color property
There are a few methods you could use.
Use the forms TransparencyKey
Override OnPaintBackground (WM_ERASEBKGND)
Override WndProc and handle the paint messages (WM_NCPAINT, WM_PAINT, etc)
I recommend overriding the window procedure to get optimal results.
Me.Opacity = 0
Be warned that:
This is for the entire form, rather than just the background. There are work-arounds to make certain parts more opague.
It's only psuedo-transparency where it takes a snapshot of what's behind it. It's smart enough to know when you move the form, but not when you move other transparent objects on top of the form.
Related
I'm using a code to draw a custom dashed border around my textboxes and forms. If I grab the app and drag it off the screen and come back the border is all smeared and bad looking.
The only way I've been able to fix it is by using Me.Reload() event making the form reload which fixes it immediately. Which is alright but I'd rather it be fixed immediately almost to the point that you never even see it happen.
When I tried to add it into a timer it made the form blink really bad obviously.
Is there a way to use this code to detect when the app leaves the boundaries of the screen and just refresh the form only when the whole app returns within the boundaries of the monitor or monitors?
Public Function IsOnScreen(ByVal form As Form) As Boolean
Dim screens() As Screen = Screen.AllScreens
For Each scrn As Screen In screens
Dim formRectangle As Rectangle = New Rectangle(form.Left, form.Top, form.Width, form.Height)
If scrn.WorkingArea.Contains(formRectangle) Then
Return True
End If
Next
Return False
End Function
EDIT: I wanted to share the code I'm using to draw these borders in case it may actually be the issue.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' This is the override paint event that will allow us to draw all our borders
addBorders(e)
End Sub
Public Sub addBorders(ByVal e As PaintEventArgs)
For Each ctl As Control In Me.Controls
Select Case True
Case TypeOf (ctl) Is TextBox
ctl.AutoSize = False
ctl.Height = 19
Dim borderRectangle As Rectangle = New Rectangle(ctl.Location, ctl.Size)
borderRectangle.Inflate(1, 1)
ControlPaint.DrawBorder(e.Graphics, borderRectangle, ctl.ForeColor, ButtonBorderStyle.Dashed)
Case TypeOf (ctl) Is ComboBox, TypeOf (ctl) Is Button
Dim borderRectangle As Rectangle = New Rectangle(ctl.Location, ctl.Size)
borderRectangle.Inflate(1, 1)
ControlPaint.DrawBorder(e.Graphics, borderRectangle, ctl.ForeColor, ButtonBorderStyle.Dashed)
>>>ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, ctl.ForeColor, ButtonBorderStyle.Dashed)<<< This is the problem!
End Select
Next
End Sub
I've tried adding TypeOf (ctl) Is Form to the second Case and that does not work and I am not sure why!
Case TypeOf (ctl) Is ComboBox, TypeOf (ctl) Is Button, TypeOf (ctl) Is Form
This code works perfect for the comboboxes and the textboxes but it does not draw the border on the form.
I'm answering my own question because I've finally got it working.
First of all I changed how I was drawing the border around the form.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' This is the override paint event that will allow us to draw all our borders
addBorders(e)
If e.ClipRectangle.X = 0 Then
Dim dashValues As Single() = {3, 1, 3, 1}
Dim p As New Pen(btnExit.ForeColor, 1)
p.DashPattern = dashValues
e.Graphics.DrawRectangle(p, 0, 0, Me.Width - 1, Me.Height - 1)
End If
End Sub
Which worked perfect! There was still a small issue with going off the screen it would still mess the border up some so I added a Me.Refresh() code on the form_mouseup event so when the form is dropped it refreshes and fixes the problem.
Private Sub frmMain_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
Me.Refresh()
End Sub
It's not exactly what I would have liked to have but it works way better this way than the other way.
I am in the process of creating my own user control in a Windows Forms application using VB.NET, and having it change in size along with the form containing it. It looks okay so long as the window size stays constant and the control stays within its original bounds. However, if I resize the window so as to make it larger, the control's contents will resize with it but end up getting clipped at the original size. I am unsure where this is coming from and have not found any way so far to fix it.
Below is some quick sample code for a user control which can reproduce the problem I'm having:
TheCircle.vb:
Public Class TheCircle
Private _graphics As Graphics
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_graphics = CreateGraphics()
SetStyle(ControlStyles.ResizeRedraw, True)
BackColor = Color.Red
End Sub
Private Sub TheCircle_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
_graphics.FillRectangle(Brushes.Blue, ClientRectangle)
_graphics.FillEllipse(Brushes.LimeGreen, ClientRectangle)
End Sub
End Class
I then place this user control after rebuilding the project in my main form and either dock it or anchor it (same result, but the latter helps better show where the clipping issues are). And below is a screenshot of my result when I attempt to resize the control beyond its "default" size:
The green ellipse and the blue "background" rectangle should occupy the entire control area, but they aren't (it's clipped and you see the red BackColor instead). It looks like it behaves as intended though when the control is at the original size or smaller. How can I fix this? I'm pretty new to GDI+, so I'm sure it must be right under my nose...
This is unnecessary, and a generally bad idea: _graphics = CreateGraphics()
It's bad because it's volatile. You get a one-time graphics object, draw something with it, and then the next refresh cycle, it's lost unless you keep doing it.
The proper approach is to use the Paint event, as it supplies you with the Graphics object in its PaintEventArgs and it is called every time a re-paint is required. You can ask for a re-paint at any point in your code by calling theCircle.Invalidate() (or Refresh() for a more immediate redraw).
Public Class TheCircle
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
SetStyle(ControlStyles.ResizeRedraw, True)
BackColor = Color.Red
End Sub
Private Sub TheCircle_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.Blue, ClientRectangle)
e.Graphics.FillEllipse(Brushes.LimeGreen, ClientRectangle)
End Sub
End Class
I have a control derived from checkbook which I called "SettingBooleanButton", but when any window or dialog is dragged over the control the control keeps signs of the drag
The next image shows the effect of dragging an application window over control
This is the code block that I have for OnPaint()
Public Class SettingBooleanButton
Inherits CheckBox
Private _settingSection As String
Private _settingName As String
Private _associatedSetting As Setting
Public Event StateChange(ByVal affectedSetting As Setting)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Appearance = Appearance.Button
FlatStyle = FlatStyle.Flat
TextAlign = ContentAlignment.MiddleCenter
AutoSize = False
End Sub
Public Property SettingSection As String
Get
Return _settingSection
End Get
Set(value As String)
_settingSection = value
End Set
End Property
Public Property SettingName As String
Get
Return _settingName
End Get
Set(value As String)
_settingName = value
End Set
End Property
''' <summary>
''' Sets a boolean value to indicate the initial checked state of the control.
''' </summary>
''' <value>
''' <c>true</c> to set it as [checked state]; otherwise, <c>false</c>.
''' </value>
Public Property CheckedState As Boolean
Get
Return Checked
End Get
Set(value As Boolean)
_associatedSetting = New Setting(_settingSection, _settingName, String.Empty)
RemoveHandler CheckedChanged, AddressOf StateChanged
Checked = value
SetText()
AddHandler CheckedChanged, AddressOf StateChanged
End Set
End Property
Private Sub StateChanged(sender As Object, e As EventArgs)
If IsNothing(_associatedSetting) Then
Return
End If
_associatedSetting.Value = Checked.ToString()
SetText()
RaiseEvent StateChange(_associatedSetting)
End Sub
Public Sub SetText()
If Checked Then
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
ForeColor = Color.WhiteSmoke
Text = Resource.SettingBooleanButton_TrueState
Else
Font = New Font(Font.FontFamily, Font.Size, FontStyle.Regular)
ForeColor = SystemColors.ControlText
Text = Resource.SettingBooleanButton_FalseState
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Checked Then
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid)
End If
End Sub
End Class
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, ...)
Using e.ClipRectangle like this is a traditional bug in a Paint event handler. It is not a rectangle that matches the border you want to draw. It is only the part of the control that needs to be painted. Which is usually the entire control, but not always. Such as in your case when you drag a window across your control, only the part that is revealed needs to be repainted. So now you are painting the border in the wrong position, producing those black lines.
You only ever use the ClipRectangle if your painting code is expensive and you want to take the opportunity to skip that expensive code when it isn't needed anyway. Which is pretty rare, clipping in Windows is already pretty efficient.
You'll need to pass the actual rectangle of your border. Fix:
ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, _
Color.Black, ButtonBorderStyle.Solid)
Sometimes the simplest solutions (or causes) are overlooked.
I have a panel with 15 buttons on it and each has an image. Depending on rows selected from a data grid they all might be enabled or disabled.
It all worked fine except toggling between enabled and disabled was taking 2+ seconds and caused lag when multi-selecting from the data grid.
Tried a few things, then I thought maybe it was something to do with the images.
The images were all in an imagelist and size was set to 24,24 which was a compromise between 32,32 and 16,16. I changed the size in the imagelist to 32,32 as that is the native size of all the images... and shazam!!! All the buttons are basically rendered instantly now. No idea ATM whether being small PNG images makes a difference... but I'm going to convert all the images I have to ICO format.
Also... as all my buttons are on a panel I enable/disable the panel which in turn enables and disables all the children on it.
I have a 1080p touchscreen application. When a modal pops up, i want to emphasize that by darkening the main form.
Right now i use a second form, the size of the main form, that is black and has 50% opacity. Whenever a modal needs to appear, i open the opaque form, and then open the desired modal.
I feel this is a bit devious for my purpose. Its also not asshole-proof that when the user alt tabs, the forms will glitch out of sequence.
Is there a better way to achieve the darkening effect. Perhaps by darkening the main form from within itself?
Solved it myself by doing the following:
Place a hidden picturebox with dock:fill on the main form,
Take a screenshot of the current screen and darken it
assign the image to the picturebox and make it visible
open the modal in a new win
when the modal is dismissed
hide the picturebox
It really stupid that VB.net doesn't have this function built into it. Here's what you do to get around it:
Make a new form and call it Shade. I'm going to assume your main form is called frmMain. For the sake of clarity, lets assume the form you're launching is called dlgX.
Add the following lines in the Load event of dlgX (that's the sub with dlgX.Load or Me.Load or MyBase.Load):
Shade.Opacity = 0.001
Shade.Show()
Shade.Location = frmMain.Location ' Form location will only update if the form is visible.
Shade.Hide()
Shade.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'This gets rid of the windows Titlebar and window border.
Shade.Size = frmMain.Size
Shade.BackColor = Color.Black
Shade.Opacity = 0.5
Shade.Show() ' Form size will only update the next time you show it.
Shade.TopMost = True ' Puts Shade over main form
Me.TopMost = True ' Puts current form over shade
Under all events that dismiss the form dlgX (OK.click, Cancel.click, etc), add the following lines:
Shade.Close
Or you can even make your own sub that handles all events where the form is closed:
Private Sub DispelShades(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
Shade.Close()
End Sub
This is way simpler than the PictureBox scenario and you don't have to mess with layering issues and having to ensure that the PictureBox renders on top of everything (for example, tabs really do not like having things rendered above them and they will not let you render a picture box above them). Rendering a black semi transparent form above your main form gets around all these headaches.
If you have multiple forms to shade, just make a Shad1, Shade2, Shade3 etc.
This is pretty obvious but it's worth stating: if you're shading the main form, you'll also want to make it unclickable by opening dlgX via dlgX.ShowDialog and not dlgX.Show
Here is some code, very similar to the method in Thomas's answer. Note to use the Darkness property in a Try...Finally block, to make sure you never leave the form in the dark state.
Public Class Form1
Private _PB As PictureBox
Public WriteOnly Property Darkness
Set(value)
If value Then
Dim Bmp = New Bitmap(Bounds.Size.Width, Bounds.Size.Height)
Me.DrawToBitmap(Bmp, New Rectangle(Point.Empty, Bounds.Size))
Using g = Graphics.FromImage(Bmp)
Dim Brush As New SolidBrush(Color.FromArgb(125, Color.Black))
g.FillRectangle(Brush, New Rectangle(Point.Empty, Bmp.Size))
End Using
_PB = New PictureBox
Me.Controls.Add(_PB)
_PB.Size = Bounds.Size
_PB.Location = Bounds.Location - PointToScreen(Point.Empty)
_PB.Image = Bmp
_PB.BringToFront()
Else
If _PB IsNot Nothing Then
Me.Controls.Remove(_PB)
_PB.Dispose()
End If
End If
End Set
End Property
Private Sub btnDialog_Click(sender As Object, e As EventArgs) Handles btnDialog.Click
Try
Darkness = True
MsgBox("Modal dialog")
Finally
Darkness = False
End Try
End Sub
End Class
I'm filling the background of some VB.NET 2005 WinForms form with a nice pretty gradient fill (by overriding the OnPaint event). This works fine but the various labels etc on the form show with a solid background even after I set the BackColor to Color.Transparent. Setting the transparency key of the form itself seems to affect this but I cannot get the labels to have a truely transparent BackColor, is there an easy way to get around this or am I looking at custom controls etc?
Add a new class to your project and paste the code shown below. Build. Drop the new control from the top of your toolbox onto your form.
Public Class TransparentLabel
Inherits Label
Public Sub New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT
Return cp
End Get
End Property
End Class
The flicker might be noticeable, no fix.
After some experimentation I've found that the following works for gradiant filling form backgrounds and preserving label transparency:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim formGraphics As Graphics = e.Graphics
Dim gradientBrush As New LinearGradientBrush(New Point(0, Height), New Point(0, 0), Me.AppSettings.FormGradiantFrom, Me.AppSettings.FormGradiantTo)
formGraphics.FillRectangle(gradientBrush, ClientRectangle)
End Sub
And in the form load event:
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or _
ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)