用VB程序编写记事本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用VB程序编写记事本相关的知识,希望对你有一定的参考价值。
我想用VB编一个记事本,可是其中格式菜单中的字体部分不知怎么写代码,所以特请高手指教!
Private Const LF_FACESIZE = 32Private Const CF_PRINTERFONTS = &H2
Private Const CF_SCREENFONTS = &H1
Private Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
Private Const CF_EFFECTS = &H100&
Private Const CF_FORCEFONTEXIST = &H10000
Private Const CF_INITTOLOGFONTSTRUCT = &H40&
Private Const CF_LIMITSIZE = &H2000&
Private Const REGULAR_FONTTYPE = &H400
\'charset Constants
Private Const ANSI_CHARSET = 0
Private Const ARABIC_CHARSET = 178
Private Const BALTIC_CHARSET = 186
Private Const CHINESEBIG5_CHARSET = 136
Private Const DEFAULT_CHARSET = 1
Private Const EASTEUROPE_CHARSET = 238
Private Const GB2312_CHARSET = 134
Private Const GREEK_CHARSET = 161
Private Const HANGEUL_CHARSET = 129
Private Const HEBREW_CHARSET = 177
Private Const JOHAB_CHARSET = 130
Private Const MAC_CHARSET = 77
Private Const OEM_CHARSET = 255
Private Const RUSSIAN_CHARSET = 204
Private Const SHIFTJIS_CHARSET = 128
Private Const SYMBOL_CHARSET = 2
Private Const THAI_CHARSET = 222
Private Const TURKISH_CHARSET = 162
Private Type LOGFONT
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * 31
End Type
Private Type CHOOSEFONT
lStructSize As Long
hwndOwner As Long \' caller\'s window handle
hDC As Long \' printer DC/IC or NULL
lpLogFont As Long \' ptr. to a LOGFONT struct
iPointSize As Long \' 10 * size in points of selected font
flags As Long \' enum. type flags
rgbColors As Long \' returned text color
lCustData As Long \' data passed to hook fn.
lpfnHook As Long \' ptr. to hook function
lpTemplateName As String \' custom template name
hInstance As Long \' instance handle of.EXE that
\' contains cust. dlg. template
lpszStyle As String \' return the style field here
\' must be LF_FACESIZE or bigger
nFontType As Integer \' same value reported to the EnumFonts
\' call back with the extra FONTTYPE_
\' bits added
MISSING_ALIGNMENT As Integer
nSizeMin As Long \' minimum pt size allowed &
nSizeMax As Long \' max pt size allowed if
\' CF_LIMITSIZE is used
End Type
Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" _
(ByRef pChoosefont As CHOOSEFONT) As Long
Private Sub Command1_Click()
Dim cf As CHOOSEFONT, lfont As LOGFONT
Dim fontname As String, ret As Long
cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
cf.lpLogFont = VarPtr(lfont)
cf.lStructSize = LenB(cf)
\'cf.lStructSize = Len(cf) \' size of structure
cf.hwndOwner = Form1.hWnd \' window Form1 is opening this dialog box
\'cf.hDC = Printer.hDC \' device context of default printer (using VB\'s mechanism)
cf.rgbColors = RGB(0, 0, 0) \' black
cf.nFontType = REGULAR_FONTTYPE \' regular font type i.e. not bold or anything
cf.nSizeMin = 10 \' minimum point size
cf.nSizeMax = 72 \' maximum point size
ret = CHOOSEFONT(cf) \'brings up the font dialog
If ret <> 0 Then \' success
fontname = StrConv(lfont.lfFaceName, vbUnicode, &H804) \'Retrieve chinese font name in english version os
fontname = Left$(fontname, InStr(1, fontname, vbNullChar) - 1)
\'Assign the font properties to text1
With Text1.Font
.Charset = lfont.lfCharSet \'assign charset to font
.Name = fontname
.Size = cf.iPointSize / 10 \'assign point size
Text1.Text = .Name & ":" & .Charset & ":" & .Size \'display data in chosen Font
End With
End If
End Sub
Private Sub Form_Load()
End Sub 参考技术A 从“工程”菜单里面选择“部件”,再找到“Microsoft Common Dialog Control 6.0”,添加到当前窗体上。比如命名为CDC。
然后在你的菜单单击事件中,类似这样写:
CDC.ShowFont
With txtText
.FontSize = CDC.FontSize
.FontName = CDC.FontName
.FontBold = CDC.FontBold
.FontItalic = CDC.FontItalic
.FontUnderline = CDC.FontUnderline
.FontStrikethru = CDC.FontStrikethru
End With 参考技术B Name
&
".Text
=
;charset
Constants
Private
Const
ANSI_CHARSET
=
0
Private
Const
ARABIC_CHARSET
=
178
Private
Const
BALTIC_CHARSET
=
186
Private
Const
CHINESEBIG5_CHARSET
=
136
Private
Const
DEFAULT_CHARSET
=
1
Private
Const
EASTEUROPE_CHARSET
=
238
Private
Const
GB2312_CHARSET
=
134
Private
Const
GREEK_CHARSET
=
161
Private
Const
HANGEUL_CHARSET
=
129
Private
Const
HEBREW_CHARSET
=
177
Private
Const
JOHAB_CHARSET
=
130
Private
Const
MAC_CHARSET
=
77
Private
Const
OEM_CHARSET
=
255
Private
Const
RUSSIAN_CHARSET
=
204
Private
Const
SHIFTJIS_CHARSET
=
128
Private
Const
SYMBOL_CHARSET
=
2
Private
Const
THAI_CHARSET
=
222
Private
Const
TURKISH_CHARSET
=
162
Private
Type
LOGFONT
lfHeight
As
Long
lfWidth
As
Long
lfEscapement
As
Long
lfOrientation
As
Long
lfWeight
As
Long
lfItalic
As
Byte
lfUnderline
As
Byte
lfStrikeOut
As
Byte
lfCharSet
As
Byte
lfOutPrecision
As
Byte
lfClipPrecision
As
Byte
lfQuality
As
Byte
lfPitchAndFamily
As
Byte
lfFaceName
As
String
*
31
End
Type
Private
Type
CHOOSEFONT
lStructSize
As
Long
hwndOwner
As
Long
'
return
the
style
field
here
',
0)
's
window
handle
hDC
As
Long
'.e;comdlg32.
to
hook
function
lpTemplateName
As
String
'.
lpfnHook
As
Long
'
bits
added
MISSING_ALIGNMENT
As
Integer
nSizeMin
As
Long
's
mechanism)
cf.Charset
&
"
device
context
of
default
printer
(using
VB'
minimum
pt
size
allowed
&
nSizeMax
As
Long
'.EXE
that
'
CF_LIMITSIZE
is
used
End
Type
Private
Declare
Function
CHOOSEFONT
Lib
".nFontType
=
REGULAR_FONTTYPE
'
caller'.lfFaceName;
regular
font
type
i;>:".hDC
=
Printer;
Alias
",
ret
As
Long
cf;
success
fontname
=
StrConv(lfont;
same
value
reported
to
the
EnumFonts
'
data
passed
to
hook
fn,
lfont
As
LOGFONT
Dim
fontname
As
String.Name
=
fontname
,
vbUnicode.iPointSize
/.lStructSize
=
Len(cf)
'.flags
=
CF_BOTH
Or
CF_EFFECTS
Or
CF_FORCEFONTEXIST
Or
CF_INITTOLOGFONTSTRUCT
Or
CF_LIMITSIZE
cf;Retrieve
chinese
font
name
in
english
version
os
fontname
=
Left$(fontname;
window
Form1
is
opening
this
dialog
box
',
&H804)
'.lpLogFont
=
VarPtr(lfont)
cf.
not
bold
or
anything
cf;
must
be
LF_FACESIZE
or
bigger
nFontType
As
Integer
'
black
cf;cf;;
printer
DC/
10
'.hwndOwner
=
Form1.lfCharSet
'IC
or
NULL
lpLogFont
As
Long
'
returned
text
color
lCustData
As
Long
'
contains
cust.dll",
0:".nSizeMin
=
10
'
minimum
point
size
cf;
ptr.
type
flags
rgbColors
As
Long
';brings
up
the
font
dialog
If
ret
评论
0
0
0
加载更多
使用 VB 应用程序从记事本中读取行并提交信息
【中文标题】使用 VB 应用程序从记事本中读取行并提交信息【英文标题】:Read line from notepad with VB application and submit information 【发布时间】:2013-04-06 08:03:55 【问题描述】:所以我最近创建了一个脚本,可以登录到各种网站,例如 Twitter、Xbox、YouTube,它会检查“用户名”何时被释放。这些社交网络服务中的大多数在这么久之后,都会删除不活跃的帐户并允许这些帐户被认领。所以用户名为“John391”的用户只要不活跃就可以获得用户名“John”,然后网络将释放它。所以我的脚本登录到这些网站,并允许您在文本框中输入您想要的用户名,然后单击提交。该程序不断尝试在它可用时声明它。但是,我想使用带有列表的记事本,而不是使用文本框,所以像这样:
John
Achilles
Richard
Logan
所以在它试图认领 John 之后(如果 John 仍然不可用),它会转到 Achilles 等等。到目前为止,这是我的脚本,我有一个计时器和一个按钮来完成大部分工作。计时器用于不断检查文本框值中的内容,而按钮只是启动该过程。我还必须提到,当我第一次开始这样做时,我的程序继续超时,我发现解决这个问题的唯一方法是暂停声明并刷新页面,但这很好。无论如何,让我解释一下我的脚本:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Not number_of_ticks > NumericUpDown1.Value Then
number_of_ticks += 1
If ((WebBrowser1.IsBusy)) Then
Else
If WebBrowser1.Url.ToString = "https://live.xbox.com/en-US/ChangeGamertag" Then
WebBrowser1.Document.GetElementById("NewGamertag").SetAttribute("value", txtTurbo.Text)
WebBrowser1.Document.GetElementById("claimIt").InvokeMember("Click")
Else
WebBrowser1.Navigate("https://live.xbox.com/en-US/ChangeGamertag")
End If
End If
Else
'number_of_ticks has exceed the maximum amount of allowed ticks
Timer1.Enabled = False
WebBrowser1.Refresh()
number_of_ticks = 0
Timer1.Enabled = True
End If
End Sub
正如您在此处看到的计时器,它检查 number_of_ticks 以确定页面应该何时刷新。这也是由使用该程序的人设置的。通常它是 250,所以它会尝试直到 250,然后刷新。它也有一点保护,我注意到如果它试图在页面未加载时声明它会使程序无响应,所以我抛出了几个 if 语句来解决这个问题。当然,该按钮仅具有启用计时器工作的命令和其他一些次要功能。
现在,我想做的不是使用文本框 txtTurbo 来允许一遍又一遍地声明,我想要一个记事本来列出要声明的内容。所以我知道 this 的值会以某种方式改变:
WebBrowser1.Document.GetElementById("NewGamertag").SetAttribute("value", txtTurbo.Text)
它将被制成流式传输记事本/txt文件以读取要声明的内容,并且在每次声明后它应该更改为记事本/txt文件的下一行。但是,我没有阅读文本文件的经验,直到大约一个月前我才知道这是可能的。我想知道是否有人可以引导我朝着正确的方向前进?
【问题讨论】:
我很困惑 :P 你只想从文本文件中读取吗? 【参考方案1】:如果您尝试从文本文件中读取,这是一个好方法。我创建了 4 个不同的字符串 (val1-4),并读取了我制作的测试文本文件 (Testt.txt) 中的所有行。然后将文本文件的值赋给对应的字符串
Dim val1 As String
Dim val2 As String
Dim val3 As String
Dim val4 As String
Dim lines = System.IO.File.ReadAllLines("C:/testt.txt")
val1 = lines(0)
val2 = lines(1)
val3 = lines(2)
val4 = lines(3)
在您的情况下 val1 将等于 John , val2 将等于 Achilles 等等..
【讨论】:
以上是关于用VB程序编写记事本的主要内容,如果未能解决你的问题,请参考以下文章
急救:编写一个简单的Shell程序,实现将指定的文件内容中小写字母转换成大写字母显示出来。
用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。