D1V1网社区 @开门芝麻网 连劲淘 芝麻卡 吃饭赚钱 睡觉赚钱 做梦赚钱 http://sns.d1v1.com & http://www.KaiMenZhiMa.com/

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12055|回复: 0

用API创建VB窗体

[复制链接]
发表于 2012-6-4 00:00:05 | 显示全部楼层 |阅读模式 <
开门芝麻网
芝麻卡私域引流:芝麻卡赋能百业增值大众。http://sns.d1v1.com/forum.php?mod=viewthread&tid=6184
,小泽玛利亚
在vb中用api创建窗体和vc中的步骤是一样的,只不过用ide环境创建是把过程都封装起来,现在我们用api方式创建,大致让我们了解一个窗体的产生过程,让我们使用vb的程序员对系统的机制多一些了解.
先所以下用c++创建窗体的过程:
程序的入口:int apientry winmain(hinstance hinstance,
                     hinstance hprevinstance,
                     lpstr     lpcmdline,
                     int       ncmdshow)
进入后先初始化结构wndclassex wcex;
调用apiregisterclassex,注册窗体结构,如果成功一次调用createwindow、showwindow、updatewindow这样一个主窗体就成功的创建并显示给用户,下面就缺少一个处理消息的死循环。
那么我们我们可以按照这个步骤在vb中实现一样的效果:
在c++因为api声明已经被包涵到头文件所以直接用就可以,但是在vb中就要逐个声明一下用到的api,结构。
public declare function registerclass lib "user32" alias "registerclassa" (class as wndclass) as long
public declare function unregisterclass lib "user32" alias "unregisterclassa" (byval lpclassname as string, byval hinstance as long) as long
public declare function defwindowproc lib "user32" alias "defwindowproca" (byval hwnd as long, byval wmsg as long,两性, byval wparam as long, byval lparam as long) as long
public declare function getmessage lib "user32" alias "getmessagea" (lpmsg as msg,led lights, byval hwnd as long, byval wmsgfiltermin as long, byval wmsgfiltermax as long) as long
public declare function translatemessage lib "user32" (lpmsg as msg) as long
public declare function dispatchmessage lib "user32" alias "dispatchmessagea" (lpmsg as msg) as long
public declare function showwindow lib "user32" (byval hwnd as long, byval ncmdshow as long) as long
public declare function loadcursor lib "user32" alias "loadcursora" (byval hinstance as long, byval lpcursorname as any) as long
public declare function loadicon lib "user32" alias "loadicona" (byval hinstance as long, byval lpiconname as string) as long
public declare function createwindowex lib "user32" alias "createwindowexa" (byval dwexstyle as long, byval lpclassname as string, byval lpwindowname as string,色情游戏, byval dwstyle as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hwndparent as long, byval hmenu as long, byval hinstance as long, lpparam as any) as long
public declare function callwindowproc lib "user32" alias "callwindowproca" (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long
public declare function setwindowlong lib "user32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
public declare function getwindowlong lib "user32" alias "getwindowlonga" (byval hwnd as long, byval nindex as long) as long
public declare function messagebox lib "user32" alias "messageboxa" (byval hwnd as long, byval lptext as string, byval lpcaption as string, byval wtype as long) as long
public declare sub postquitmessage lib "user32" (byval nexitcode as long)
public type wndclass
    style as long
    lpfnwndproc as long
    cbclsextra as long
    cbwndextra2 as long
    hinstance as long
    hicon as long
    hcursor as long
    hbrbackground as long
    lpszmenuname as string
    lpszclassname as string
end type
public type pointapi
    x as long
    y as long
end type
public type msg
    hwnd as long
    message as long
    wparam as long
    lparam as long
    time as long
    pt as pointapi
end type
public const cs_vredraw = &h1
public const cs_hredraw = &h2
public const cw_usedefault = &h80000000
public const es_multiline = &h4&
public const ws_border = &h800000
public const ws_child = &h40000000
public const ws_overlapped = &h0&
public const ws_caption = &hc00000 ' ws_border or ws_dlgframe
public const ws_sysmenu = &h80000
public const ws_thickframe = &h40000
public const ws_minimizebox = &h20000
public const ws_maximizebox = &h10000
public const ws_overlappedwindow = (ws_overlapped or ws_caption or ws_sysmenu or ws_thickframe or ws_minimizebox or ws_maximizebox)
public const ws_ex_clientedge = &h200&
public const color_window = 5
public const wm_destroy = &h2
public const wm_lbuttondown = &h201
public const wm_lbuttonup = &h202
public const idc_arrow = 32512&
public const idi_application = 32512&
public const gwl_wndproc = (-4)
public const sw_shownormal = 1
public const mb_ok = &h0&
public const mb_iconexclamation = &h30&
声明几个我们需要的变量、常量:
public const gclassname = "myclassname"
public const gappname = "my window caption"
public gbutoldproc as long
public ghwnd as long, gbuttonhwnd as long, gedithwnd as long

入口函数:
sub main
代码如下:
public sub main()
   dim wmsg as msg
   ''call procedure to register window classname. if false, then exit.
   if registerwindow then exit sub
   
      ''create window
      if createwindows then
         ''loop will exit when wm_quit is sent to the window.
         do while getmessage(wmsg, 0&, 0&, 0&)
            ''translatemessage takes keyboard messages and converts
            ''them to wm_char for easier processing.
            call translatemessage(wmsg)
            ''dispatchmessage calls the default window procedure
            ''to process the window message. (wndproc)
            call dispatchmessage(wmsg)
         loop
      end if
    call unregisterclass(gclassname$, app.hinstance)
end sub
public function registerwindowclass() as boolean
    dim wc as wndclass
   
   
    wc.style = cs_hredraw or cs_vredraw
    wc.lpfnwndproc = getaddress(addressof wndproc) ''address in memory of default window procedure.
    wc.hinstance = app.hinstance
    wc.hicon = loadicon(0&, idi_application) ''default application icon
    wc.hcursor = loadcursor(0&, idc_arrow) ''default arrow
    wc.hbrbackground = color_window ''default a color for window.
    wc.lpszclassname = gclassname$
    registerwindow <> 0
   
end function
public function createwindows() as boolean
  
    ''开始创建窗体
主窗体.
    ghwnd& = createwindowex(0&, gclassname$, gappname$,成人游戏, ws_overlappedwindow, cw_usedefault, cw_usedefault, 208, 150, 0&, 0&, app.hinstance, byval 0&)
    ''创建一个按钮
    gbuttonhwnd& = createwindowex(0&, "button", "click here", ws_child, 58, 90, 85, 25,情趣, ghwnd&, 0&, app.hinstance, 0&)
    ''创建一个(ws_ex_clientedge、es_multiline风格的textbox
    gedithwnd& = createwindowex(ws_ex_clientedge, "edit",美女人体, "this is the edit control." & vbcrlf & "as you can see, it's multiline.", ws_child or es_multiline, 0&,wedding dresses, 0&,制服丝袜, 200, 80,私处, ghwnd&, 0&,<a href="http://xinglewu.com" style="colo
开门芝麻网
部分内容由网友发布或收集于互联网,如有侵权,请联系QQ/微信76815288,第一时间删除!(开门芝麻网 sns.d1v1.com)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
400-888-xxxx

微信扫一扫,私享最新原创实用干货

QQ|申请友链|Archiver|手机版|小黑屋|D1V1网社区 @开门芝麻网 ( 沪ICP备15050032号-2 )

GMT+8, 2025-7-7 03:15 , Processed in 0.118030 second(s), 30 queries .

Powered by Discuz! X3.4 Designed by www.D1V1.cn

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表