C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 165 を表示中)
■95088 / )  Re[1]: PrintDialogをスクリーン中央に表示
□投稿者/ KOZ (129回)-(2020/06/23(Tue) 09:22:17)
No95081 (メロン さん) に返信
> x64の為、UseEXDialog=trueの状態でスクリーンの中央に表示したい。

こんな感じでどうでしょう。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DlgTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var dlg = new PrintDialog())
            {
                currentScreen = Screen.FromControl(this);
                hookHandle = NativeMethods.SetWindowsHookEx(new NativeMethods.HOOKPROC(HookProc));
                dlg.UseEXDialog = true;
                dlg.ShowDialog();
            }
        }

        static Screen currentScreen;
        static IntPtr hookHandle;

        private static IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode == NativeMethods.HCBT_ACTIVATE)
            {
                var scrRect = currentScreen.Bounds;
                var dlgRect = NativeMethods.GetWindowRectangle(wParam);
                var x = scrRect.Left + (scrRect.Width - dlgRect.Width) / 2;
                var y = scrRect.Top + (scrRect.Height - dlgRect.Height) / 2;
                NativeMethods.SetWindowPos(wParam, x, y);
                NativeMethods.UnhookWindowsHookEx(hookHandle);
            }
            return NativeMethods.CallNextHookEx(hookHandle, nCode, wParam, lParam);
        }

        static class NativeMethods
        {
            const int WH_CBT = 5;
            const int SWP_NOSIZE = 0x0001;
            const int SWP_NOZORDER = 0x0004;
            const int SWP_NOACTIVATE = 0x0010;
            public const int HCBT_ACTIVATE = 5;

            public struct RECT
            {
                public int Left, Top, Right, Bottom;
            }

            [DllImport("kernel32.dll")]
            public static extern int GetCurrentThreadId();
            
            public delegate IntPtr HOOKPROC(int nCode, IntPtr wParam, IntPtr lParam);

            [DllImport("user32.dll")]
            static extern IntPtr SetWindowsHookEx(int idHook, HOOKPROC lpfn, IntPtr hInstance, int threadId);

            [DllImport("user32.dll")]
            static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);

            [DllImport("user32.dll")]
            public static extern bool UnhookWindowsHookEx(IntPtr hHook);

            [DllImport("user32.dll")]
            public static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);

            [DllImport("user32.dll")]
            static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

            public static IntPtr SetWindowsHookEx(HOOKPROC lpfn)
            {
                return SetWindowsHookEx(WH_CBT, lpfn, IntPtr.Zero, GetCurrentThreadId());
            }

            public static Rectangle GetWindowRectangle(IntPtr hWnd)
            {
                var rc = new RECT();
                GetWindowRect(hWnd, out rc);
                return Rectangle.FromLTRB(rc.Left, rc.Top, rc.Right, rc.Bottom);
            }

            public static bool SetWindowPos(IntPtr hWnd, int x, int y)
            {
                return SetWindowPos(hWnd, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
            }
        }
    }
}

返信 編集キー/


管理者用

- Child Tree -