Problems with the System.Windows.Form package with C# (Visual Studio)

0

I am VERY new to C# and I'm trying to make a program that will simulate a mouse right-click when it detects sound. Here is my code...

The error I keep getting is below. I have been trying to figure out how to properly get System.Windows.Forms to work, but I do not have this solution explorer... and I'm new to visual studios so I don't know how to fix this or if I am even on the right track.

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058) File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.

Here is my code below. Everything works until I try to make my program right-click. What am I missing here? How do I fix the Systems.Windows.Forms error? I really do not know what I am doing lol but I feel like I'm so close if I can just fix this last problem.

using CSCore.CoreAudioAPI;
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Input;

namespace AudioPlayingTest
{
    class AudioPlayChecker
    {
        // Gets the default device for the system
        public static MMDevice GetDefaultRenderDevice()
        {
            using (var enumerator = new MMDeviceEnumerator())
            {
                return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            }
        }
        
            //CLASS CLICK


        // Checks if audio is playing on a certain device
        public static int IsAudioPlaying(MMDevice device)
        {
            using (var meter = AudioMeterInformation.FromDevice(device))
            {
                return (int)Math.Round(meter.PeakValue * 100);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine(IsAudioPlaying(GetDefaultRenderDevice())); 

            VirtualMouse.RightClick();
            
        }

         public static class VirtualMouse
        {
            [DllImport("user32.dll")]
            static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

            private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
            private const int MOUSEEVENTF_RIGHTUP = 0x0010;


            public static void RightClick()
            {
                mouse_event(MOUSEEVENTF_RIGHTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
                mouse_event(MOUSEEVENTF_RIGHTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
            }

        }
    }
}
c#
.net
visual-studio
reference
project
asked on Stack Overflow Jul 18, 2020 by Silas

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0