My C# Menu Class
Hey guys, I attempted to get a menu working on my C# hack but I couldn't get the d3d device so I just gave up. I'ma just give up here after a few hours of googling, there seems to be no simple solution I could use. If anyone manages to get it, please help me out. Otherwise here is my menu code... It might not work as I can't test it without getting the device.
This is a basic port of Void's menu, Void also helped me out on a lot of the menu...
To C&P'ers, theres no point as this code will not work without a device.
Note: This code supports display numeral values as well as On/Off, the number can only go up to 10, but you can easily change that.
[PHP]//User1's C# Menu Class, ported from Void's menu class.
//Please give credits when used.
//This code might catastrophically fail as I never tried this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace CombatArmsHack
{
class Menu
{
//Please tell me you understand the below code...
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
private System.Drawing.Font menuFont = new System.Drawing.Font("Comic Sans MS", 15); //The font to set the D3D Font to...
private Device m_Device; //D3D Device, if you can get this Managed or Unmanaged, please contact me.
private Font m_Font; //D3D Font
public List<MenuItem> MenuItems = new List<MenuItem>();
//Creates the font and sets the device
public Menu(Device device)
{
m_Device = device;
m_Font = new Font(m_Device,menuFont);
}
//Add a new Item to the Menu.
public void AddItem(string Text, int State, bool Logic)
{
MenuItem NewItem = new MenuItem();
NewItem.Text = Text;
NewItem.State = State;
NewItem.Logic = Logic;
MenuItems.Add(NewItem);
}
//Draws menu.
public void RenderMenu()
{
//Rectangle where one of the menu items will be rendered
System.Drawing.Rectangle CurRect = new System.Drawing.Rectangle(20,20,150,20);
for(int i = 0; i < MenuItems.Count;i++)
{
bool Selected = MenuItems[i].Selected;
int State = MenuItems[i].State;
bool Logic = MenuItems[i].Logic;
string Text = MenuItems[i].Text;
if(Selected)
{//Item is Selected
m_Font.DrawText(null,Text,CurRect,DrawTextFormat.L eft,System.Drawing.Color.Lime);
if(Logic)
{//Item is On/Off
if(State == 0)
{//Item Off
m_Font.DrawText(null,"Off",CurRect,DrawTextFormat. Right,System.Drawing.Color.Red);
}
else
{//Item On
m_Font.DrawText(null,"On",CurRect,DrawTextFormat.R ight,System.Drawing.Color.White);
}
}
else
{//If Not boolean, draw value
m_Font.DrawText(null,State.ToString(),CurRect,Draw TextFormat.Right,System.Drawing.Color.White);
}
}
else
{//Normal Item
m_Font.DrawText(null,Text,CurRect,DrawTextFormat.L eft,System.Drawing.Color.CornflowerBlue);
if(Logic)
{//Item is On/Off
if(State == 0)
{//Item Off
m_Font.DrawText(null,"Off",CurRect,DrawTextFormat. Right,System.Drawing.Color.Red);
}
else
{//Item On
m_Font.DrawText(null,"On",CurRect,DrawTextFormat.R ight,System.Drawing.Color.White);
}
}
else
{//Draw Number
m_Font.DrawText(null,State.ToString(),CurRect,Draw TextFormat.Right,System.Drawing.Color.White);
}
}
CurRect.Y += 17; //Shift rectangle down so the text does not get clipped.
}
}
//Takes care of which menu item is selected, and if it is on or off.
public void Controls()
{
int CurIndex = 0;
if(GetAsyncKeyState(System.Windows.Forms.Keys.Down ) != 0)
{
CurIndex++;
if (CurIndex > MenuItems.Count - 1)
{
CurIndex = MenuItems.Count - 1;
}
MenuItems[CurIndex].Selected = true;
try
{
MenuItems[CurIndex - 1].Selected = false;
}
catch (Exception) { }
}
if (GetAsyncKeyState(System.Windows.Forms.Keys.Up) != 0)
{
CurIndex--;
if (CurIndex < 0)
{
CurIndex = 0;
}
MenuItems[CurIndex].Selected = true;
try
{
MenuItems[CurIndex + 1].Selected = false;
}
catch (Exception) { }
}
if (GetAsyncKeyState(System.Windows.Forms.Keys.Right) != 0)
{
int CurState = MenuItems[CurIndex].State;
if (MenuItems[CurState].Logic)
{//If Boolean
if (CurState == 0)
{
CurState++;
}
else
{
CurState = 0;
}
}
else
{//Numeral Value
CurState++;
if (CurState > 10)
{
CurState = 10;
}
}
}
if (GetAsyncKeyState(System.Windows.Forms.Keys.Left) != 0)
{
int CurState = MenuItems[CurIndex].State;
if (MenuItems[CurState].Logic)
{//If Boolean
if (CurState == 0)
{
CurState = 1;
}
else
{
CurState = 0;
}
}
else
{//Numeral Value
CurState--;
if (CurState < 0)
{
CurState = 0;
}
}
}
}
public void Release()
{//Release Unmanaged memory.
m_Font.Dispose();
m_Device.Dispose();
}
}
//Menu Items
class MenuItem
{
public string Text; //Text to Display
public bool Selected; //Highlight?
public int State; //On/Off/Number
//If Logic is true, 0 = Off, 1 = On. Else it is a number
public bool Logic; //State is Bool or Num?
//If Logic = True, then the menu will display the State as On/Off.
}
}
[/PHP]
I hope one day someone can put this to good use, and again please help me get the D3D Device if possible...
Yes I know this code might be an epic fail as well, I'm not at all experienced with DX, or MDX.