时间: 2021-07-31 作者:daque
#include <windows.h>hwnd hwndbutton;int cx, cy;
lresult callback mainwndproc (hwnd hwindow, uint nmsg, wparam wprm, lparam lprm){
hdc dc; paintstruct ps; rect rc; switch (nmsg) { case wm_create: { textmetric tm;
dc = getdc (hwindow); selectobject (dc, getstockobject (system_fixed_font)); gettextmetrics (dc, &tm); cx = tm.tmavecharwidth * 30; cy = (tm.tmheight + tm.tmexternalleading) * 2; releasedc (hwindow, dc);
hwndbutton = createwindow ( "button", "click here", ws_child | ws_visible | bs_pushbutton, 0, 0, cx, cy, hwindow, (hmenu) 1, ((lpcreatestruct) lprm)->hinstance, null );
return 0; break; }
case wm_destroy: { postquitmessage (0); return 0; break; }
case wm_paint: { dc = beginpaint (hwindow, &ps); getclientrect (hwindow, &rc);
rc.bottom = rc.bottom / 2; drawtext (dc, "hello, world!", -1, &rc, dt_singleline | dt_center | dt_vcenter);
endpaint (hwindow, &ps); return 0; break; }
case wm_size: { if (hwndbutton && (wprm == sizefullscreen ||wprm == sizenormal)) { rc.left = (loword(lprm) - cx) / 2; rc.top = hiword(lprm) * 3 / 4 - cy / 2; movewindow (hwndbutton,rc.left, rc.top, cx, cy, true); } return 0; break; }
case wm_command: { if (loword(wprm) == 1 && hiword(wprm) == bn_clicked && (hwnd) lprm == hwndbutton) { destroywindow (hwindow); } return 0; break; } }
return defwindowproc (hwindow, nmsg, wprm, lprm);}
//winmain int __stdcall winmain (hinstance hinst, hinstance hprev, lpstr lpcmd, int nshow){ hwnd hwindowmain; msg mymsg;
wndclassex wcex;
wcex.cbsize = sizeof(wndclassex); wcex.style = cs_hredraw | cs_vredraw; wcex.lpfnwndproc = (wndproc)mainwndproc; wcex.cbclsextra = 0; wcex.cbwndextra = 0; wcex.hinstance = hinst; wcex.hicon = loadicon (null, idi_application); wcex.hcursor = loadcursor (null, idc_arrow); wcex.hbrbackground = (hbrush)(color_window+1); wcex.lpszclassname = "wintestwin"; wcex.hiconsm = loadicon (null, idi_application);
registerclassex (&wcex);
hwindowmain = createwindow ( "wintestwin", "hello", ws_overlappedwindow, cw_usedefault, cw_usedefault, cw_usedefault, cw_usedefault, 0, 0, hinst, null );
showwindow (hwindowmain, nshow); updatewindow (hwindowmain);
while (getmessage (&mymsg, 0, 0, 0)) { translatemessage (&mymsg); dispatchmessage (&mymsg); } return mymsg.wparam;}
个中采用release办法编写翻译,翻开vc6的最小代码优化,编写翻译天生的实行码为36.0kb, 而后将其翻译成delphi代码,如次:program wintest;
uses windows,messages;
var hwndbutton:hwnd; cx,cy:integer;
function mainwndproc (hwindow:hwnd;nmsg:uint;wprm:wparam;lprm:lparam):lresult;stdcall;var dc:hdc; ps:paintstruct; rc:trect; tm:textmetric; pctst:pcreatestruct;begin case nmsg of wm_create: begin dc := getdc (hwindow); selectobject (dc, getstockobject (system_fixed_font)); gettextmetrics (dc, tm); cx := tm.tmavecharwidth * 30; cy := (tm.tmheight + tm.tmexternalleading) * 2; releasedc (hwindow, dc); pctst:= pcreatestruct(lprm); hwndbutton := createwindow( 'button', 'click here', ws_child or ws_visible or bs_pushbutton, 0, 0, cx, cy, hwindow, hmenu(1), pctst^.hinstance, nil ); result:=0; exit; end;
wm_destroy: begin postquitmessage(0); result:=0; exit; end;
wm_paint: begin dc := beginpaint (hwindow, ps); getclientrect (hwindow, rc);
rc.bottom := round(rc.bottom / 2); drawtext (dc, 'hello, world!', -1, rc, dt_singleline or dt_center or dt_vcenter);
endpaint (hwindow, ps); result:= 0; exit; end;
wm_size: begin if (hwndbutton<>0) and (wprm = sizefullscreen) or (wprm = sizenormal) then begin rc.left := round((loword(lprm) - cx) / 2); rc.top := round(hiword(lprm) * 3 / 4 - cy / 2); movewindow (hwndbutton,rc.left, rc.top, cx, cy, true); end; result:= 0; exit; end;
wm_command: begin if (loword(wprm) = 1) and (hiword(wprm) = bn_clicked) and (hwnd(lprm) = hwndbutton) then begin destroywindow (hwindow); end; result:= 0; exit; end;
end;
result:=defwindowproc (hwindow, nmsg, wprm, lprm);
end;
//winmain var hwindowmain:hwnd; mymsg:msg; wcex:wndclassex;begin wcex.cbsize := sizeof(wndclassex); wcex.style := cs_hredraw or cs_vredraw; wcex.lpfnwndproc := @mainwndproc; wcex.cbclsextra := 0; wcex.cbwndextra := 0; wcex.hinstance := maininstance; wcex.hicon := loadicon (0, idi_application); wcex.hcursor := loadcursor (0, idc_arrow); wcex.hbrbackground := hbrush(color_window+1); wcex.lpszclassname := 'wintestwin'; wcex.hiconsm := loadicon (0, idi_application);
registerclassex (wcex);
hwindowmain := createwindow ( 'wintestwin', 'hello', ws_overlappedwindow, cw_usedefault, cw_usedefault, cw_usedefault, cw_usedefault, 0, 0, maininstance, nil );
showwindow (hwindowmain, cmdshow); updatewindow (hwindowmain);
while getmessage (mymsg, 0, 0, 0)=true do begin translatemessage (mymsg); dispatchmessage (mymsg); end; end.