当前位置: 首页 > news >正文

【PB案例学习笔记】-22制作一个语音朗读金额小应用

写在前面

这是PB案例学习笔记系列文章的第22篇,该系列文章适合具有一定PB基础的读者。

通过一个个由浅入深的编程实战案例学习,提高编程技巧,以保证小伙伴们能应付公司的各种开发需求。

文章中设计到的源码,小凡都上传到了gitee代码仓库https://gitee.com/xiezhr/pb-project-example.git

gitee代码仓库

需要源代码的小伙伴们可以自行下载查看,后续文章涉及到的案例代码也都会提交到这个仓库【pb-project-example

如果对小伙伴有所帮助,希望能给一个小星星⭐支持一下小凡。

一、小目标

上一个案例中我们将小写金额转换为大写金额,这一个案例中我们将制作一个语音播报金额的小应用。

这个在日常开发中也很常见,尤其是在收费结算应用中。最终实现效果如下

金额语音播报

二、实现思路

首先我们需要准备 零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、整

的.wav格式的语音文件。然后通过WINMM.dll外部动态库的sandPlaySoundA()和waveOutGetNumDevs()

联合起来播放语音文件

三、创建程序基本框架

① 新建examplework工作区

② 新建exampleapp应用

③ 新建w_main窗口,将其Title属性值设置成“朗读款项金额”

由于篇幅原因,以上步骤这儿就不展开了,忘记了的小伙伴翻一翻该系列文章的第一篇

④ 在w_main窗口上放置控件

在窗口上添加3个StaticEdit控件,1个singleLineEdit控件和2个CommandButton。将其分别命名为st_1st_2st_3

sle_1sle_2cb_1cb_2。 调整各个控件布局后如下

控件布局

⑤ 保存窗口

四、编写代码

① 定义本地外部扩展函数

w_mainDeclare Local External Function 选项卡中添加如下代码

Function boolean sndPlaySoundA(string SoundName, uint Flags) Library "WINMM.DLL"
Function uint waveOutGetNumDevs ()Library "WINMM.DLL"

定义扩展函数

② 在W_main窗口的Function List 选项卡中添加Playsound(string as_filename,integer ai_option) return integer函数

代码如下

uint lui_numdevslui_numdevs = WaveOutGetNumDevs() 
If lui_numdevs > 0 Then sndPlaySoundA(as_filename,ai_option)return 1
Elsereturn -1
End If

③ 在w_main窗口的Function List 选项卡中添加xx2dx(decimal ls) return string函数

代码如下

string dx_sz,dx_dw,str_int,str_dec,dx_str,fu,a,b,b2,c,d,result
long num_int,num_dec,len_int,i,a_int,ppdx_sz = "零壹贰叁肆伍陆柒捌玖" 
dx_dw = "万仟佰拾亿仟佰拾万仟佰拾元" //处理小于零情况
if ls<0 thenls = ls*(-1) fu = "负" 
else fu = "" 
end if //取得整数及整数串
dx_str = string(ls)
if (ls>0) and (ls<1) then dx_str = "0"+dx_str 
pp = pos(dx_str,".") 
if pp>0 then str_int = mid(dx_str,1,pos(dx_str,".")-1)
elsestr_int = dx_str 
end if 
num_int = long(str_int) //取得小数及小数串
if (ls>0) and (ls<1) then num_dec = ls * 100
elsenum_dec = (ls - num_int) * 100 
end if 
str_dec = string(num_dec) 
len_int = len(str_int) 
dx_str = "" //转换整整部分
for i = 1 to len_int //a为小写数字字符,b为对应的大写字符,c为对应大写单位,d为当前大写字符串的最后一个汉字a= mid(str_int,i,1) a_int = long(a) b = mid(dx_sz,(a_int*2)+1,2) c = mid(dx_dw,((13 - len_int +i - 1)*2+1),2) if dx_str<>"" thend=mid(dx_str,len(dx_str)-1,2)elsed= "" end if if (b="零") and ((d="零") or (b=b2) or (c="元") or (c="万") or (c="亿")) then  b = "" if (a="0") and (c<>"元") and (c<>"万") and (c<>"亿") then c="" if ((c="元") or (c="万") or (c="亿")) and (d="零") and (a="0") thendx_str = mid(dx_str,1,len(dx_str)-2) d=mid(dx_str,len(dx_str)-1,2) if ((c="元") and (d="万")) or ((c="万") and (d="亿")) then c = "" end if dx_str = dx_str + b+ c b2 = b 
next//处理金额小于1的情况if len(dx_str) <= 2 then dx_str= "" //转换小数部分if (num_dec<10) and (ls>0) thena_int = long(str_dec) b = mid(dx_sz,(a_int*2+1),2) if num_dec = 0 then dx_str = dx_str + "整" if num_dec > 0 then dx_str = dx_str +"零"+b+"分" end ifif num_dec >= 10 thena_int = long(mid(str_dec,1,1)) a = mid(dx_sz,(a_int*2+1),2) a_int = long(mid(str_dec,2,1)) b = mid(dx_sz,(a_int*2+1),2) if a<>"零" then a = a+"角" if b <> "零" thenb = b+"分"else b= "" end ifdx_str = dx_str + a + b end ifif ls= 0 then dx_str = "零元整" dx_str = fu+dx_str result = dx_str return result

④ 将事先准备好的.wav格式声音放到项目temp目录下

事下准备好的声音

语音包小凡已经上传的百度网盘了,需要的小伙伴自行下载哈

链接:https://pan.baidu.com/s/17sPGYC21fvzw4ebgXll74A?pwd=8888
提取码:8888

⑤在w_main窗口的cb_1按钮的Clicked事件 中添加如下代码

integer i,count
string ls_current_path
//获取项目当前路径
ls_current_path = GetCurrentDirectory()st_3.text = xx2dx(dec(sle_1.text))count = len(st_3.text)for i= 1 to count step 2CHOOSE CASE mid(st_3.text,i,2)CASE "零"playsound(ls_current_path+"\temp\0.wav",0)				CASE "壹"playsound(ls_current_path+"\temp\1.wav",0)				CASE "贰"playsound(ls_current_path+"\temp\2.wav",0)				CASE "叁"playsound(ls_current_path+"\temp\3.wav",0)				CASE "肆"playsound(ls_current_path+"\temp\4.wav",0)				CASE "伍"playsound(ls_current_path+"\temp\5.wav",0)				CASE "陆"playsound(ls_current_path+"\temp\6.wav",0)				CASE "柒"playsound(ls_current_path+"\temp\7.wav",0)				CASE "捌"playsound(ls_current_path+"\temp\8.wav",0)				CASE "玖"playsound(ls_current_path+"\temp\9.wav",0)				CASE "拾"playsound(ls_current_path+"\temp\十.wav",0)				CASE "佰"playsound(ls_current_path+"\temp\佰.wav",0)				CASE "仟"playsound(ls_current_path+"\temp\仟.wav",0)				CASE "万"playsound(ls_current_path+"\temp\万.wav",0)				CASE "亿"playsound(ls_current_path+"\temp\亿.wav",0)				CASE "元"playsound(ls_current_path+"\temp\元.wav",0)				CASE "角"playsound(ls_current_path+"\temp\角.wav",0)				CASE "分"playsound(ls_current_path+"\temp\分.wav",0)				CASE "整"playsound(ls_current_path+"\temp\整.wav",0)				END CHOOSE
next

⑥ 在cb_2退出按钮的Clicked事件中添加如下代码

close(parent)

⑦ 在开发界面左边的System Tree窗口中双击exampleapp应用对象,并在其Open事件中添加如下代码

open(w_main)

五、运行程序

代码写完了,来检验下我们的劳动成果。

金额语音播报

本期内容到这儿就结束了 ,★,°:.☆( ̄▽ ̄)/$:.°★ 。希望对您有所帮助

我们下期再见 ヾ(•ω•`)o (●’◡’●)

http://www.lryc.cn/news/375300.html

相关文章:

  • glmark2代码阅读总结
  • 第 6 章 监控系统 | 监控套路 - 总结
  • VsCode中C文件调用其他C文件函数失败
  • css中content属性你了解多少?
  • JVM-GC-G1垃圾回收器
  • 【Ubuntu通用压力测试】Ubuntu16.04 CPU压力测试
  • Artix Linux 默认不使用 systemd
  • JVM-GC-CMS垃圾回收器
  • 【玩转google云】实战:如何在GKE上使用Helm安装和配置3节点的RabbitMQ集群
  • 【神经网络】深度神经网络
  • 机器学习算法 —— K近邻(KNN分类)
  • Thinkphp5内核流浪猫流浪狗宠物领养平台H5源码
  • c++ 智能指针使用注意事项及解决方案
  • SQLite Delete 语句
  • vue3的基本使用方法
  • Java数据结构与算法(盛水的容器贪心算法)
  • MYSQL 数字(Aggregate)函数
  • 【TensorFlow深度学习】如何处理不平衡数据集与欠采样、过采样技术
  • 【考研数学】如何保证进度不掉队?暑假强化保姆级规划
  • Vue3【二十一】Vue 路由模式(createWebHashHistory /createWebHistory )和RouterLink写法
  • 【交易策略】#22-24 残差资金流强度因子
  • CentOS 7.9检测硬盘坏区、实物定位(三)
  • redis持久化方式—RDB
  • java8实战1(让方法参数具备行为能力)
  • C#(C Sharp)学习笔记_多态【十九】
  • 电子竞赛1——基于DDS的AM信号发生器
  • CentOS7的#!bash #!/bin/bash #!/bin/env bash #!/usr/bin/bash #!/usr/bin/env bash
  • 代码随想录第四十一天打卡
  • 矩阵补全IGMC 学习笔记
  • 面试题之CSS