纯代码创建按钮

  UIButton *btn = [[UIButton alloc] init];  开辟初始化 按钮

    按钮有normal highlighted 等状态 所以 不能直接用 text之类的

    [btn setTitle:@"啦啦啦" forState:UIControlStateNormal];  不同

    [btn setTitle:@"shaX" forState:UIControlStateHighlighted];状态下的文字

    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];不同

    [btn  setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]状态下的颜色;

    UIImage *imaNormal = [UIImage imageNamed:@"btn02"];不同状态下的 

    UIImage *imaHighlighted = [UIImage imageNamed:@"btn03"];背景图片

    [btn setBackgroundImage:imaNormal forState:UIControlStateNormal];将片加载

    [btn setBackgroundImage:imaHighlighted forState:UIControlStateHighlighted];到按钮背景 的方法

    

    btn.frame = CGRectMake(50, 100, 100, 100) ;很重要的一步哦   设置按钮的  位置大小

    

    [self.view addSubview:btn];   将做好的按钮控件加载到view上

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Imports System.ComponentModel _ Public Class DSButton Private _ButtonColor As Color = Color.White Private SF As New System.Drawing.StringFormat Private _Text As String Public Property ButtonColor As Color Get Return _ButtonColor End Get Set(ByVal value As Color) _ButtonColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Public Property RoundRectValue As Integer = 10 Private nIndex As Integer = 0 Private IsMouseEnter As Boolean = False Public Property IsShowAnimate As Boolean = False Public Property ButtonText As String Get Return _Text End Get Set(ByVal value As String) _Text = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private _TextColor As Color = Color.White Public Property TextColor As Color Get Return _TextColor End Get Set(ByVal value As Color) _TextColor = value MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Set End Property Private Sub DSButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetStyle(ControlStyles.UserPaint, True) SetStyle(ControlStyles.AllPaintingInWmPaint, True) SetStyle(ControlStyles.ResizeRedraw, True) SetStyle(ControlStyles.Selectable, True) SF.LineAlignment = StringAlignment.Center SF.Alignment = StringAlignment.Center MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * 50, ButtonColor.G / 255 * 50, ButtonColor.B / 255 * 50)) End Sub Private Sub MakeRoundedRect(ByVal Rounded As Integer, ByVal Ct As Control, ByVal ButtonColor As Color) If Ct.BackgroundImage IsNot Nothing Then Ct.BackgroundImage.Dispose() Ct.BackgroundImage = New Bitmap(Ct.Width, Ct.Height) Dim WW, HH As Integer WW = Ct.Width - 1 HH = Ct.Height - 1 Using G As Graphics = Graphics.FromImage(Ct.BackgroundImage) G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(0, 0, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 0, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH - Rounded, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(0, HH - Rounded, Rounded, Rounded), 90, 90) Gp.AddLine(New Point(0, HH - Rounded), New Point(0, Rounded / 2)) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH), ControlPaint.Dark(ButtonColor, 0.5), ButtonColor) G.FillPath(Lg, Gp) G.DrawPath(Pens.Black, Gp) End Using End Using WW = WW - 3 HH = HH - 3 Using Gp As New Drawing2D.GraphicsPath Gp.AddArc(New Rectangle(3, 3, Rounded, Rounded), 180, 90) Gp.AddArc(New Rectangle(WW - Rounded, 3, Rounded, Rounded), -90, 90) Gp.AddArc(New Rectangle(WW - Rounded, HH / 2 - Rounded - 1, Rounded, Rounded), 0, 90) Gp.AddArc(New Rectangle(3, HH / 2 - Rounded - 1, Rounded, Rounded), 90, 90) Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, HH / 2), Color.FromArgb(220, 255, 255, 255), Color.FromArgb(50, 255, 255, 255)) G.FillPath(Lg, Gp) End Using End Using Using Gp As New Drawing2D.GraphicsPath Gp.AddEllipse(New Rectangle(3, HH / 2 + 10, WW, HH / 2)) Using Lg As New Drawing2D.PathGradientBrush(Gp) Lg.CenterColor = Color.FromArgb(150, 255, 255, 255) Lg.SurroundColors = New Color() {Color.Transparent} Gp.FillMode = Drawing2D.FillMode.Winding G.FillPath(Lg, Gp) End Using End Using Try If _Text.Length 0 Then G.DrawString(_Text, Me.Font, New SolidBrush(TextColor), New Rectangle(0, 0, Me.Width, Me.Height), SF) Catch End Try End Using End Sub Private Sub DSButton_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick End Sub Private Sub DSButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If e.Button = MouseButtons.Left Then MakeRoundedRect(RoundRectValue, Me, Color.Black) End If End Sub Private Sub DSButton_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter If DesignMode = False Then IsMouseEnter = True Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave If DesignMode = False Then IsMouseEnter = False Timer1.Enabled = True End If End Sub Private Sub DSButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp MakeRoundedRect(RoundRectValue, Me, _ButtonColor) End Sub Private Sub DSButton_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged If Me.IsHandleCreated Then MakeRoundedRect(RoundRectValue, Me, ButtonColor) End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Select Case IsMouseEnter Case True If IsShowAnimate = True Then nIndex = IIf(nIndex + 30 >= 225, 255, nIndex + 30) If nIndex >= 255 Then Timer1.Enabled = False Else nIndex = 255 Timer1.Enabled = False End If Case False nIndex = IIf(nIndex - 20 <= 50, 50, nIndex - 20) If nIndex <= 50 Then Timer1.Enabled = False End Select Try MakeRoundedRect(RoundRectValue, Me, Color.FromArgb(255, ButtonColor.R / 255 * nIndex, ButtonColor.G / 255 * nIndex, ButtonColor.B / 255 * nIndex)) Catch End Try End Sub End Class
### 回答1: Matlab是一款强大的科学计算软件,可以进行数据分析、可视化、建模等众多操作。在Matlab中,可以使用GUIDE工具箱来设计GUI界面,方便用户交互和操作。下面将介绍如何使用Matlab代码设计GUI。 首先,需要创建一个Figure对象,即窗口。可以使用以下代码创建一个窗口: ```matlab fig = figure('Name','MyGUI','Position',[400,400,400,300]); ``` 其中,‘MyGUI’是窗口名称,‘Position’设置窗口的位置和大小。 然后,需要添加一些控件,如按钮、文本框等。可以使用uicontrol函数来创建控件。例如,创建一个按钮: ```matlab btn1 = uicontrol('Style','pushbutton','String','Click me','Position',[50,50,100,30]); ``` 其中,‘pushbutton’表示按钮类型,‘String’设置按钮文字,‘Position’设置按钮的位置和大小。 接下来,需要为控件添加回调函数,以实现控件的功能。可以使用以下代码按钮添加回调函数: ```matlab set(btn1,'Callback',@btn1_callback); function btn1_callback(hObject,eventdata) disp('Button clicked'); end ``` 其中,‘set’函数用于设置控件属性,‘Callback’用于设置回调函数。按钮的回调函数由‘btn1_callback’函数实现,当用户点击按钮时,屏幕将显示‘Button clicked’。 最后,需要运行GUI界面,使其显示在屏幕上。可以使用以下代码实现: ```matlab guidata(fig,handles); ``` 其中,‘guidata’用于将数据存储在Figure对象中。 以上便是使用Matlab代码设计GUI的基本流程及示例。随着GUI设计的深入,还可以添加更多控件和功能,实现更复杂的应用程序。 ### 回答2: MATLAB是一款十分优秀的科学计算软件,它拥有强大的数据分析和图形化处理能力,可以广泛应用于各种领域中。在MATLAB中,GUI是用来与用户交互的一种方式,它允许用户通过可视化的界面来进行各种操作,从而改变程序的行为。在MATLAB中,通过编写代码来设计GUI是一种非常方便和实用的方法。 首先,我们需要了解MATLAB中GUI的基本概念和构成。在MATLAB中,GUI主要由控件、回调函数和布局组成。控件包括按钮、文本框、下拉框等等,它们是用来接收用户输入和输出信息的。回调函数是用来处理控件的交互事件的,比如按钮点击、文本框输入等等。布局则是用来安排控件和界面的显示位置和大小的。 MATLAB中GUI的设计过程一般可以分为以下几个基本步骤: 1. 创建GUI窗口 通过MATLAB的uifigure函数来创建GUI窗口,设置窗口的大小和位置等参数。通过设置窗口的Name和Tag属性可以方便后续的控件查找和操作。 2. 添加控件和回调函数 通过MATLAB的各种控件函数来创建各种控件,比如uicontrol、uipanel等等。为每个控件添加回调函数,用来处理控件的交互事件。回调函数可以是已经编写好的.m文件,也可以是匿名函数或字符串形式的MATLAB命令。 3. 设置控件的属性和布局 对于每个控件,可以设置它们的属性,比如位置、大小、背景颜色等等。通过设置控件的Parent属性可以将控件添加到窗口或其他容器控件中。通过设置容器控件的布局方式,可以实现界面的排版、大小和位置的控制。 4. 运行GUI程序 通过MATLAB中的uiwait函数来启动GUI程序,等待用户的交互事件。一旦有事件到来,就会调用相应的回调函数来处理事件。可以通过设置窗口的CloseRequestFcn属性来处理GUI窗口的关闭事件。 综上所述,MATLAB代码设计GUI的方法很简单,需要掌握的主要是GUI的基本概念和构成,以及各种控件的属性和回调函数的使用。通过详细的代码实现和不断的实践,可以逐渐提高自己的GUI设计能力,开发出更为实用和美观的GUI程序。 ### 回答3: MATLAB 是一种十分强大的程序设计语言,它可以用于进行复杂的计算和数据分析。除此之外,MATLAB 还可以用来设计图形用户界面(GUI),方便用户进行交互式操作。一般情况下,MATLAB GUI 有两种设计方法:一种是使用 GUIDE 工具箱,另一种是使用代码进行设计。在本文中,我们将介绍如何使用代码的方式来设计 MATLAB GUI。 1. 建立主窗口 我们首先需要建立一个主窗口,可以使用 uifigure 函数来实现。该函数的参数包括宽度、高度、标题、以及回调函数等。 例如: ```matlab fig = uifigure('Name','My GUI','Position',[100 100 800 600]); ``` 这个命令创建了一个名为“ My GUI ”的 GUI 窗口,宽度为800像素,高度为600像素,并将窗口定位在屏幕上的(100,100)位置。窗口的句柄将存储在 fig 变量中。 2. 添加控件 接下来,我们可以向主窗口中添加各种控件(例如按钮、文本框、图像等等)。MATLAB 中有许多 UI 控件可供选择,如 uibutton, uieditfield, uitable 等。具体控件的属性和方法可以通过 MATLAB 的文档进行查询。 例如,我们可以向主窗口中添加一个 pushbutton 按钮,用于打开一个文件: ```matlab btn = uibutton(fig,'push','Text','Open file','Position',[10 10 80 30],'Callback',@openFile); ``` 这将创建一个名为 openFile 函数的回调函数,该函数用于打开一个文本文件并在命令窗口中显示其内容。 3. 设计回调函数 回调函数是一种在用户与 GUI 交互时调用的函数。在 MATLAB 中,回调函数可以用作响应各种事件的处理程序,例如单击按钮、选择列表框、移动滚动条等。回调函数可以使用各种 MATLAB 的函数、变量和操作来实现特定的目的。 例如,我们可以编写一个名为 openFile 的回调函数,用于打开一个文本文件并在命令窗口中显示其内容: ```matlab function openFile(src,evt) [filename, pathname] = uigetfile('*.txt', 'Select a text file'); if isequal(filename,0) disp('User selected Cancel') else fileID = fopen(fullfile(pathname,filename)); fileText = textscan(fileID,'%s','Delimiter','\n'); fclose(fileID); disp(fileText{:}) end end ``` 该函数使用 uigetfile 函数来打开一个文本文件,然后使用 textscan 函数读取文件中的所有行,并分别显示它们。 4. 运行 GUI 最后,我们需要使用 MATLAB 的 uiwait 函数来运行 GUI。该函数可以阻止 MATLAB 继续向下执行,直到 GUI 窗口被关闭。 例如: ```matlab uiwait(fig); ``` 这将防止 MATLAB 继续向下执行,直到 GUI 窗口被关闭为止。 总之,MATLAB 代码设计 GUI 需要以下步骤:创建主窗口、添加控件、设计回调函数,并运行 GUI。在 GUI 设计过程中,我们需要考虑到用户的体验,使其易于使用,美观实用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值