程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 學習 Message(22): WM_SYSCOMMAND 消息[六] : 系統菜單綜合操作示例

學習 Message(22): WM_SYSCOMMAND 消息[六] : 系統菜單綜合操作示例

編輯:Delphi

 本例效果圖:

學習 Message(22): WM_SYSCOMMAND 消息[六] : 系統菜單綜合操作示例

  代碼文件:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  Button3: TButton;
  Button4: TButton;
  Button5: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure Button3Click(Sender: TObject);
  procedure Button4Click(Sender: TObject);
  procedure Button5Click(Sender: TObject);
  procedure FormCreate(Sender: TObject);
 protected
  procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

{定義兩個菜單命令 ID 常數, 自定義的此類常數應該小於 $F000, 因為系統使用的都大於這個數}
const
 MenuCmdID1 = 101;
 MenuCmdID2 = 102;

{添加}
procedure TForm1.Button1Click(Sender: TObject);
var
 h: HMENU;
begin
 {系統菜單句柄}
 h := GetSystemMenu(Handle, False);
 AppendMenu(h, MFT_STRING, MenuCmdID1, '新添加的菜單項');
end;

{插入}
procedure TForm1.Button2Click(Sender: TObject);
var
 h: HMENU;
begin
 h := GetSystemMenu(Handle, False);
 {這是插入在 "還原" 菜單(SC_RESTORE) 前面}
 InsertMenu(h, SC_RESTORE, MFT_STRING, MenuCmdID2, '插入的菜單項');
end;

{修改}
procedure TForm1.Button3Click(Sender: TObject);
var
 h: HMENU;
begin
 h := GetSystemMenu(Handle, False);
 ModifyMenu(h, SC_MOVE, MF_BYCOMMAND, SC_MOVE, '這是我改的');
end;

{刪除}
procedure TForm1.Button4Click(Sender: TObject);
var
 h: HMENU;
begin
 h := GetSystemMenu(Handle, False);
 {這是刪除的 "關閉" 菜單, 此時窗口的關閉按鈕會變灰}
 DeleteMenu(h, SC_CLOSE, MF_BYCOMMAND);
end;

{恢復}
procedure TForm1.Button5Click(Sender: TObject);
begin
 GetSystemMenu(Handle, True);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Button1.Caption := '添加';
 Button2.Caption := '插入';
 Button3.Caption := '修改';
 Button4.Caption := '刪除';
 Button5.Caption := '恢復';
end;

procedure TForm1.WMSysCommand(var Message: TWMSysCommand);
var
 h: HMENU;
 buf: array[Byte] of Char;
begin
 h := GetSystemMenu(Handle, False);
 case Message.CmdType of
  MenuCmdID1: begin
   GetMenuString(h, MenuCmdID1, buf, Length(buf), MF_BYCOMMAND);
   ShowMessage(buf);
  end;
  MenuCmdID2: begin
   GetMenuString(h, MenuCmdID2, buf, Length(buf), MF_BYCOMMAND);
   ShowMessage(buf);
  end;
 end;
 inherited;
end;

end.


 窗體文件:

object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClIEntHeight = 191
 ClIEntWidth = 206
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 OnCreate = FormCreate
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
  Left = 48
  Top = 20
  Width = 75
  Height = 25
  Caption = 'Button1'
  TabOrder = 0
  OnClick = Button1Click
 end
 object Button2: TButton
  Left = 48
  Top = 51
  Width = 75
  Height = 25
  Caption = 'Button2'
  TabOrder = 1
  OnClick = Button2Click
 end
 object Button3: TButton
  Left = 48
  Top = 82
  Width = 75
  Height = 25
  Caption = 'Button3'
  TabOrder = 2
  OnClick = Button3Click
 end
 object Button4: TButton
  Left = 48
  Top = 113
  Width = 75
  Height = 25
  Caption = 'Button4'
  TabOrder = 3
  OnClick = Button4Click
 end
 object Button5: TButton
  Left = 48
  Top = 144
  Width = 75
  Height = 25
  Caption = 'Button5'
  TabOrder = 4
  OnClick = Button5Click
 end
end


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved