Delphi下用Windows API建立表單

來源:互聯網
上載者:User

// Delphi 下調用Windows API 建立表單.//

program delphi;

uses
  windows,
  messages;
const
  hellostr='Hello World!';
{$R delphi.res}
//視窗訊息處理函數.
function MyWinProc(hWnd:THandle;uMsg:UINT;wParam,lParam:Cardinal):Cardinal;exp
ort;stdcall;
var
  hdca,hdcb:THandle; //裝置描述表控制代碼.
  rect:TRect; //矩形結構.
  font:HFont;
  ps:TPaintStruct; //繪圖結構.
begin
  result:=0;
  case uMsg of
   WM_PAINT:
    begin
     hdca:=BeginPaint(hWnd,ps);
     SetBkMode(hdca, Transparent);
     SetBkColor(hdca,GetBkColor(hdca));
     GetClientRect(hWnd,rect); //擷取視窗客戶區的尺寸.
     DrawText(hdca,Pchar(hellostr),-1,rect,DT_SINGLELINE or DT_CENTER or DT
_VCENTER);
//   TextOut(hdc,100,40,hellostr,Length(hellostr));
     EndPaint(hWnd,ps);
    end;
   WM_CREATE:
    begin
     hdcb := GetDC(hWnd);
     font := CreateFont(45, 0, 0, 0, FW_normal, 0, 0, 0, ansi_charset, out
_default_precis, clip_default_precis,
     default_quality, 34, PChar('Arial'));
     SelectObject(hdcb, font);
     ReleaseDC(hWnd, hdcb);
    end;
   WM_DESTROY:
    PostQuitMessage(0)
   else
//使用預設的視窗訊息處理函數.
    result:=DefWindowProc(hWnd,uMsg,wParam,lParam);
   end;
end;
//主程式開始.
var
  Msg    :TMsg; //訊息結構.
  hWnd,hInst :THandle; //Windows 視窗控制代碼.
  WinClass :TWndClassEx; //Windows 視窗類別結構.
begin
  hInst:=GetModuleHandle(nil); // get the application instance
  WinClass.cbSize:=SizeOf(TWndClassEx);
  WinClass.lpszClassName:='MyWindow'; //類名.
  WinClass.style:=CS_HREDRAW or CS_VREDRAW or CS_OWNDC;
  WinClass.hInstance:=hInst; //程式的執行個體控制代碼.
//設定視窗訊息處理函數.
  WinClass.lpfnWndProc:=@MyWinProc; //視窗過程.
  WinClass.cbClsExtra:=0; //以下兩個域用於在類結構和Window
s內部儲存的視窗結構
  WinClass.cbWndExtra:=0; //中預留一些額外空間.
  WinClass.hIcon:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
  WinClass.hIconsm:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
  WinClass.hCursor:=LoadCursor(0,IDC_Arrow);
//GetStockObject 擷取一個繪圖物件,在這裡是擷取繪製視窗背景的刷子,返回一個白色刷
子的控制代碼.
  WinClass.hbrBackground:=HBRUSH(GetStockObject(white_Brush));
  WinClass.lpszMenuName:=nil; //指定視窗類別菜單.
//向Windows 註冊視窗類別.
  if RegisterClassEx(WinClass)=0 then
  begin
   MessageBox(0,'Registeration Error!','SDK/API',MB_OK);
   Exit;
  end;
//建立視窗對象.
  hWnd:=CreateWindowEx(
         WS_EX_OVERLAPPEDWINDOW,        //擴充的視窗風格.
         WinClass.lpszClassName,        //類名.
         'Hello Window',            //視窗標題.
         WS_OVERLAPPEDWINDOW,          //視窗風格.
         CW_USEDEFAULT,             //視窗左上方相對於螢幕
左上方的初始位置x.
         0,                   //....右y.
         CW_USEDEFAULT,             //視窗寬度x.
         0,                   //視窗高度y.
         0,                   //父視窗控制代碼.
         0,                   //視窗菜單控制代碼.
         hInst,                 //程式執行個體控制代碼.
         nil); //建立參數指標.
  if hWnd<>0 then
   begin
    ShowWindow(hWnd,SW_SHOWNORMAL); //顯示視窗.
    UpdateWindow(hWnd); //指示視窗重新整理自己.
    SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
   end
  else
   MessageBox(0,'Window not Created!','SDK/API',MB_OK);
//主訊息迴圈程式. 
  while GetMessage(Msg,0,0,0) do
  begin
   TranslateMessage(Msg); //轉換某些鍵盤訊息.
   DispatchMessage(Msg); //將訊息發送給視窗過程.
  end;
end.

其實Windows 編程是每個學寫程式的人都要掌握的,學Delphi時也最好能先學習Windos編程(最少要知道).以上代碼雖說不如在Delphi中直接來個New->Form來的快,但它能告訴你本質的東西.能讓你更好的瞭解訊息迴圈以及其他.而這些正是讓New出來的表單給掩蓋的部分 .

>注:以上代碼是我從Windows 程式設計上通過C++文法直譯過來的(),測試後沒有問題.若我的註解有什麼錯誤的地方,請各位指正!^_^

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.