DirectX学习2---建立一个简单的窗口

总共包括4个部分

 

1、WinMain函数

    程序的入口函数,调用SystemClass类的相关函数

2、SystemClass类

    窗口管理类,进行窗口的建立,消息的分发,以及各种基本的消息响应

3、InputClass类

   输入管理类,记录键盘输入

4、GraphicsClass类

   处理DirectX的图形代码的类,由于只是建立一个空的对话框,所以这个类几乎么有什么内容

 

WinMain部分

#include "systemclass.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
	SystemClass* System;
	bool result;          // 创建system对象。    
	System = new SystemClass;
	if(!System)
	{
		return 0;    
	}         //初始化并运行system对象。    
	result = System->Initialize();
	if(result)
	{        
		System->Run();
	}          // 关闭并释放system对象。    
	System->Shutdown();
	delete System;
	System = 0;
	return 0;
}

 

SystemClass类部分

SystemClass.h

#ifndef SYSTEMCLASS_H
#define SYSTEMCLASS_H
#define WIN32_LEAN_AND_MEAN//去除某些不常用的API
#include <windows.h>

#include "inputclass.h"
#include "graphicsclass.h"

class SystemClass
{
	public:    
	SystemClass();
	SystemClass(const SystemClass&);
	~SystemClass();
	
	bool Initialize();
	void Shutdown();
	void Run();
	LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);

private:
	bool Frame();
	void InitializeWindows(int&, int&);
	void ShutdownWindows();

private:
	LPCWSTR m_applicationName;
	HINSTANCE m_hinstance;
	HWND m_hwnd;
	InputClass* m_Input;
	GraphicsClass* m_Graphics;
};
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static SystemClass* ApplicationHandle = 0; 

#endif

systemclass.cpp

#include "Systemclass.h"

SystemClass::SystemClass():  m_Input(NULL),  m_Graphics(NULL) {

}

SystemClass::SystemClass(const SystemClass&) {

}

SystemClass::~SystemClass() {

}

bool SystemClass::Initialize() {  int ScreenWindth,ScreenHeight;  ScreenHeight=ScreenWindth=0;  InitializeWindows(ScreenWindth,ScreenHeight);

 m_Input=new InputClass();

 m_Input->Initialize();

 m_Graphics=new GraphicsClass();

 int ret=m_Graphics->Initialize(ScreenWindth,ScreenHeight,m_hwnd);

 if(ret==0)  {   return false;  }

 return true; }

void SystemClass::Shutdown() {  if(m_Graphics!=NULL)  {   m_Graphics->Shutdown();   delete m_Graphics;   m_Graphics=NULL;  }

 if(m_Input!=NULL)  {   delete m_Input;   m_Input=NULL;  }

 ShutdownWindows(); }

void SystemClass::Run() {  MSG msg;  bool done,result;  ZeroMemory(&msg,sizeof(MSG));

 done=false;

 while (!done)  {   if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))   {    TranslateMessage(&msg);    DispatchMessage(&msg);   }

  if(msg.message==WM_QUIT)   {    done=true;   }   else   {    result=Frame();    if(!result)    {     done=true;    }   }  } }

bool SystemClass::Frame() {  bool result;  if(m_Input->IsKeyDown(VK_ESCAPE))  {   return false;  }  result=m_Graphics->Frame();  if(!result)  {   return false;  }  return true; }

LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) {  switch(umsg)  {  case WM_KEYDOWN:   m_Input->KeyDown((unsigned int)wParam);   break;  case WM_KEYUP:   m_Input->KeyUp((unsigned int)wParam);   break;  default:   return DefWindowProc(hwnd,umsg,wParam,lParam);  }  return 0; }

void SystemClass::InitializeWindows(int& width, int& height) {  WNDCLASSEX wc;  DEVMODE dmScreenSettings;  int posX,posY;

 ApplicationHandle=this;

 m_hinstance=GetModuleHandle(NULL);

 m_applicationName=TEXT("Demo Window");

    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;  wc.lpfnWndProc   = WndProc;  wc.cbClsExtra    = 0;  wc.cbWndExtra    = 0;  wc.hInstance     = m_hinstance;  wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);  wc.hIconSm       = wc.hIcon;  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);  wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);  wc.lpszMenuName  = NULL;  wc.lpszClassName = m_applicationName;  wc.cbSize        = sizeof(WNDCLASSEX);

 RegisterClassEx(&wc);

 width=GetSystemMetrics(SM_CXSCREEN);  height=GetSystemMetrics(SM_CYSCREEN);

 if(FULL_SCREEN)  {   memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));   dmScreenSettings.dmSize       = sizeof(dmScreenSettings);   dmScreenSettings.dmPelsWidth  = (unsigned long)width;   dmScreenSettings.dmPelsHeight = (unsigned long)height;    dmScreenSettings.dmBitsPerPel = 32;   dmScreenSettings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;    ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);      posY=posX=0;  }  else  {   width=800;   height=600;   posX = (GetSystemMetrics(SM_CXSCREEN) - width)  / 2;   posY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;  }

 m_hwnd=CreateWindowEx(WS_EX_APPWINDOW,m_applicationName,m_applicationName,   WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_POPUP,   posX,posY,width,height,NULL,NULL,m_hinstance,NULL);

 ShowWindow(m_hwnd,SW_SHOW);  SetForegroundWindow(m_hwnd);  SetFocus(m_hwnd);

 ShowCursor(false);  return;

}

void SystemClass::ShutdownWindows() {  ShowCursor(true);  if(FULL_SCREEN)  {   ChangeDisplaySettings(NULL,0);  }  DestroyWindow(m_hwnd);

 m_hwnd=NULL;

 UnregisterClass(m_applicationName,m_hinstance);  m_hinstance=NULL;  ApplicationHandle=NULL; }

LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam) {  switch(umessage)  {  case WM_DESTROY:   PostQuitMessage(0);   break;  case WM_CLOSE:   PostQuitMessage(0);   break;  default:   return ApplicationHandle->MessageHandler(hwnd,umessage,wparam,lparam);  }  return 0; }

 

 

InputClass类部分

Inputclass.h

#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_
class InputClass
{
public:
	InputClass();
	InputClass(const InputClass&);
	~InputClass(); 

	void Initialize();
	void KeyDown(unsigned int);
	void KeyUp(unsigned int); 
	bool IsKeyDown(unsigned int);

private:
	bool m_keys[256];
}; 

#endif 

inputclass.cpp

#include "inputclass.h"
#include <windows.h>
InputClass::InputClass()
{

}  

InputClass::InputClass(const InputClass& other)
{

}  

InputClass::~InputClass()
{

}  

void InputClass::Initialize()
{    
	int i; 
	for(i=0; i<256; i++)
	{ 
		m_keys[i] = false;
	}    
	return;
}  

void InputClass::KeyDown(unsigned int input)
{ 
	m_keys[input] = true;
	return;
}  

void InputClass::KeyUp(unsigned int input)
{  
	m_keys[input] = false;
	return;
} 

bool InputClass::IsKeyDown(unsigned int key)
{
	return m_keys[key];
}

GraphicsClass类部分

graphicsclass.h

#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_
#include<windows.h>

const bool FULL_SCREEN = true;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;


class GraphicsClass
{
public:   
	GraphicsClass(); 
	GraphicsClass(const GraphicsClass&);
	~GraphicsClass(); 

	bool Initialize(int, int, HWND); 
	void Shutdown();
	bool Frame();

private: 
	bool Render();
private: 

};

#endif

Graphicsclass.cpp

#include "graphicsclass.h" 

GraphicsClass::GraphicsClass()
{

}  

GraphicsClass::GraphicsClass(const GraphicsClass& other)
{

}  

GraphicsClass::~GraphicsClass()
{

}  

bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{    
	return true;
}  

void GraphicsClass::Shutdown()
{     
	return;
}  

bool GraphicsClass::Frame()
{     
	return true;
}  

bool GraphicsClass::Render()
{    
	return true;
}

 

这四个部分可以实现一个简单的窗口。


#include "graphicsclass.h" 

GraphicsClass::GraphicsClass()
{

}  

GraphicsClass::GraphicsClass(const GraphicsClass& other)
{

}  

GraphicsClass::~GraphicsClass()
{

}  

bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{    
	return true;
}  

void GraphicsClass::Shutdown()
{     
	return;
}  

bool GraphicsClass::Frame()
{     
	return true;
}  

bool GraphicsClass::Render()
{    
	return true;
}


 


GraphicsClassGraphicsClassGraphicsClass

 


#include "inputclass.h"
#include <windows.h>
InputClass::InputClass()
{

}  

InputClass::InputClass(const InputClass& other)
{

}  

InputClass::~InputClass()
{

}  

void InputClass::Initialize()
{    
	int i; 
	for(i=0; i<256; i++)
	{ 
		m_keys[i] = false;
	}    
	return;
}  

void InputClass::KeyDown(unsigned int input)
{ 
	m_keys[input] = true;
	return;
}  

void InputClass::KeyUp(unsigned int input)
{  
	m_keys[input] = false;
	return;
} 

bool InputClass::IsKeyDown(unsigned int key)
{
	return m_keys[key];
}


 

 

 


#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_
class InputClass
{
public:
	InputClass();
	InputClass(const InputClass&);
	~InputClass(); 

	void Initialize();
	void KeyDown(unsigned int);
	void KeyUp(unsigned int); 
	bool IsKeyDown(unsigned int);

private:
	bool m_keys[256];
}; 

#endif 


 


 

 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值