用Window API创建窗口,请看这里(0分)

  • 主题发起人 主题发起人 Sachow
  • 开始时间 开始时间
(抱歉,忘了贴内容^_^)<br>近几天我在研究Dephi动态创建窗口的方法,今天终于有了点眉目,现想和朋友<br>们探讨一下。<br><br>program APIWindow;<br><br>uses<br>&nbsp; Windows,<br>&nbsp; MMSystem,<br>&nbsp; Messages;<br><br>var<br>&nbsp; WinClass: TWndClassA;<br>&nbsp; Inst, Handle: Integer;<br>&nbsp; Msg: TMsg;<br>&nbsp; AppName: PChar;<br><br>{ 定义WindowProc过程 }<br>function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;<br>var<br>&nbsp; hdc: Integer;<br>&nbsp; Rect: TRect;<br>&nbsp; ps: PAINTSTRUCT;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; case uMsg of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WM_CREATE : &nbsp;//在窗口建立时播放声音<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PlaySound('TestSnd.wav', 0, SND_FILENAME + SND_ASYNC) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WM_PAINT :<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin &nbsp; &nbsp; //在窗口中央输入文字<br> &nbsp; &nbsp; &nbsp; hdc := BeginPaint (Handle, ps) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetClientRect (Handle, Rect) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DrawText (hdc, 'Hello, Windows 95!', -1, Rect,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DT_SINGLELINE + DT_CENTER + DT_VCENTER) ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EndPaint (Handle, ps) ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WM_DESTROY :<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// PostQuitMessage(0) ; &nbsp;//这一句似乎不管用,要Halt才行<br> &nbsp; &nbsp; &nbsp; Halt; <br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; Result := DefWindowProc(hWnd, uMsg, wParam, lParam);<br>end;<br><br>begin<br>&nbsp; AppName := 'HelloWin';<br><br>&nbsp; { 初始化窗体类 }<br>&nbsp; Inst := hInstance;<br>&nbsp; with WinClass do<br>&nbsp; begin<br>&nbsp; &nbsp; style &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= CS_HREDRAW + CS_VREDRAW;<br>&nbsp; &nbsp; lpfnWndProc &nbsp; &nbsp; &nbsp; &nbsp;:= @WindowProc;<br>&nbsp; &nbsp; hInstance &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Inst;<br>&nbsp; &nbsp; hbrBackground &nbsp; &nbsp; &nbsp;:= GetStockObject (WHITE_BRUSH);<br>&nbsp; &nbsp; lpszClassname &nbsp; &nbsp; &nbsp;:= AppName;<br>&nbsp; &nbsp; hCursor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= LoadCursor(0, IDC_ARROW);<br>&nbsp; end; <br>&nbsp; RegisterClass(WinClass); &nbsp;//注册窗体类<br><br>&nbsp; { 创建窗口 }<br>&nbsp; &nbsp; &nbsp;Handle := CreateWindow (AppName, &nbsp; &nbsp; &nbsp; &nbsp; // 窗体类名称<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'The Hello Program', &nbsp; &nbsp; // 窗体标题<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WS_OVERLAPPEDWINDOW, &nbsp; &nbsp; // 窗体风格<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化窗体X座标<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化窗体Y座标<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化窗体X尺寸<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CW_USEDEFAULT, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 初始化窗体Y尺寸<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 父窗体句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 窗体菜单句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inst, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 程序实例(instance)句柄<br> &nbsp; &nbsp;nil) ; // 其它参数<br><br>&nbsp; ShowWindow(Handle, SW_SHOWNORMAL);<br>&nbsp; UpdateWindow(Handle);<br><br>&nbsp; { 消息循环 }<br>&nbsp; while(GetMessage(Msg, Handle, 0, 0)) do<br>&nbsp; begin<br>&nbsp; &nbsp; TranslateMessage(msg);<br>&nbsp; &nbsp; DispatchMessage(msg);<br>&nbsp; end; <br>end.<br><br>程序基本上是Charles Petzold的HelloWin.c例程的翻版,运行起来看上去<br>也一模一样,但是在编译时Delphi会报警告:“Constant expression<br>voilates subrange bounds”,这是为什么?还有一个不解,API函数中有<br>很多变量类型,如HWND、hInstance、hdc等,它们实际上是整数型的,但<br>和整数型的有什么区别?在定义函数时为什么不直接定义为Int、long等?<br>
 
关于:“Constant expressionvoilates subrange bounds”可能是数据对齐的问题,delphi默认的是对齐的,可以用编译器指令去掉;<br>至于hwnd,hinstance。。主要是为了程序可读,为什么这样叫得去问微软了 ~-~<br>
 
1,我做了好多,怎么没有这个警告呀?<br><br>2,可读性,清晰性。<br><br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WM_DESTROY :<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// PostQuitMessage(0) ; &nbsp;//这一句似乎不管用,要Halt才行<br> &nbsp; &nbsp; &nbsp; Halt;<br>我也是,搞不懂为什么, 窗体虽然关了,但整个程序还在运行,<br>按说 GetMessage 得到 WM_QUIT 就会为 0 的,<br>??
 
接受答案了.
 
后退
顶部