[MFC] Analog Acquisition System - Interface Design (17)

Features

Start interface

 Start collecting:

 PS: It does not involve data saving, reproduction and other functions

interface design

The interface is divided into three parts: the black strip on the top with the close button, the dialog box on the left, and the main interface on the right

resource:

 Top black bar top.bmp 2* 29 (width*height pixels)

 left dialog left.bmp

 Main dialog: bk.bmp

1. Realization of the black bar at the top - texture function

Main class: CBitmap to load pictures CRect to get window size etc. CDC to draw pictures

function:

Load pictures: LoadBitmap (CBitmap member function, recommended) LoadImage (API function)

Get image properties: BITMAP structure and GetBitmap

Size related: GetSystemMetrics (API function, get screen size) GetClientRect (window class function, get client area size)

CDC function: GetDC ReleaseDC Get and release DC CreateCompatibleDC Create compatible DC SelectObject Select GDI object BitBlt StretchBlt map (the following functions can be scaled)

About GDI and CDC

GDI is brushes, brushes, fonts, pictures, etc.

CDC is the device context, which can be understood as the combination of the human "hand" and the workspace - with the hand, you can pick up brushes, brushes and other tools to draw in the designated area!

Memory DC and display DC 

The display DC is the screen area, and its characteristic is that all operations are displayed in the window synchronously

The memory DC can be understood as the same memory area as the display DC, and its operations will not be displayed on the window synchronously. The content of the memory DC can be "pasted" to the display DC by means of a map to display the content.

Specific steps:

Create a dialog-type MFC program (omitted), delete the button of the main dialog, and remove the title bar:

Load resources:

 The CBitmap instance is defined in the dialog class header file (the pointer is also available, new is required)

CBitmap m_top; and memory DC pointer CDC *mdc;

In the dialog box CPP file: OnInitDialog() function (initialization function, only done once)

BOOL CMFC09Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_top.LoadBitmap(IDB_TOP);
	mdc = new CDC;
	CDC *dc = this->GetDC();
	mdc->CreateCompatibleDC(dc);
	this->ReleaseDC(dc);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

Load the image, create an mdc compatible with the display DC, remember to release the DC

OnPaint() function (redraw message response function, may be executed multiple times)

void CMFC09Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPaintDC dc(this);
		CRect rect;
		GetClientRect(&rect);
		mdc->SelectObject(&m_top);
		dc.SetStretchBltMode(STRETCH_HALFTONE);
		dc.StretchBlt(0,0,rect.right,30,mdc,0,0,2,29,SRCCOPY);
	
		CDialogEx::OnPaint();
	}
}

After running, the title bar of the dialog box is displayed:

The main window is also a similar operation, the merged code is:

BOOL CMFC09Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_top.LoadBitmap(IDB_TOP);
	m_BackGround.LoadBitmap(IDB_BK);
	mdc = new CDC;
	CDC *dc = this->GetDC();
	mdc->CreateCompatibleDC(dc);
	this->ReleaseDC(dc);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}
void CMFC09Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPaintDC dc(this);
		CRect rect;
		GetClientRect(&rect);
		mdc->SelectObject(&m_top);
		dc.SetStretchBltMode(STRETCH_HALFTONE);
		dc.StretchBlt(0,0,rect.right,30,mdc,0,0,2,29,SRCCOPY);
	    mdc->SelectObject(&m_BackGround);
		dc.StretchBlt(180,30,rect.right - 150,rect.bottom - 30,mdc,0,0,735,549,SRCCOPY);
		CDialogEx::OnPaint();
	}
}

 The definition of m_BackGround is the same as the picture at the top, in the header file.

StretchBlt is a texture function with scaling, and it is necessary to carefully understand the meaning of each parameter.

operation result:

 The settings of window size and position, and the realization of the dialog box on the left will be introduced later.

Guess you like

Origin blog.csdn.net/yixiaobo2001/article/details/129027933