zoukankan      html  css  js  c++  java
  • 截获WndProc过程

    为防止点击窗体右上角的关闭按钮(X按钮)关闭窗体,我们可以覆盖WndProc过程,只要发现消息为WM_SYSCOMMAND且wparam参数为SC_CLOSE就不让继续传下去。

    Delphi代码:

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
    
    type
      TForm1 = class(TForm)
      private
        { Private declarations }
        procedure WndProc(var AMessage:TMessage);override;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.WndProc(var AMessage: TMessage);
    begin
      if (AMessage.Msg=WM_SYSCOMMAND) and (AMessage.WParam=SC_CLOSE) then
        Exit
      else
        inherited;
    
    end;
    
    end.

    C#代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_CLOSE = 0xF060;
            public Form1()
            {
                InitializeComponent();
            }
            protected override void WndProc(ref Message m)
            {
                if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_CLOSE))
                    return;
                base.WndProc(ref m);
            }
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
  • 相关阅读:
    Maven安装及配置
    Java部分概念理解
    API.day01
    随机生成10元素数组并找出最大元素(Java)
    冒泡排序(Java)
    俄罗斯方块部分功能(Java)
    判断闰年(Java)
    判断质数(Java)
    基于DSP的IS95正向业务信道模块设计
    Lua程序设计(4th) 第一部分 语言基础
  • 原文地址:https://www.cnblogs.com/yagzh2000/p/2792971.html
Copyright © 2011-2022 走看看