• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

C# - EnumDisplaySettings() returns a wrong dmPelsWidth

Status
Not open for further replies.
Level 4
Joined
May 25, 2009
Messages
100
[FIXED] C# - EnumDisplaySettings() returns a wrong dmPelsWidth

Hi,
I tried making my own plugin for Sharpcraft but it's not working the way i want to.

The Plugin is meant to get the screen resolution and sets the mousecursor in the middle of the screen and it's working in general...the only thing is, that dmPelsWidth is set to '32' and not '1050' as it should be...dmPelsHeight is set to '1680'.

Does somebody has an idea, what could be wrong?

Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using TinkerWorX.SharpCraft.Game;
using TinkerWorX.SharpCraft.Game.Jass;


[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
    private const int CCHDEVICENAME = 0x20;
    private const int CCHFORMNAME = 0x20;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
    public string dmDeviceName;
    public short dmSpecVersion;
    public short dmDriverVersion;
    public short dmSize;
    public short dmDriverExtra;
    public int dmFields;
    public int dmPositionX;
    public int dmPositionY;
    public int dmDisplayFixedOutput;
    public short dmColor;
    public short dmDuplex;
    public short dmYResolution;
    public short dmTTOption;
    public short dmCollate;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
    public string dmFormName;
    public short dmLogPixels;
    public int dmBitsPerPel;
    public int dmPelsWidth;
    public int dmPelsHeight;
    public int dmDisplayFlags;
    public int dmDisplayFrequency;
    public int dmICMMethod;
    public int dmICMIntent;
    public int dmMediaType;
    public int dmDitherType;
    public int dmReserved1;
    public int dmReserved2;
    public int dmPanningWidth;
    public int dmPanningHeight;
}

namespace Cataract92.KtKPlugin
{
    public class KtKPlugin : GamePluginBase
    {
        public override String Name { get { return "KtK Plugin"; } }
        public override Version Version { get { return new Version(0, 2); } }
        
		[DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
		
        private delegate void CenterMousePrototype();
        private void CenterMouse() {
            DEVMODE vDevMode = new DEVMODE();
            EnumDisplaySettings(null, -1, ref vDevMode);
            Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}", vDevMode.dmPelsWidth, vDevMode.dmPelsHeight, 1 << vDevMode.dmBitsPerPel, vDevMode.dmDisplayFrequency);

            SetCursorPos(vDevMode.dmPelsWidth / 2, vDevMode.dmPelsHeight / 2); 
        }		

        public override void Initialize() {
            WarcraftIII.AddNative(new CenterMousePrototype(this.CenterMouse));
        }

    }
}
 
Last edited:
Level 4
Joined
May 25, 2009
Messages
100
changing to -2 didn't work.
I'm not really into C# and the only two function i found on the internet were EnumDisplaySettings() and Screen.PrimaryScreen.Bounds, but Screen.PrimaryScreen.Bounds is just for a window.form which Sharpcraft doesn't have, afaik.

Here's the output i get:
9dm0.jpg
 
I'm not sure about sharpcraft but this is what I use to display the resolution in any of my programs. It is in C#
Code:
System.Windows.Forms.MessageBox.Show("Height = " + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString()
                        + " Width = " + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString());
Without the message box i use to show the screen.
Code:
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
 
Level 4
Joined
May 25, 2009
Messages
100
Yeah...i tried it, but i got an error, that Sharpcraft couldn't find the "Forms" in System.Windows and i looked it up and it said, that it's just for C# Windows Presentation Foundation (WPF) and because Sharpcraft doesn't have any GUI i think that sharpcraft isn't a WPF-project
 
Level 4
Joined
May 25, 2009
Messages
100
okay, I did a bit of research. To use screen class i have to add the System.Windows.Forms.dll, the only problem is that it isn't in sharpcraft yet. So I have to add the .dll on the fly to my .cs file to get the class Screen. I got to the point where i have to use the Assembly-class, but i can't find anything about adding a class to the code...everything i can find is just adding a method to the code. For Example
Does somebody of you has any idea how to do it?
 
Level 4
Joined
May 25, 2009
Messages
100
The problem is, that I only have one .cs file...no project.
And if I release my map I just want the player to download the additional plugin and copy it into the sharpcraft/plugins folder and I have no idea in which file the refernces of a project are saved so I can put it which my plugin in a .rar file.
 
Level 14
Joined
Jan 5, 2009
Messages
1,127
So Sharpcraft handles the compiling and stuff..

This MIGHT work:

JASS:
public const int WIDTH = 0;
public const int HEIGHT = 1;
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);

JASS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Resolution
{
    class Program
    {
        public const int WIDTH = 0;
        public const int HEIGHT = 1;
        [DllImport("user32.dll")]
        public static extern int GetSystemMetrics(int nIndex);
        static void Main(string[] args)
        {
            Console.WriteLine("Width: " + GetSystemMetrics(WIDTH));
            Console.WriteLine("Height: " + GetSystemMetrics(HEIGHT));
            Console.Read();
        }
    }
} // Full Example Code, for Console Application.
 
Last edited:
Level 4
Joined
May 25, 2009
Messages
100
It's working perfectly :D
Thanks alot!

Why didn't i find it on my research?
Everybody was just talking about GetEnumDisplaySettings() and the Screenclass...

Maybe i should have had a closer look to the user32.dll because it's already a references...
 
Level 15
Joined
Oct 18, 2008
Messages
1,591
It's working perfectly :D
Thanks alot!

Why didn't i find it on my research?
Everybody was just talking about GetEnumDisplaySettings() and the Screenclass...

Maybe i should have had a closer look to the user32.dll because it's already a references...

It's usually not indicated anywhere since it's a non-managed library, and as such many programmers don't even know about it's existence, not to mention it's functions.

Also, I wonder what it returns on multiple screens for width - it may just display the sum of the screen width, but never tried getting the screen resolution with it.
 
Level 14
Joined
Jan 5, 2009
Messages
1,127
It's usually not indicated anywhere since it's a non-managed library, and as such many programmers don't even know about it's existence, not to mention it's functions.

Also, I wonder what it returns on multiple screens for width - it may just display the sum of the screen width, but never tried getting the screen resolution with it.

It only returns the primary monitor, if you want the width of all the screens then you can:

Width: 78
Height: 79
 
Status
Not open for further replies.
Top