【解決方法】C# で画面上に描画したピクセルをクリア (透明にする) ことはできますか?

プログラミングQA


フォームを使用しないプログラムの上にWindows画面で描いたものを描画してから非表示にする方法、サブリミナル画像プログラムを作成しています.3秒ごとに100ミリ秒ごとにpicを表示してから非表示にします、私は作成しましたフォームを使用しますが、フォームはWindowsのフォーカスを取得し、マウスのクリックを食べ、おそらくオンのときにキーボードを押します。

または、フォームがフォーカスされないようにし、マウスのクリックを食べないようにする方法は?

また、私のプログラムは画面の境界を誤って検出します (表示ズームと作業領域の問題が原因かもしれません)。左上付近? または、画面全体の境界からポイントで毎回写真を表示することさえありますか?

私の画面は 4K (3840×2160) で、ここでは 3072×1728 として検出されます。

Screen.PrimaryScreen.Bounds

そして、プライマリ画面のディスプレイズームを125%にしましたが、コンボボックスが有効になっておらず空であるため、何らかの理由でDsiplay設定で表示できません編集:今、私はそれを見ることができます..

私が試したこと:

public partial class FormPic : Form
    {

        Image image;


        BackgroundWorker bgwLoadImage = new BackgroundWorker();


        public FormPic(string pathToPic)
        {
            InitializeComponent();

            

            timerCloseForm.Interval = Program.durationOfDisplay;

            image = Image.FromFile(pathToPic);


            //if I do this the form gets out of the primary screen and covers a part of the right screen
            //Rectangle s = getSizeAndLoc(new Size(3840,2160), new Size(3840,2160));




            //my screen is 4K(3840x2160) and it is detected here as: 3072x1728
            //this.Bounds= getSizeAndLoc(image.Size, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));




            //also here my screen is 4K(3840x2160) and it is detected as: 3072x1728 in System.Windows.SystemParameters.PrimaryScreenWidth and System.Windows.SystemParameters.PrimaryScreenHeight
            this.Bounds= getSizeAndLoc(image.Size, new Size((int)System.Windows.SystemParameters.PrimaryScreenWidth, (int)System.Windows.SystemParameters.PrimaryScreenHeight));






            // to not freeze the program on loading the image and make it load async
            pictureBoxPic.WaitOnLoad = false;

            


            
            // Load the image asynchronously.
            bgwLoadImage.DoWork += BgwLoadImage_DoWork;
            bgwLoadImage.RunWorkerAsync();
        }



        private void BgwLoadImage_DoWork(object sender, DoWorkEventArgs e)
        {
            pictureBoxPic.Image = image;
        }


        

        

        Rectangle getSizeAndLoc(Size s, Size b)
        {
            double ratio;

            if ((double)s.Width / b.Width < (double)s.Height / b.Height)
            {
                ratio = (double)b.Height / s.Height;
            }
            else
            {
                ratio = (double)b.Width / s.Width;
            }

            int w = (int)(s.Width * ratio);
            int h = (int)(s.Height * ratio);

            return new Rectangle(
                  (b.Width - w) / 2
                , (b.Height - h) / 2,
                w, h
                );
        }

        private void timerCloseForm_Tick(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void pictureBoxPic_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.Show();
        }

        

        private void FormPic_Shown(object sender, EventArgs e)
        {
            timerCloseForm.Start();
        }
    }

解決策 1

        [DllImport("user32.dll", SetLastError = true)]
        private static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);





        [DllImport("user32.dll")]

        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);




        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);




        public const int GWL_EXSTYLE = -20;

        public const int WS_EX_LAYERED = 0x80000;

        public const int WS_EX_TRANSPARENT = 0x20;

        public const int LWA_ALPHA = 0x2;

        public const int LWA_COLORKEY = 0x1;








public FormPic()
        {
            InitializeComponent();

            //make the windows click through-able
            SetWindowLong(this.Handle, GWL_EXSTYLE,
(IntPtr)(GetWindowLong(this.Handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT));
          }






private void pictureBoxPic_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
           

            //set transparency to from 0 to 255
            SetLayeredWindowAttributes(this.Handle, 0, Properties.Settings.Default.picTransparency, LWA_ALPHA);
            timerCloseForm.Start();


            this.Bounds = getSizeAndLoc(image.Size, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
        }

コメント

タイトルとURLをコピーしました