新疆软件开发

本站首页 软件开发 成功案例 公司新闻 公司简介 客服中心 软件技术 网站建设
  您现在的位置: 新疆二域软件开发公司 >> 开发语言 >> 文章正文

用vb制作文字窗体

我们知道,通常WINDOWS的窗体大都是矩形的。当然也可以使用API函数制作椭圆型的、三角型的窗体,还可以制作图片窗体。下面我给大家介绍一种制作文字、字符窗体的方法,希望能对大家有所帮助。
要想制作文字窗体就要用到WINDOWS的几个API函数,首先我们来看看这几个函数的功能:
1、 GetPixel (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) 函数
函数功能:取得一个像素的RGB值
参数:hdc ,设备场景的句柄
      x,y ,逻辑坐标中的点
2: CreateRectRgn (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
函数功能:创建一个由点X1,Y1和X2,Y2描述的矩形区域
参数:x1,y1,矩形左上角X,Y坐标
      x2,y2,矩形右下角X,Y坐标
3:Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
函数功能:将两个区域组合为一个新区域
参数:hDestRgn,结果区域句柄
      hSrcRgn1,源区域1
      hSrcRgn2,源区域2
      nCombineMode,合并模式
4:Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
函数功能:设置窗口的区域
参数:hWnd ,窗口句柄
      hRgn,将设置的区域的句柄
      bRedraw ,是否立即重画窗口

    在文字窗体的设计中应当先是选定一种屏蔽色作为窗体的背景色(当然这种颜色应当是窗体
图形中所没有的颜色),然后利用了一个字符数组,将设计好的图形存储在里面,之后将图形输
出在窗体上。最后用GetPixel函数扫描窗体上已经输出字符的区域,将窗体上与屏蔽色不同的区
域标记出来,并且用CreateRectRgn函数创建成矩形区域,再将它们用CombineRgn函数合并成一
个区域,之后用SetWindowRgn函数设置窗体区域并生成窗体。

属性设置:新建一个窗体(CAPTION->"制作文字窗体";NAME=FORM1;AUTOREDRAW->TRUE;
SCALEMODE->1;BACKCOLOR->屏蔽色)窗体字体和前景色可根据需要设置,也可以在代码中设置。


以下是程序源代码:
------------- API函数声明---------------------
Option Explicit
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc   Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
-------------常量-------------
Const RGN_OR = 2
------------- 变量-------------
Dim oldx As Integer
Dim oldy As Integer
Dim rgn As Long
Dim MaskColor As Long

Private Sub Form_Load()
MaskColor=Form1.BackColor
CreateForm
End Sub

'-------生成文字窗体-------
Public Sub CreateForm()
Const max = 5   '字符行数即窗体图形数组的最大值
Dim graphics(max) As String '存储窗体图形
Dim X As Long
Dim Y As Long
Dim count As Long  '相同像素数
Dim curpixels As Long   '当前检查像素
Dim temp As Long
Dim textheight As Long '扫描区域的高度
Dim textwidth As Long '扫描区域的宽度
Dim i As Integer
Dim maxwidth As Long  '窗体的最大宽度

Me.CurrentX = 0
Me.CurrentY = 0
'-------初始化窗体图形,可根据自己的需要设计出多种样式-------------
graphics(0) = "     ★"
graphics(1) = "    ★★"
graphics(2) = "   ◎▲◎"
graphics(3) = "  ★★★★"
graphics(4) = " ★■■■★"
graphics(5) = "哈哈文字窗体 "
'--------输出窗体图形------------
maxwidth = Me.textwidth(graphics(0))
For i = 0 To max
     If Me.textwidth(graphics(i)) >= maxwidth Then
        maxwidth = Me.textwidth(graphics(i))
     End If
     textheight = textheight + Me.textheight(graphics(i))
     '-----设置图形颜色-----------
     If i Mod 2 = 0 Then
        Me.ForeColor = vbBlue
     Else
        Me.ForeColor = vbRed
     End If
    
     Me.Print graphics(i)
     '-----根据文字大小自动缩放窗体------
     If Me.Height < textheight Then
        Me.Height = textheight + 500
     End If
     If Me.Width < maxwidth Then
        Me.Width = maxwidth + 500
     End If
Next i

textheight = Int(textheight / 15)
textwidth = Int(maxwidth / 15)

RGN = CreateRectRgn(0, 0, 0, 0)   '创建空区域
For Y = 0 To textheight - 1
    count = 0
    For X = 0 To textwidth - 1
        curpixels = GetPixel(Form1.hdc, X, Y)
       
        If X >= textwidth - 1 And count > 0 Then
           temp = CreateRectRgn(X + Int((Me.Width - Me.ScaleWidth) \ 30) - count, Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30), X + Int((Me.Width - Me.ScaleWidth) \ 30), Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30) + 1) '创建区域
           CombineRgn RGN, RGN, temp, RGN_OR '合并两个区域
           DeleteObject temp
        End If
       
        If curpixels <> MaskColor Then
           count = count + 1
        Else
           If count > 0 Then
              temp = CreateRectRgn(X + Int((Me.Width - Me.ScaleWidth) \ 30) - count, Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30), X + Int((Me.Width - Me.ScaleWidth) \ 30), Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30) + 1)
              CombineRgn RGN, RGN, temp, RGN_OR
              DeleteObject temp
           End If
           count = 0
        End If
    Next X
Next Y
SetWindowRgn Me.hwnd, RGN, True '设置窗体区域
End Sub


Private Sub Form_DblClick()
End
End Sub

'-----移动窗体代码------

Private Sub form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
oldx = X
oldy = Y
End If
End Sub

Private Sub form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
    If X <> oldx Or Y <> oldy Then
    Form1.Left = Form1.Left + (X - oldx)
    Form1.Top = Form1.Top + (Y - oldy)
    End If
End If
End Sub

作者:新疆二域… | 文章来源:本站原创 | 更新时间:2007-10-23 11:12:20

  • 上一篇文章:

  • 下一篇文章:

  • 相关文章:
    VB如何调用EXCEL 对象更改单元格中的内容
    VB6.0的3种类型的数据库访问方式介绍
    在VB中移动当前记录的方法
    VB编程中的If函数
    软件技术
    · 开发语言
    · Java技术
    · .Net技术
    · 数据库开发
    最新文章  
    ·搜集整理的asp.net的验证方
    ·各种FOR循环结构的整理
    ·软件项目开发中应该考虑那
    ·搜集整理的javascript sel
    ·软件开发中项目经理有那些
    ·学习如何在Lambda表达式进
    ·C++基础知识:结构体数据的
    ·C#实现短信发送程序的例子
    ·sun最近修补了一部分java的
    ·rss定制的另外一种实现方式
    ·delphi实现利用arp欺骗来实
    ·基础学习:基于WF的流程框
    ·网络编程中怎样得知一次数
    ·如何逆序输出单链表?
    ·软件开发过程中的性能设计
    关于我们 | 软件开发 | 下载试用 | 客服中心 | 联系我们 | 友情链接 | 网站地图 | 新疆电子地图 | RSS订阅
    版权所有 © 2016 新疆二域软件开发网 www.k8w.net All Rights Reserved 新ICP备14003571号
    新疆软件开发总机:0991-4842803、4811639.
    客服QQ:596589785 ;地址:新疆乌鲁木齐北京中路华联大厦A-5C 邮编:830000