HanDs
NO.2

[Delphi文章] Delphi全API创建窗体、工具栏、按钮 





学习中请遵循国家相关法律法规,黑客不作恶。没有网络安全就没有国家安全

本站需要登陆后才能查看

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.


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
Delphi API 创建窗体 工具栏 按钮
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组