window api创建窗体的一般步骤

//共三个主要步骤,1 注册wndclassex,2 产生窗体并实现msg循环,3 实现winproc回调函数

#include <Windows.h>

void registerMyWndclass(HINSTANCE hInstance);
LRESULT CALLBACK winProc(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam);


int APIENTRY WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	registerMyWndclass(hInstance);


//2创建窗口并实现循环
	HWND hWnd=CreateWindow("MYWINDOW","running snake",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,300,640,480,0,0,hInstance,NULL);
	if(hWnd)
	{
		ShowWindow(hWnd,SW_NORMAL);
		UpdateWindow(hWnd);
	}



	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
//1注册窗口类
void registerMyWndclass(HINSTANCE hInstance)
{
	WNDCLASSEX* wc=new WNDCLASSEX;
	wc->cbClsExtra=NULL;
	wc->cbSize=sizeof(WNDCLASSEX);
	wc->cbWndExtra=NULL;
	wc->hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc->hCursor=LoadCursor(NULL,IDC_ARROW);
	wc->hIcon=LoadIcon(NULL,IDI_WINLOGO);
	wc->hIconSm=NULL;
	wc->hInstance=hInstance;
	wc->lpfnWndProc=winProc;
	wc->lpszMenuName=NULL;
	wc->lpszClassName="MYWINDOW";
	wc->style=CS_HREDRAW |CS_VREDRAW;
	RegisterClassEx(wc);
}

//3回调函数
LRESULT CALLBACK winProc(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	///...more case statement

	default:
		break;
	}
	return 	DefWindowProc(hWnd,msg,wParam,lParam);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
program Hackdiy; uses Windows, Messages; var TheMessage: TMsg; const ClassName = 'MainForm_FOrm1'; // 窗体过程回调函数 function FormProc(hForm, MsgID, WParam, LParam: LongWord): LongWord; stdcall; const {$J+} TempFont: DWORD = 0; {$J-} ControlID1 = 1; ControlID2 = 2; ControlID3 = 3; var EditText: array[0..30] of Char; begin Result := DefWindowProc(hForm, MsgID, WParam, LParam); // 标准处理 case MsgID of WM_CREATE: begin TempFont := CreateFont(12, 6, 0, 0, FW_EXTRALIGHT, Byte(FALSE), Byte(FALSE), Byte(FALSE), GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, '宋体'); CreateWindowEx(0, 'BUTTON', '退出程序', WS_CHILD or WS_VISIBLE, 60, 20, 70, 25, hForm, ControlID2, HInstance, nil); SendMessage(GetDlgItem(hForm, ControlID2), WM_SETFONT, TempFont, 1); end; WM_COMMAND: begin if (HIWORD(wParam) = BN_CLICKED) then // 单击按钮 begin case LOWORD(wParam) of // 控件ID ControlID1: begin end; ControlID2: SendMessage(hForm, WM_CLOSE, 0, 0); end; end; end; WM_DESTROY: begin PostQuitMessage(0); DeleteObject(TempFont); end; end; end; // 注册窗体类 procedure Register_MainForm; var FormClass: TWndClass; begin FormClass.Style := CS_HREDRAW or CS_VREDRAW; FormClass.lpfnWndProc := @FormProc; FormClass.cbClsExtra := 0; FormClass.cbWndExtra := 0; FormClass.hInstance := SysInit.HInstance; FormClass.hIcon := LoadIcon(HInstance, 'Cool'); FormClass.hCursor := LoadCursor(0, IDC_ARROW); FormClass.hbrBackground := COLOR_WINDOW; FormClass.lpszMenuName := nil; FormClass.lpszClassName := ClassName; RegisterClass(FormClass); end; // 注销窗体类 procedure Unregister_MainForm; begin UnregisterClass(ClassName, HInstance); end; // 建立主窗体 procedure Create_MainForm; begin CreateWindowEx(WS_EX_TOPMOST, ClassName, 'Form1', WS_VISIBLE or WS_TILED or WS_SYSMENU or WS_MINIMIZEBOX or WS_SIZEBOX, 200, 200, 200, 100, 0, 0, HInstance, nil); end; begin Register_MainForm; Create_MainForm; while GetMessage(TheMessage, 0, 0, 0) do begin TranslateMessage(TheMessage); DisPatchMessage(TheMessage); end; end.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值