当前位置:网站首页>Create a form whose client area is 800 pixels by 600 pixels

Create a form whose client area is 800 pixels by 600 pixels

2022-06-11 07:11:00 lihongtao8209

1.Windows Desktop wizard

choice “windows Desktop wizard ”, Click on " next step ".

2. Prepare new items

modify “ Project name ”, modify “ Location ”, Click on “ establish ”.

3.Windows Desktop projects

choice “ Application type ” by “ Desktop applications .exe”.

“ Other options ” choice “ Empty item ”, Click on " determine ”.

4. Code segment

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 800 + 16, 600 + 59, nullptr, nullptr, hInstance, nullptr);

5. Specific code

//  Global variables :
HINSTANCE hInst;								//  The current instance 
TCHAR szTitle[MAX_LOADSTRING];					//  Title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];			//  Main window class name 

...... // Omit some code 

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; //  Store instance handles in global variables 
   /*
   *  Width = Right border width + Left border width + Customer area width =800+16=816
   *  Height = Status box height + Menu box Height +( Bottom border height + Top border height )+ Customer area height =23+20+16+600=659
   */
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 800+16, 600+59, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//   function : WndProc(HWND, UINT, WPARAM, LPARAM)
//
//   Purpose :  Processing messages from the main window .
//
//  WM_COMMAND	-  Handle the application menu 
//  WM_PAINT	-  Draw the main window 
//  WM_DESTROY	-  Send an exit message and return 
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	WCHAR wcSize[64]={0L};					//  Print information 
	RECT rect;								//  Client area size 
	int  statusBarH;
	int  menuBarH;
	int wcstrlen;
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		//  Analysis menu selection :
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
		// Window creation 
	case WM_CREATE: 
		{
			
		GetClientRect(hWnd, &rect);
		break;
		}
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		//  Get the customer area 
		GetClientRect(hWnd,&rect);
	    wcstrlen=wsprintf(wcSize,_T("left=%d,right=%d,top=%d,bottom=%d"),rect.left,rect.right,rect.top,rect.bottom);
		TextOut(hdc,0,0,wcSize,wcstrlen);
		// Zero clearing 
		_wcsset_s(wcSize,64,0);
		// Get status bar height 
		statusBarH=GetSystemMetrics(SM_CYCAPTION);
		wcstrlen=wsprintf(wcSize,_T("STATUS BAR HEIGHT=%d"),statusBarH);
		TextOut(hdc,0,16,wcSize,wcstrlen);
		// Zero clearing 
		_wcsset_s(wcSize,64,0);
		// Get the menu bar height 
		menuBarH=GetSystemMetrics(SM_CYMENU);
		wcstrlen=wsprintf(wcSize,_T("MENU BAR HEIGHT=%d"),menuBarH);
		TextOut(hdc,0,32,wcSize,wcstrlen);
		 

		EndPaint(hWnd, &ps);
		break;
	// When the user clicks the close button in the upper right corner of the window 
	case WM_CLOSE:    
		{
			DestroyWindow(hWnd);    // Destruction of the window 
			break;
		}
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

...... // Omit some code 

 

原网站

版权声明
本文为[lihongtao8209]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020522382751.html