6

I don't know the .NET framework (4.5) well enough, so here's a question I can't find an answer to.

How do I get the screen resolution of the primary screen when not working with windows forms or any other graphical environment like WPF, Silverlight, ASP.NET, etc? I'm trying to get the resolution in a class library (dll) and pass it on from there.

Does .NET have such functionality?

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
  • You can add a reference to windows forms even if you do not have a GUI interface. Is that an option? – Sam Plus Plus Apr 28 '13 at 21:29
  • http://stackoverflow.com/questions/1317235/c-get-complete-desktop-size?rq=1 – bizzehdee Apr 28 '13 at 21:39
  • @SamPlusPlus: Thanks but that doesn't work, I'm getting an error when adding the reference. – DerpyNerd Apr 28 '13 at 22:04
  • @bizzehdee: I'll give the presentationFramework a shot, thanks! – DerpyNerd Apr 28 '13 at 22:04
  • If you don't use any of the class libraries that actually care about the screen size then why would you care about it? Do avoid falling in the trap of writing "this is what my machine looks like" code. Common for new programmers, it's an exercise of sorts and console mode apps are easy, but it isn't useful to learn these things. Read Petzold to learn the core. – Hans Passant Apr 28 '13 at 22:26
  • @HansPassant: You got me, I am a student, but I don't think i'm noobish anymore. I'm trying to saperate some code by putting most of the code that doesn't have to be in my forms' code-behind in a sparerate file (in this case a saperate dll) – DerpyNerd Apr 28 '13 at 22:33
  • @HansPassant Actually, there are totally legit reasons for wanting to know the user's screen size and they have everything to do with making an app responsive and not tied to an individual screen size, so that it can change certain layout things to optimize to, say, screens within a few different ranges. – Dave Munger Jan 16 '14 at 18:52

2 Answers2

11

Although you're not working in a Winforms enviroment, you can still add a reference to it's DLLs. Adding a reference to the System.Windows.Forms.dll means that you can use:

SystemInformation.VirtualScreen.Width   
SystemInformation.VirtualScreen.Height
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • 2
    +1: note that [PrimaryMonitorSize](http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.primarymonitorsize.aspx) is probably more suitable in this case (only matters for multi-monitor configs). – Alexei Levenkov Apr 28 '13 at 22:10
  • Hey, and thanks... I've tried adding the reference before but somehow it didn't work! I decided to give it one more shot and now it's working :/ Thanks for pushing me to try again! – DerpyNerd Apr 28 '13 at 22:11
1

Here is what you need.

It's too late but i think it will useful for another :) The answer is using P/Invoke with SystemMetric.

You can get size of your primary screen without add reference to System.Windows.Forms.dll.

http://pinvoke.net/default.aspx/Enums.SystemMetric

I don't like import a big dll to using one simple method XD

Thinh Vu
  • 3,036
  • 1
  • 12
  • 8
  • 1
    I'm not that sure, doesn't importing a DLL for one function impact performance even more than referencing a DLL from your application once? I mean, referencing a DLL that's already part of the .NET framework doesn't make your application more bulky than importing a DLL each time your require a function. Am I missing something? – DerpyNerd Mar 23 '16 at 07:45
  • In fact, there are many .NET method using P/invoke to call win 32 api function. You should access referencesource.microsoft.com to see that. So if you don't want to import .net dll, you can invoke win32 api directly. In this case, you just need to create SystemMetric enum with one or two value to get screen size. Then call to GetSystemMetrics function. – Thinh Vu Jul 09 '16 at 03:24
  • A P/invoke call is always a performance hit compared to a .Net library call. – Chase Dec 18 '20 at 16:38