首页
社区
课程
招聘
[推荐]Delphi全API创建窗体、工具栏、按钮
发表于: 2011-4-11 09:04 8266

[推荐]Delphi全API创建窗体、工具栏、按钮

2011-4-11 09:04
8266
program Project1;


uses

 Windows, Messages,CommCtrl;
 

{回调函数; 其中要处理的消息很多, 最好用 case 语句}
const IDC_BTN1=101;
      IDC_BTN2=102;
      IDC_EDT1=103;
var HFON:Integer;
   WND_EDT:HWND;
   WND_BUTTON:HWND;
   htoolbar:HWND; //得是全局变量,否则显示不全
var str_50:string='新建文件';
function WndProc(wnd: HWND; msg: UINT; wParam: Integer; lParam: Integer): Integer; stdcall;
var g_hEdit_PassWord:array[0..MAX_PATH] OF Char;
    var tbb:tTBButton;

 Hstatus:HWND;
 Tba   :TBADDBITMAP;
 var

 Handle: HDC;

 ps: PAINTSTRUCT;

 rc:TRect;
 tbhi:LongInt;

begin

 Result := 0;

case msg of
   WM_PAINT: begin

     handle := BeginPaint(wnd, ps);
//       OnPaint(Handle);
     GetClientRect(wnd,rc);
     GetWindowRect(htoolbar,rc);
      asm
       mov eax, Rc.bottom
       sub eax, Rc.top
       mov tbhi, eax
       mov Rc.left, 0
       mov Rc.top, 36
       mov Rc.right, 494
       mov Rc.bottom, 268
      end;
DrawEdge(Handle,rc,EDGE_SUNKEN,BF_RECT);
       EndPaint(wnd, ps);
       result := 0;
    end;


   WM_SIZE:   // ;否则图片显示不全
   SendMessage(hToolBar,TB_AUTOSIZE,0,0);
   WM_CREATE:begin

     asm
        mov tbb.iBitmap,   0
        mov tbb.idCommand, 0
        mov tbb.fsState,   TBSTATE_ENABLED
        mov tbb.fsStyle,   TBSTYLE_SEP
        mov tbb.dwData,    0
        mov tbb.iString,   0
     end;
  Hstatus := CreateStatusWindow(WS_CHILD or WS_VISIBLE or
                                   SBS_SIZEGRIP,nil, Wnd, 200);
 hToolBar := CreateToolBarEx(wnd,WS_CHILD or WS_CLIPSIBLINGS,
                               300,1,0,0,@tbb,
                               1,16,16,0,0,sizeof( TBB)

                        );

         ShowWindow(hToolBar,SW_SHOW);
         UpdateWindow(htoolbar);
        asm
         mov Tba.hInst, HINST_COMMCTRL
        mov Tba.nID, 1   //; btnsize 1=big 2=small
         end;
         SendMessage(hToolBar,TB_ADDBITMAP,1,Integer(@Tba));
       asm
        mov tbb.iBitmap,   STD_FILENEW
        mov tbb.fsStyle,   TBSTYLE_BUTTON
        mov tbb.idCommand, 50

       end;
         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));

       asm
        mov tbb.iBitmap,   STD_FILEOPEN
        mov tbb.idCommand, 51
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;

         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));

       asm
        mov tbb.iBitmap,   STD_FILESAVE
        mov tbb.idCommand, 52
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       //
       asm
       mov tbb.iBitmap,   0
        mov tbb.idCommand, 0
        mov tbb.fsStyle,   TBSTYLE_SEP
        end;

        SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_REPLACE
        mov tbb.idCommand, 53
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;

         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_PRINT
        mov tbb.idCommand, 54
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_PASTE
        mov tbb.idCommand, 55
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
       mov tbb.iBitmap,   STD_UNDO
        mov tbb.idCommand, 56
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
       mov tbb.iBitmap,   STD_CUT
        mov tbb.idCommand, 57
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));


         CreateWindowEx(0,              {不使用扩展风格}

                        'button',       {窗口类型名}

                        'PUSH ME',               {标题}

                        WS_VISIBLE or WS_CHILD,   {窗口的常规样式}
                        340,120,120,24,

                        wnd,                     {父窗口}

                        IDC_BTN1,                     {主菜单}

                        HInstance,             {实例句柄}

                        nil                     {无附加信息}

                        );
                    asm
                    MOV WND_BUTTON,EAX
                    end;

         CreateWindowEx(0, 'EDIT', 'INPUT',               {标题}

                        WS_VISIBLE or WS_CHILD or WS_BORDER,   {带边框子窗口}
                        40,120,240,24,

                        wnd,

                        IDC_EDT1,

                        HInstance,

                        nil

                        );
                        asm
                        MOV WND_EDT,EAX
                        end;
        hFon :=CreateFont(-12, 0, 0, 0, FW_NORMAl, 0, 0, 0, DEFAULT_CHARSET,
    0,0,0,0, 'Verdana');

         SendMessage(WND_EDT, WM_SETFONT, HFON, 0);
         SendMessage(WND_BUTTON,WM_SETFONT,HFON,0);
          end;

   WM_COMMAND:
   begin
    case wParam OF 50:begin
            //szText tb50,"New File"
//            SendMessage(hStatus,SB_SETTEXT,0,tb50);
            MessageBox(0,'新建文件','',MB_OK);
            SENDMESSAGE(Hstatus,WM_SETTEXT,0,Integer(str_50));
            end;
        51:
          MessageBox(0,'打开文件','这是你的选择!',MB_OK);
        52:
          MessageBox(0,'保存','这是你的选择!',MB_OK);
        53:
          MessageBox(0,'Replace','这是你的选择!',MB_OK);
        54:
          MessageBox(0,'Print','这是你的选择!',MB_OK);
        55:
          MessageBox(0,'Paste','这是你的选择!',MB_OK);
        56:
          MessageBox(0,'Undo','这是你的选择!',MB_OK);
        57:
          MessageBox(0,'Cut','这是你的选择!',MB_OK);
     IDC_BTN1:
         BEGIN
          GetWindowText(WND_EDT,g_hEdit_PassWord,MAX_PATH);
          MessageBox(0,g_hEdit_PassWord,'',0);
         end;
    end;
   end;
   WM_DESTROY: PostQuitMessage(0); {收到 WM_DESTROY 后, 发送 WM_QUIT 消息指示退出}

else

  {其他消息交 DefWindowProc 处理; DefWindowProc 会返回回调函数需要的返回值}

   Result := DefWindowProc(wnd, msg, wParam, lParam);

end;

end;



{登记与注册窗口类的函数}

function RegMyWndClass: Boolean;

var

 MyWndClass: TWndClass;

begin

{指定窗口类型}

 MyWndClass.style         := CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW; {改变大小时重绘}

 MyWndClass.lpfnWndProc   := @WndProc;                 {回调函数指针}

 MyWndClass.cbClsExtra    := 0;                         {没有额外的窗口类信息}

 MyWndClass.cbWndExtra    := 0;                         {没有额外的窗口信息}

 MyWndClass.hInstance     := HInstance;                 {程序实例句柄}

 MyWndClass.hIcon         := 0;                         {没指定图标}

 MyWndClass.hCursor       := LoadCursor(0, IDC_ARROW); {选用了系统提供的指针}

 MyWndClass.hbrBackground := HBRUSH(COLOR_BTNFACE + 1); {背景色使用 Windows 默认的按钮颜色}

 MyWndClass.lpszMenuName  := nil;                       {不指定默认菜单}

 MyWndClass.lpszClassName := 'MyWindowClass';           {给窗口类型命名}


 Result := RegisterClass(MyWndClass) <> 0;             {注册窗口类型}

end;



{主程序}

var

 hWnd: THandle;

 Msg : TMsg;

begin

{调用登记与注册窗口的函数}

if not RegMyWndClass then

begin

   MessageBox(0, '窗口类注册失败!', '提示', MB_OK + MB_ICONERROR);

   Exit;

end;



{建立窗口并返回句柄; 既然有 CreateWindowEx 就不使用 CreateWindow 了}

 hWnd := CreateWindowEx(WS_EX_LEFT,                     {不使用扩展风格}

                        'MyWindowClass',       {窗口类型名}

                        '新窗口',               {标题}

                        WS_SYSMENU or WS_MINIMIZEBOX,   {窗口的常规样式}

                        Integer(CW_USEDEFAULT), {默认水平位置}
                        Integer(CW_USEDEFAULT), {默认垂直位置}
                        500,
                        320,
                        0,                     {无父窗口}

                        0,                     {无主菜单}

                        HInstance,             {实例句柄}

                        nil                     {无附加信息}

                        );



if hWnd = 0 then {如果窗口建立失败}

begin

   MessageBox(0, '窗口建立失败!', '提示', MB_OK + MB_ICONERROR);

   Exit;

end;
  {
   asm
        mov tbb.iBitmap,   0
        mov tbb.idCommand, 0
        mov tbb.fsState,   TBSTATE_ENABLED
        mov tbb.fsStyle,   TBSTYLE_SEP
        mov tbb.dwData,    0
        mov tbb.iString,   0
   end;

 hToolBar := CreateToolBarEx(hwnd,WS_CHILD or WS_CLIPSIBLINGS,

                        100,9,10,10,
                       @tbb,
                       1,32,32,10,60,200

                        );

         ShowWindow(hToolBar,SW_SHOW);
        asm
         mov Tba.hInst, HINST_COMMCTRL
        mov Tba.nID, 1   //; btnsize 1=big 2=small
         end;
         SendMessage(hToolBar,TB_ADDBITMAP,1,Integer(@Tba));
       asm
        mov tbb.iBitmap,   STD_FILENEW
        mov tbb.idCommand, 50
        mov tbb.fsStyle,   TBSTYLE_BUTTON

       end;
         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tba));

       asm
        mov tbb.iBitmap,   STD_FILEOPEN
        mov tbb.idCommand, 51
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;

         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));

       asm
        mov tbb.iBitmap,   STD_FILESAVE
        mov tbb.idCommand, 52
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       //
       asm
       mov tbb.iBitmap,   0
        mov tbb.idCommand, 0
        mov tbb.fsStyle,   TBSTYLE_SEP
        end;

        SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_REPLACE
        mov tbb.idCommand, 53
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;

         SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_PRINT
        mov tbb.idCommand, 54
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
        mov tbb.iBitmap,   STD_PASTE
        mov tbb.idCommand, 55
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
       mov tbb.iBitmap,   STD_UNDO
        mov tbb.idCommand, 56
        mov tbb.fsStyle,   TBSTYLE_BUTTON
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
       asm
       mov tbb.iBitmap,   STD_CUT
        mov tbb.idCommand, 57
        mov tbb.fsStyle,   TBSTYLE_SEP
       end;
       SendMessage(hToolBar,TB_ADDBUTTONS,1,Integer(@tbb));
                }
 ShowWindow(hWnd, SW_SHOWNORMAL);

 UpdateWindow(hWnd);

 

{消息循环; GetMessage 在收到 WM_QUIT 消息时会返回 False, 从而终止循环}

while(GetMessage(Msg, 0, 0, 0)) do

begin

   TranslateMessage(Msg); {需要对部分键盘消息的再处理}

   DispatchMessage(Msg); {将消息发送给回调函数}

end; 

end.

 

窗体创建部分参照万一老师代码

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

上传的附件:
收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 563
活跃值: (101)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
咋和C++的差不多 
2011-4-11 20:52
0
雪    币: 34
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我菜,没看出啥不同的。。。
2011-4-13 14:37
0
雪    币: 34
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
刚才说的有些自大了,抱歉!
如果舍去VCL,全部手工创建,已经没有delphi的精华所在了,这样实现和用汇编实现没有区别啊,呵呵,我个人的理解,可能有些偏差!
2011-4-13 14:41
0
雪    币: 34
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
刚才说的有些自大了,抱歉!
如果舍去VCL,全部手工创建,已经没有delphi的精华所在了,这样实现和用汇编实现没有区别啊,呵呵,我个人的理解,可能有些偏差!
2011-4-13 14:42
0
雪    币: 190
活跃值: (42)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
6
岂不是麻烦么,有一个工具叫DFM2RC,蛮好用的,效果就是你这样子的。
2011-7-21 12:42
0
游客
登录 | 注册 方可回帖
返回
//