当前位置: 首页 > 工具软件 > wxErlang > 使用案例 >

wxErlang sashWindow 分割窗口并且各个部分可以改变大小

范福
2023-12-01

说明:效果是一个wxNotebook 的第一个Tab 里面分割为左边和右边,左边分为上下两个部分,

三个部分大小可以拖动改变,并且捕获到窗口大小变化的事件!

-module(sashWindow3plus).

-behaviour(wx_object).

%% Client API
-export([start/1, new/0]).

%% wx_object callbacks
-export([init/1, terminate/2, code_change/3,
handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).

-include_lib("wx/include/wx.hrl").

-record(state,
{
parent,
config,
top_sash,
bottom_sash,
right
}).

-define(TOP_SASH, 1).
-define(BOTTOM_SASH, 2).
-define(TIGHT_SASH, 3).

start(Config) ->
wx_object:start_link(?MODULE, Config, []).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(Config) ->
wx:batch(fun() -> do_init(Config) end).

do_init(Config) ->
Parent = proplists:get_value(parent, Config),
{ParentWidth, ParentHeight} = wxWindow:getVirtualSize(Parent),
io:format("ParentWidth = ~p, ParentHeight =~p~n", [ParentWidth, ParentHeight]),
Panel = wxPanel:new(Parent, [{size, {ParentWidth , ParentHeight}}]),
Style = (?wxAUI_NB_BOTTOM bor
?wxAUI_NB_WINDOWLIST_BUTTON bor
?wxAUI_NB_TAB_MOVE bor ?wxAUI_NB_SCROLL_BUTTONS),
Notebook = wxAuiNotebook:new(Panel, [{style, Style},
{size, {ParentWidth , ParentHeight}}]),

InstrumentOne = wxPanel:new(Notebook, [{size, {ParentWidth , ParentHeight}}]),
%% Setup sizers
MainSizer = wxBoxSizer:new(?wxHORIZONTAL),
Sizer = wxBoxSizer:new(?wxVERTICAL),

TopSash = wxSashWindow:new(InstrumentOne, [{id, ?TOP_SASH},
                   {style, ?wxSW_3D}]),
Win1 = wxPanel:new(TopSash, []),
wxStaticText:new(Win1, ?wxID_ANY, "This is the top sash", []),

BottomSash = wxSashWindow:new(InstrumentOne, [{id, ?BOTTOM_SASH},
                  {style, ?wxSW_3D}]),
Win2 = wxPanel:new(BottomSash, []),
wxStaticText:new(Win2, ?wxID_ANY, "This is the bottom sash", []),

RightSash = wxSashWindow:new(InstrumentOne, [{id, ?TIGHT_SASH},
                  {style, ?wxSW_3D}]),
Win3 = wxPanel:new(RightSash, []),
wxStaticText:new(Win3, ?wxID_ANY, "This is the right sash", []),
%% Make the bottom edge of the top sash dragable
wxSashWindow:setSashVisible(TopSash, ?wxSASH_BOTTOM, true),
wxSashWindow:setSashVisible(RightSash, ?wxSASH_LEFT, true),
wxPanel:connect(InstrumentOne, sash_dragged),
wxPanel:connect(InstrumentOne, size),

%% Add to sizers
Options = [{flag, ?wxEXPAND}, {proportion, 1}],
wxSizer:add(Sizer, TopSash, Options),
wxSizer:add(Sizer, BottomSash, Options),
wxSizer:add(MainSizer, Sizer, Options),
wxSizer:add(MainSizer, RightSash, Options),
wxPanel:setSizer(InstrumentOne, MainSizer),
% wxSizer:fit(MainSizer, InstrumentOne),
% wxSizer:setSizeHints(MainSizer, InstrumentOne),

% PSizer = wxBoxSizer:new(?wxHORIZONTAL),
% wxSizer:add(PSizer, Notebook),
% wxPanel:setSizer(Panel, PSizer),
InstrumentThree = wxPanel:new(Notebook, []),
wxPanel:setBackgroundColour(InstrumentThree, ?wxWHITE),
wxAuiNotebook:addPage(Notebook, InstrumentOne, "策略1", []),
wxAuiNotebook:addPage(Notebook, InstrumentThree, "策略", []),
wxFrame:show(Parent),
{Panel, #state{parent=Panel,  
               right = RightSash,
               top_sash = TopSash, 
               bottom_sash = BottomSash,
               config=Config}}.

%% Async Events are handled in handle_event as in handle_info
handle_event(#wx{event = #wxSash{edge = ?wxSASH_BOTTOM, dragRect = {X,Y, W, H}}},
State = #state{top_sash = TopSash,
bottom_sash = BottomSash}) ->
io:format("wxSASH_BOTTOM~n"),
{OldW, OldH} = wxPanel:getSize(State#state.parent),
Diff = OldH - H,
{OldX, _OldY} = wxSashWindow:getPosition(BottomSash),
{SBX, SBY} = wxPanel:getSize(BottomSash),

wxSashWindow:setMinSize(TopSash, {SBX, H}),
wxSashWindow:setMinSize(BottomSash, {SBX,Diff}),
wxSashWindow:setSize(TopSash, {SBX,H}),
wxSashWindow:setSize(BottomSash, {OldX, Y,SBX, Diff}),
wxPanel:refresh(State#state.parent),
io:format("OldW = ~p, OldH = ~p, Diff ~p~n", [OldW, OldH, Diff]),
io:format("dragRect = ~pn", [{X,Y, W, H}]),
io:format("OldX = ~p, _OldY = ~p~n", [OldX, _OldY]),
io:format("now Top Width,  Heigth = ~p~n", [wxPanel:getSize(State#state.top_sash)]),
io:format("now Bottom Width,  Heigth = ~p~n", [wxPanel:getSize(State#state.bottom_sash)]),
io:format("now right Width,  Heigth = ~p~n", [wxPanel:getSize(State#state.right)]),
{noreply, State};

handle_event(#wx{event = #wxSash{edge = ?wxSASH_LEFT, dragRect = {X,Y, W, H}}},
State = #state{top_sash = TopSash,
bottom_sash = BottomSash, right = RightSash}) ->
io:format("wxSASH_LEFT~n"),
{OldW, OldH} = wxPanel:getSize(TopSash),
{TW, TH} = wxPanel:getSize(State#state.parent),
Diff = TW - W,
{OldBX, OldBY} = wxSashWindow:getPosition(BottomSash),
{OldRX, OldRY} = wxSashWindow:getPosition(RightSash),
{OldTX, OldTY} = wxSashWindow:getPosition(TopSash),
wxSashWindow:setMinSize(BottomSash, {Diff, TH - OldTY}),
wxSashWindow:setMinSize(TopSash, {Diff, OldBY}),
wxSashWindow:setMinSize(RightSash, {W, H}),
wxSashWindow:setSize(BottomSash, {OldBX, OldBY,Diff, TH - OldBY}),
wxSashWindow:setSize(TopSash, {0, 0, Diff, OldBY}),
wxSashWindow:setSize(RightSash, {Diff, 0, W, H}),
wxPanel:refresh(State#state.parent),
io:format("OldW = ~p, OldH = ~p, Diff ~p~n", [OldW, OldH, Diff]),
io:format("dragRect = ~p~n", [{X,Y, W, H}]),
io:format("now Top Width, Heigth = ~p~n", [wxPanel:getSize(State#state.top_sash)]),
io:format("now Bottom Width, Heigth = ~p~n", [wxPanel:getSize(State#state.bottom_sash)]),
io:format("now right Width, Heigth = ~p~n", [wxPanel:getSize(State#state.right)]),
{noreply, State};

handle_event(#wx{event = #wxSize{size = {W, H}}},
State = #state{top_sash = TopSash,
bottom_sash = BottomSash, right = RightSash}) ->
wxSashWindow:setMinSize(TopSash, {4 * W div 5, 2 * H div 3}),
wxSashWindow:setMinSize(BottomSash, {4 * W div 5, 1 * H div 3}),
wxSashWindow:setMinSize(RightSash, {W div 5, H }),
wxSashWindow:setSize(TopSash, {0, 0, 4 * W div 5, 2 * H div 3}),
wxSashWindow:setSize(BottomSash, {0, 2 * H div 3, 4 * W div 5, H div 3}),
wxSashWindow:setSize(RightSash, {4 * W div 5, 0, 1 * W div 5, H}),
wxPanel:refresh(State#state.parent),
{noreply, State};

handle_event(Ev = #wx{}, State = #state{}) ->
io:format("Got Event ~p\n", [Ev]),
{noreply, State}.

%% Callbacks handled as normal gen_server callbacks
handle_info(Msg, State) ->
io:format(State#state.config, "Got Info ~p\n", [Msg]),
{noreply, State}.

handle_call(shutdown, _From, State=#state{parent=Panel}) ->
wxPanel:destroy(Panel),
{stop, normal, ok, State};

handle_call(Msg, _From, State) ->
io:format(State#state.config, "Got Call ~p\n", [Msg]),
{reply,{error, nyi}, State}.

handle_cast(Msg, State) ->
io:format("Got cast ~p~n",[Msg]),
{noreply,State}.

code_change(, , State) ->
{stop, ignore, State}.

terminate(_Reason, _State) ->
ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
new() ->
_WX = wx:new(),
Size = {size, {400, 400}},
Pos = {pos, {0, 0}},
Style = {style, ?wxDEFAULT_FRAME_STYLE},
NOptions = [Pos, Size, Style],
Frame = makeFrame("sashWindow Test", NOptions),
start([{parent, Frame}]).

makeFrame(Title, Options) ->
Frame = wxFrame:new(wx:null(), ?wxID_ANY, Title, Options),
MenuSet = wxMenu:new(),
MenuHelp = wxMenu:new(),
wxMenu:append(MenuHelp, 1, "About..."),
MenuBar = wxMenuBar:new(),
wxMenuBar:append(MenuBar, MenuSet, "Setting"),
wxMenuBar:append(MenuBar, MenuHelp, "Help"),
wxFrame:setMenuBar(Frame, MenuBar),
wxFrame:createStatusBar(Frame),
wxFrame:setStatusText(Frame,"deal wxSashWindow"),
wxFrame:connect(Frame, command_menu_selected),
Frame.

转载于:https://www.cnblogs.com/ShankYan/p/4366100.html

 类似资料: