Win32项目自制边框,加载PNG背景

先上一张最终效果图:
在这里插入图片描述
这里用到的背景是:
在这里插入图片描述
空白的地方最好做成镂空,避免后期闪烁问题
下面是代码:

//图片相对途径
static char Main_Background[30] = "main\\Main_Background.png";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine, int nCmdShow)
{
	// Main message loop: 
	// 添加用于侦听操作系统所发送消息的消息循环。 
	// 当应用程序收到一条消息时,此循环将该消息调度到 WndProc 函数以进行处理。 
	MSG msg;
	//文件路径查找
	Func_Pic_Path("");
	strcpy(FilePath, LOGO_PATH);
	//MessageBox(NULL, FilePath, "", NULL);
	//对设定的窗口进行注册
	MyRegisterClass(hInst);
	MyRegisterClass2(hInst);
	if (!InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}
	//进行消息循环
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg); //翻译消息
		DispatchMessage(&msg);  //派遣消息
	}
	return (int)msg.wParam;
}
//拼接字符串的函数,输入两个char型指针
void link(char *s, char *t)
{
	while (*s != '\0')
	{
		s++;
	}
	while (*t != '\0')
	{
		*s++ = *t++;
	}
	*s = '\0';
}
//由相对路径找到PNG图片在本机的路径,最后路径存储在全局常量LOGO_PATH中
//在这里的link(char *,char*)函数是我自己做的拼接字符串的函数,在这里用于拼接路径
void Func_Pic_Path(char *ss)
{
	//路径
	TCHAR szFilePath[MAX_PATH + 1] = { 0 };
	char pic_path[100] = "pic\\";
	link(pic_path,ss);
	GetModuleFileName(NULL, szFilePath, MAX_PATH);
	(_tcsrchr(szFilePath, _T('\\')))[1] = 0; // 删除文件名,只获得路径字串
	char *str_url = szFilePath;  //例如str_url==e:\program\Debug
	wsprintf((char *)LOGO_PATH, str_url);
	link(LOGO_PATH, pic_path);
}
//输入路径,得到图片image指针
Image* HyqOnCreate(char*path)
{
	Image *image;
	char pg5[100];
	strcpy(pg5, FilePath);
	link(pg5, path);
	CT2CW strFileName5(pg5);
	image = Image::FromFile((LPCWSTR)strFileName5);
	if ((image == NULL) || (image->GetLastStatus() != Ok))
	{
		try { delete m_pSetting_S; }
		catch (...) { assert(FALSE); } image = NULL;
		return image;
	}
	return image;
}
//用GDI把PNG图片画出
void DrawPngWithImage(RectF rect,HDC DC,Image *image)
{
	assert(image != NULL);
	if (image == NULL) return ;
	assert(DC != NULL);
	Graphics graphics(DC);
	graphics.DrawImage(image, rect, 0, 0, rect.Width, rect.Height, UnitPixel);
}
//加载png图片函数
bool OnCreate(HWND hwnd)
{
	//加载文件  
	m_Imagebmp.Load(TEXT("F:\\"));
	m_pImage = HyqOnCreate(Main_Background);
	return true;
}
//绘画PNG图像
bool DrawImagePng(HDC DC, INT nXPos, INT nYPos,int Width,int Height,int pngNum)
{
	//构造位置
	RectF rcDrawRect;
	rcDrawRect.X = (REAL)nXPos;
	rcDrawRect.Y = (REAL)nYPos;
	rcDrawRect.Width = Width;
	rcDrawRect.Height = Height;
	switch (pngNum)
	{
	case MAIN_BACKGROUND_NUM:
	{
		DrawPngWithImage(rcDrawRect, DC, m_pImage);
		break;
	}
	}
	return true;
}
LRESULT CALLBACK  WndProcMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	switch (message)
	{
	case WM_PAINT:
	{
		hdc = BeginPaint(hWnd, &ps);
		//加载主界面的背景图片文件,
		//MAIN_SRC_WIDTH是图片要显示的宽度,最好跟原图一样大
		//MAIN_SRC_HEIGHT是图片的高度,最好也是原图的参数
		//以上两个参数可以按比例缩小放大
		//MAIN_BACKGROUND_NUM是图片的编号,因为我要加载的图比较多,所以设置了这个参数
		DrawImagePng(hdc, 0, 0, MAIN_SRC_WIDTH, MAIN_SRC_HEIGHT, MAIN_BACKGROUND_NUM);
		DeleteObject(hdc);
		EndPaint(hWnd, &ps);
		break;
	}
	//爱你哟lxy
	}
}

以上作为笔记跟大家分享,感兴趣的下载以后可以调试,有错误请指正

猜你喜欢

转载自blog.csdn.net/hu421160052/article/details/87627495
今日推荐