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 


 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值