Getting black screen when setting wallpaper using VB.NET - vb.net

I was using the below code to programaticaly set my desktop wallpaper in VB.NET
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDWININICHANGE As Integer = &H2
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
Sub Main()
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "result.jpg", SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
End Sub
It was working fine earlier. But now when i run it, it sets my desktop as black, instead of setting the image and I am not able to find out why. Does anyone know why this happens? Does this method have any dependency?

Related

How do i prevent resising of the console in VB.NET?

I need to prevent the user of my console program from resizing the window, only allowing it to be changed programmatically. If the user changes the width or hight, everything messes up. Also, I want to therefore disable/remove the maximise button. I belive that it was previously possible in VB.Net 2015 [See this answer]. However everything i am seeing is outdated and doesn't work.
I also need to prevent resizing of the window when it is snapped to a corner.
This was the old VB.Net code:
`Module Module1
Private Const MF_BYCOMMAND As Integer = &H0
Public Const SC_CLOSE As Integer = &HF060
Public Const SC_MINIMIZE As Integer = &HF020
Public Const SC_MAXIMIZE As Integer = &HF030
Public Const SC_SIZE As Integer = &HF000
Friend Declare Function DeleteMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Friend Declare Function GetSystemMenu Lib "user32.dll" (hWnd As IntPtr, bRevert As Boolean) As IntPtr
Sub Main()
Dim handle As IntPtr
handle = Process.GetCurrentProcess.MainWindowHandle ' Get the handle to the console window
Dim sysMenu As IntPtr
sysMenu = GetSystemMenu(handle, False) ' Get the handle to the system menu of the console window
If handle <> IntPtr.Zero Then
DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND) ' To prevent user from closing console window
DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND) 'To prevent user from minimizing console window
DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND) 'To prevent user from maximizing console window
DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND) 'To prevent the use from re-sizing console window
End If
Do Until (Console.ReadKey.Key = ConsoleKey.Escape)
'This loop keeps the console window open until you press escape
Loop
End Sub
End Module
`
I have also considered using console.setbuffersize but wouldn't know how to have the programm continually set the buffer to that size as my program is across a lot of subroutines.
This will prevent the console window from being resized by the user. First, add this into your module:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)>
Friend Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("user32.dll")>
Friend Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rect As RECT) As Boolean
End Function
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)>
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Then in your Main sub add this code, before anything else:
Dim CurrentProcess As Process = Process.GetCurrentProcess()
Dim hWnd As IntPtr = CurrentProcess.MainWindowHandle
Dim RECT As New RECT()
GetWindowRect(hWnd, RECT)
Dim Width As Integer = RECT.Right - RECT.Left
Dim Height As Integer = RECT.Bottom - RECT.Top
Dim X As Integer = RECT.Left
Dim Y As Integer = RECT.Top
Dim WorkerThread As New Threading.Thread(Sub()
While (True)
MoveWindow(CurrentProcess.MainWindowHandle, X, Y, Width, Height, True)
End While
End Sub)
WorkerThread.Start()
This will allow the program to change the width, height or position of the window, by changing the values of the variables. Here is an example:
Width = 200
Height = 150
You would place that somewhere else in the Main sub.
I assume that your using a .Net Framework, not .Net Core.

Writing text to console buffer

I am just learning programming, so please be understanding :)
I am writing an application that needs to send text data to another console program. I was successful with SendMessage(), but unfortunately with Wine (on linux) it is not supported for console applications.
So I found a function that is supported. This is WriteConsoleInput().
Here is my code, could someone indicate why it doesn't work?
Public Declare Function AttachConsole Lib "kernel32" (ByVal ProcessID As Long) As Boolean
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Int32) As IntPtr
Declare Function WriteConsoleInputW Lib "kernel32" Alias "WriteConsoleInputW" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Integer
Declare Function WriteConsoleInputA Lib "kernel32" Alias "WriteConsoleInputA" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Integer
Declare Function SetConsoleTitle Lib "kernel32.dll" Alias "SetConsoleTitleA" (lpConsoleTitle As String) As Boolean
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_ERROR_HANDLE As Long = -12&
Private Const INVALID_HANDLE_VALUE As Long = -1&
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim titleStr As String = "Test"
Dim textToWrite As Char() = CType("test", Char())
Dim stdIN As Integer
Dim stdOUT As Integer
Dim charwritten As Integer = 0
Dim ret As Integer
If ReturnCMDProcessID() = 0 Then
MessageBox.Show("CMD off")
Exit Sub
End If
AttachConsole(ReturnCMDProcessID())
stdIN = CInt(GetStdHandle(CInt(STD_INPUT_HANDLE)))
stdOUT = CInt(GetStdHandle(CInt(STD_OUTPUT_HANDLE)))
ret = WriteConsoleInputA(stdIN, textToWrite, textToWrite.Length, charwritten)
SetConsoleTitle(titleStr)
End Sub
Did you try the "built-in" way to do this?
System.Console:
https://learn.microsoft.com/en-us/dotnet/api/system.console?view=netframework-4.8
And in particular Console.In
https://learn.microsoft.com/en-us/dotnet/api/system.console.in?view=netframework-4.8
And Console.Out
https://learn.microsoft.com/en-us/dotnet/api/system.console.out?view=netframework-4.8
I also just noticed you are setting the title, that is as simple as Console.Title = "My Title"

VB6 - Issues with being in the background to another program window?

I have created a VB6 program which runs in the background to another program. It means the program window will be in the back only to this other program. I am using this code for it,
Private Declare Function FindWindow1 Lib "User32" Alias "FindWindowA" (ByVal lpclassname As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_HWNDPARENT = -8
Private parenthwnd As Long
Private strTitle As String
Private Sub Form_Load()
strTitle = "My Program" 'Title of the program window
parenthwnd = FindWindow1(vbNullString, strTitle)
Dim R As Long
R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd)
End Sub
The other program will run this VB6 program which sets it-self to the background to the other program window. It works. But there are two problems.
When the VB6 program executes the code R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd), the other program along with VB6 program goes to the background. How to make other program active when it is run and the VB6 program is executed?
When the other program is closed, it has the code to terminate the VB6 program. But this does not close the VB6 program. I think this may be due to running the code R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd). How to fix this?
If I understand you question correctly (and I'm not sure I do) here's the code I use to send a form to the background or send the form to the top. Perhaps it's what you're looking for.
' Declares and constants for BringToFront and SendToBack
Public Declare Function SetWindowPos Lib "user32" (ByVal _
hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As _
Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As _
Long, ByVal wFlags As Long) As Long
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_BOTTOM = 1
Public Const HWND_TOP = 0
Public Sub BringToFront(frm As Form)
Dim flags As Long
flags = SWP_NOSIZE Or SWP_NOMOVE
SetWindowPos frm.hWnd, HWND_TOP, 0, 0, 0, 0, flags
End Sub
Public Sub SendToBack(frm As Form)
Dim flags As Long
flags = SWP_NOSIZE Or SWP_NOMOVE
SetWindowPos frm.hWnd, HWND_BOTTOM, 0, 0, 0, 0, flags
End Sub

how to check the master volume is muted or not

how to check the master volume is muted in windows 7 OS
I have the code for mute or unmute
i.e
Public Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Public Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Public Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
Public Const WM_APPCOMMAND As Integer = &H319
Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
MM.SendMessageW(Me.Handle, MM.WM_APPCOMMAND, Me.Handle, CType(MM.APPCOMMAND_VOLUME_MUTE, IntPtr))
here i wont to check only mute condition of master valume.
than q in advance.
You can easily check by "core audio.dll" api.
download coreaudio.dll from here
Refernce it in your project and..
than use the following code:
private function getmute() as boolean
Dim devenum As New MMDeviceEnumerator
Dim device As MMDevice = devenum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
If device.AudioEndpointVolume.Mute = True Then
Return True
Else
Return False
End If
End Function
hope this works....
(sorry for bad english)

LoadKeyboardLayout() Function does not work in VB 2010

I want to change my default keyboard Layout for a installed another Keyboard Layout using my VB application.I googled about this and find Function LoadKeyboardLayout() Function to do that.But Is this support in vb 2010.When I wrote below code and there is no syntax error.But when I run the program there is an error called "PInvokeStackImbalance was detected... "
How can I solve this in vb 2010.
Here is my code:
Private Const KLF_ACTIVATE As Long = &H1
Private Const KLF_NOTELLSHELL As Long = &H80
Private Const KLF_REORDER As Long = &H8
Private Const KLF_REPLACELANG As Long = &H10
Private Const KLF_RESET As Long = &H40000000
Private Const KLF_SETFORPROCESS As Long = &H100
Private Const KLF_SHIFTLOCK As Long = &H10000
Private Const KLF_SUBSTITUTE_OK As Long = &H2
Private Const KLF_UNLOADPREVIOUS As Long = &H4
Private Declare Function LoadKeyboardLayout _
Lib "user32.dll" _
Alias "LoadKeyboardLayoutA" ( _
ByVal pwszKLID As String, _
ByVal flags As Long) As Long
'Inside a button click event
LoadKeyboardLayout("00000409", KLF_ACTIVATE)
Can anyone help me...
I think all you have to do is switch the longs to integers...
Private Declare Function LoadKeyboardLayout Lib "user32.dll" _
Alias "LoadKeyboardLayoutA" ( ByVal pwszKLID As String, _
ByVal flags As Integer) As Integer