Change the picture position for the desktop background in VB - vb.net

I have wrote this code to change the desktop background using the function SystemParametersInfo in the Windows API user32.dll :
Imports System.IO
Public Class Form1
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" (ByVal X As Integer, ByVal Y As Integer, ByVal Z As String, ByVal W As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
SystemParametersInfo(20, 0, Application.StartupPath & "\Frames\animation" & FrameX & ".png", 1 Or 2)
Catch ex As Exception : End Try
End Sub
End Class
What I need to know if I can change the picture position of that desktop background - the same property when changing the background from the Control Panel - as shown below :
UPDATE :
I found the right solution here.

Related

Continuously change the title of a cmd window in the background

I use the code down below to start a cmd window and then change it's title. This works, but the new title is changed back to the default title, as soon as you use a copy/paste function in this window.
So I'm looking for some code which changes the title after completing MyBase.load and which continues running in the background, as long as the cmd process exists. However, I do not know how to do this.
Can somebody help me out here with some kind of example? Thanks for any help in advance!
Kind regards, Eric
Imports System.Threading
Public Class Form1
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim h_wnd As Integer
Dim proc As New Process
proc = Process.Start("cmd.exe")
Thread.Sleep(10000)
h_wnd = proc.MainWindowHandle
SetWindowText(h_wnd, "My new Window")
End Sub
End Class

Visual studio Progressbar control: blue color

using the progressbar control i can only use the default green one.
Looking in windows 7 i have noticed this control, see image below.
I found this article:
Windows ProgressBar
My question is, how to use this control in Visual Studio 2013 labelling a blue progress bar?
thanks
Edit: i would like the blue color, not the red, yellow or green one. This control is called "meter".
Sorry but its not Accessible as stated:
In Windows Vista, progressbars come in different flavors, the most
common progressbar being the green one. However, there are also the
red and yellow versions as well (there is a blue version, known as a
meter, but that is inaccessible). The progressbar colors seem to
correspond to specific progressbar states. You can set these states
using the PBM_SETSTATE [0x40F] message. The states are PBST_NORMAL
[0x0001], PBST_ERROR [0x0002] and PBST_PAUSE [0x0003].
But if you are interested it is possible to access the Red and Yellow Color
using the following:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Enum ProgressBarColor
Green = &H1
Red = &H2
Yellow = &H3
End Enum
Private Shared Sub ChangeProgBarColor(ByVal ProgressBar_Name As ProgressBar, ByVal ProgressBar_Color As ProgressBarColor)
SendMessage(ProgressBar_Name.Handle, &H410, ProgressBar_Color, 0)
End Sub
Example of Usage:
ChangeProgBarColor(Progress_Bar, ProgressBarColor.Red)
I found a way to change the colors, but no blue...
But I suppose it's done via the same way
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyProgressBar1.pdColor = myProgressBar.ProgressBarColor.red
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MyProgressBar1.Increment(1)
End Sub
End Class
Public Class myProgressBar
Inherits ProgressBar
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Sub New()
End Sub
Enum ProgressBarColor
green = 1
red = 2
yellow = 3
End Enum
Private pbColor As ProgressBarColor
Public Property pdColor As ProgressBarColor
Get
Return pbColor
End Get
Set(value As ProgressBarColor)
pbColor = value
SendMessage(Me.Handle, 1040, value, 0)
End Set
End Property
End Class

how to put winform behind all open programs vb.net

I'm calling a vb.net exe from a program in java, and i want it to stay behind all of the already opened programs.
I read that you can kinda bring to front with TopMost set to true , but i find no way to put it in the back.
There's some workarounds, like setvisible=false the previous form and setting to true after that, but that isn't quite good with my application. Thank you!
Calling the form's SendToBack (Inherited from Control) method from the Shown event handler is the key.
Calling it from load for example, won't do anything since the window isn't "ready" yet anyway.
Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
SendToBack()
End Sub
Alternatively, use the SetWindowPos native function as defined in User32.dll.
Imports System.Runtime.InteropServices
...
'Signature source: http://pinvoke.net/default.aspx/user32/SetWindowPos.html
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function
...
Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
SetWindowPos(Handle, New IntPtr(1), Location.X, Location.Y, Width, Height, 0)
End Sub
The only thing I changed from the signature provided by PInvoke.net was that I changed the "uFlags" parameter type from "SetWindowPosFlags" (Also defined on the PInvoke.net) to an Integer.

Microsoft word opening in the background

I have an application written in VB.net running on Windows 7 that opens a word document and inserts some values into it. This works fine, but on my client's machines(development works fine) Word is opening up behind my application. I've tried maximizing the document in code, but it's still opening behind my application on the client machines. Does anyone have any idea how I can fix this?
Things I've already tried:
maximizing word in my application
giving focus to word in my application
I've made sure the patches are up to date.
I've performed a repair on Office
Have you tried minimizing the application, as opposed to maximizing the document?
Me.WindowState = FormWindowState.Minimized will minimize the form that calls it (this is assuming that you are using a forms application).
You might need to bring Word to the forefront. This is a bit different from bringing a form in your app to the top.
You'll need to have a reference to two APIs, FindWindow and SetWindowPos - the first one can find the windows handle for another application that is running, and the second sends a message to the operating system to give an application focus (it uses the windows handle from FindWindow)
Here's some sample code.
Public Class Form1
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
End Function
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Shell("calc.exe")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim myHandle As IntPtr = FindWindow(Nothing, "Calculator")
SetWindowPos(myHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
SetWindowPos(myHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Sub
End Class
Clicking the first button will instantiate a copy of Calculator, the second button will make it topmost, then set it back to normal... so it will still be the top form, but the user can activate other windows as well.

How can I get the RGB color of the pixel at the cursor location using VB.NET?

How can I get the color of a pixel at the location of the cursor? I know how to get the mouses position using MousePosition but I can not figure out how to get the pixel color at that location.
A C# version: How do I get the colour of a pixel at X,Y using c# ?
Should be easy to rewrite in VB.NET.
Quick simple very slow but it works.
The idea is to copy the screen to a bitmap which can be done using the GDI+ build into the drawing.graphics object. Then simply read the bitmap that it generates. Get pixel is very slow. The best way it to read the image byte array directly.
Function MakeScreenShot() As Drawing.Bitmap
Dim out As Drawing.Bitmap
'Get the screen Size
Dim bounds As Rectangle = Screen.GetBounds(Point.Empty)
'create the bitmap
out = New Drawing.Bitmap(bounds.Width, bounds.Height)
'create a graphic object to recive the pic
Using gr As Drawing.Graphics = Graphics.FromImage(out)
'Copy the screen using built in API
gr.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size)
End Using
Return out
End Function
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim BM As Drawing.Bitmap = MakeScreenShot()
Dim mouseloc As Point = Cursor.Position
Dim c As Color = BM.GetPixel(mouseloc.X, mouseloc.Y) ' The Slowest way possable to read a color
Debug.Print(c.R & "," & c.G & "," & c.B)
End Sub
Enjoy.
Try this code:
#Region "#include"
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
#End Region
Public Class Test
#Region "From Windows API"
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
'Do not try to name this method "GetDC" it will say that user32 doesnt have GetDC !!!
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32
End Function
<DllImport("gdi32.dll", SetLastError:=True)> _
Public Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As UInteger
End Function
#End Region
REM --Test--
#Region "Some Functions"
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
Dim hdc As IntPtr = GetWindowDC(IntPtr.Zero)
Dim pixel As UInteger = GetPixel(hdc, x, y)
Dim color As Color
ReleaseDC(IntPtr.Zero, hdc)
MsgBox(pixel)
color = color.FromArgb(Int(pixel And &HFF), _
Int(pixel And &HFF00) >> 8, _
Int(pixel And &HFF0000) >> 16)
Return color
End Function
#End Region
REM --Test--
#Region "OnClick"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mouseloc As Point = Cursor.Position
Me.Label1.BackColor = GetPixelColor(mouseloc.X, mouseloc.Y)
Me.Refresh()
End Sub
#End Region
End Class
You need button (name==Button1) and label (name==Label1). To get color from screen you will need to use Timer.
If you need to rewrite code from C# to VB.NET use this link: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
Keep it simple, not very optimized but do the job:
Public Class Form1
Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function GetDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32
Private Function CheckScreen()
Dim CursorLoc As Point = Cursor.Position
Dim hDC As IntPtr = GetDC(0) '0 = Get the color to any window on screen, not only your app
Try
Dim CursorColor As Integer = GetPixel(hDC, CursorLoc.X, CursorLoc.Y)
Me.Panel1.BackColor = ColorTranslator.FromWin32(CursorColor) 'a simple panel to show the color
Me.Refresh()
Return True
Catch ex As Exception
Return False
Finally
ReleaseDC(0, hDC)
End Try
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Timer1.Start() 'Timer with a short delay
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
CheckScreen()
End Sub
End Class
This is actually harder than you would guess. I would look for some example code that already does it, and copy their technique.
In the end, the algorithm is going to have to perform these operations:
Get a bitmap of the desktop/window close to the cursor position
Index into that bitmap with the cursor position and extract the pixel color
It sounds simple, but is not easy.