{"id":4534,"date":"2021-08-25T08:07:47","date_gmt":"2021-08-24T23:07:47","guid":{"rendered":"https:\/\/okamurax.com\/?p=4534"},"modified":"2021-08-25T08:07:47","modified_gmt":"2021-08-24T23:07:47","slug":"c-%e3%83%a1%e3%83%83%e3%82%bb%e3%83%bc%e3%82%b8%e3%83%ab%e3%83%bc%e3%83%97%e5%ae%9f%e9%a8%93","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=4534","title":{"rendered":"C# \u30e1\u30c3\u30bb\u30fc\u30b8\u30eb\u30fc\u30d7\u5b9f\u9a13"},"content":{"rendered":"<p>Form1\u306e\u30a6\u30a3\u30f3\u30c9\u30a6\u30e1\u30c3\u30bb\u30fc\u30b8\u3092PreFilterMessage\u306b\u3066\u8868\u793a\uff0b\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u3067PeekMessage()\u3092\u5229\u7528\u3057\u305f\u30eb\u30fc\u30d7\u3092\u5b9f\u884c\u3002<\/p>\n<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Windows.Forms;\r\n\r\nnamespace WindowsFormsApp1\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n            \r\n            this.Load += (s, e) =&gt;\r\n            {\r\n                var TestMessage = new TestMessage();\r\n                Application.AddMessageFilter(TestMessage);\r\n            };\r\n        }\r\n    }\r\n\r\n    class TestMessage : IMessageFilter\r\n    {\r\n        public bool PreFilterMessage(ref Message m)\r\n        {\r\n            System.Diagnostics.Debug.Print(DateTime.Now.ToString(\"HH:mm:ss.ff\") + \" \" + m.ToString());\r\n            return false;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Runtime.InteropServices;\r\nusing System.Windows.Forms;\r\n\r\nnamespace WindowsFormsApp1\r\n{\r\n    static class NativeMethods\r\n    {\r\n        [StructLayout(LayoutKind.Sequential)]\r\n        internal struct POINT\r\n        {\r\n            public int x;\r\n            public int y;\r\n        }\r\n\r\n        [StructLayout(LayoutKind.Sequential)]\r\n        internal struct MSG\r\n        {\r\n            public IntPtr hwnd;\r\n            public int message;\r\n            public IntPtr wParam;\r\n            public IntPtr lParam;\r\n            public int time;\r\n            public POINT pt;\r\n        }\r\n\r\n        [DllImport(\"user32.dll\", SetLastError = true)]\r\n        [return: MarshalAs(UnmanagedType.Bool)]\r\n        internal static extern bool PeekMessage(out MSG msg, IntPtr hWnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg);\r\n    }\r\n\r\n    \/*\r\n    \r\n    [DllImport(\"DLL\u540d\", \u30d1\u30e9\u30e1\u30fc\u30bf)]\r\n    DllImport\u304c\u6307\u5b9a\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u6307\u5b9a\u3055\u308c\u305fDLL\u306b\u5b58\u5728\u3059\u308b\u3068\u89e3\u91c8\u3055\u308c\u308b\r\n\r\n    \u30fb\u30d1\u30e9\u30e1\u30fc\u30bf\r\n    EntryPoint\uff1aDLL\u5185\u306e\u95a2\u6570\u306e\u540d\u524d\t\r\n    CharSet\uff1a\u6587\u5b57\u5217\u306e\u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u65b9\u6cd5\r\n    SetLastError\uff1aWin32\u30a8\u30e9\u30fc\u60c5\u5831\u3092\u7dad\u6301\u3059\u308b\u304b\r\n    ExactSpelling\uff1aEntryPoint\u306e\u95a2\u6570\u540d\u3092\u53b3\u5bc6\u306b\u4e00\u81f4\u3055\u305b\u308b\u304b\r\n    PreserveSig\uff1aT\u5b9a\u7fa9\u901a\u308a\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u30b7\u30b0\u30cd\u30c1\u30e3\u3092\u7dad\u6301\u3059\u308b\u304b\r\n    CallingConvention\uff1aEntryPoint\u3067\u4f7f\u7528\u3059\u308b\u30e2\u30fc\u30c9\u3092\u6307\u5b9a\u3059\u308b\r\n\r\n    \u30fb\u8fd4\u5024(\u53d7\u3051\u53d6\u308b\u3068\u304d)\uff1a\r\n    [return: MarshalAs(UnmanagedType.Bool)]\r\n\r\n    static extern int xxx([MarshalAs(UnmanagedType.LPWStr), Out] StringBuilder lpPathName);\r\n    var buff = new StringBuilder(255);\r\n    NativeMethods.xxx(buff);\r\n\r\n    \u30fb\u5f15\u6570(\u6e21\u3059\u3068\u304d)\uff1a\r\n    static extern bool xxx([MarshalAs(UnmanagedType.LPWStr), In] string xxx);\r\n\r\n    MarshalAs()\r\n    \u30de\u30fc\u30b7\u30e3\u30ea\u30f3\u30b0\u3068\u306f\u3001\u30de\u30cd\u30fc\u30b8\u30c9\u30b3\u30fc\u30c9\u3068\u30cd\u30a4\u30c6\u30a3\u30d6\u30b3\u30fc\u30c9\u306e\u9593\u3067\u3084\u308a\u53d6\u308a\u3059\u308b\u3068\u304d\u306b\u578b\u3092\u5909\u63db\u3059\u308b\u30d7\u30ed\u30bb\u30b9\u3002\r\n    \r\n    *\/\r\n\r\n    public class TestContext : ApplicationContext \r\n    {\r\n        public TestContext()\r\n        {\r\n            var f = new Form1();\r\n\r\n            ((Button)f.Controls[\"btn\"]).Click += (s, e) =&gt;\r\n            {\r\n                \/\/ \u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u306e\u30eb\u30fc\u30d7\u7d42\u4e86\r\n                MessageBox.Show(\"\");\r\n                Application.Idle -= this.TestLoop;\r\n            };\r\n\r\n            MainForm = f;\r\n            \/\/ \u30d5\u30a9\u30fc\u30e0\u306e\u8868\u793a\u65b9\u6cd5\u3002\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u3067Application.Run(new TestContext())\u3068\u3057\u305f\u5f8c\u3001\r\n            \/\/ MainForm\u306b\u6e21\u3059\u3060\u3051\u3067OK\r\n\r\n            Application.Idle += this.TestLoop;\r\n        }\r\n\r\n        private void TestLoop(object sender, EventArgs e)\r\n        {\r\n            while (!NativeMethods.PeekMessage(out NativeMethods.MSG _, IntPtr.Zero, 0, 0, 0))\r\n            {\r\n                System.Diagnostics.Debug.Print(\"\u2605\" + DateTime.Now.ToString(\"HH:mm:ss.ff\"));\r\n                System.Threading.Thread.Sleep(1);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Form1\u306e\u30a6\u30a3\u30f3\u30c9\u30a6\u30e1\u30c3\u30bb\u30fc\u30b8\u3092PreFilterMessage\u306b\u3066\u8868\u793a\uff0b\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u3067PeekMessage()\u3092\u5229\u7528\u3057\u305f\u30eb\u30fc\u30d7\u3092\u5b9f\u884c\u3002 using System; using System.Windows.Fo &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=4534\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# \u30e1\u30c3\u30bb\u30fc\u30b8\u30eb\u30fc\u30d7\u5b9f\u9a13&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-4534","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4534"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4534\/revisions"}],"predecessor-version":[{"id":4535,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/4534\/revisions\/4535"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}