采用ChangeDisplaySettings这个API函数就可以了。 参考 http://msdn2.microsoft.com/en-us/library/ms533260.aspx http://www.codeproject.com/csharp/CSDynamicScrRes.asp http://www.study888.com/computer/pro/cc/200512/128708.html
Windows GDI
ChangeDisplaySettings
The ChangeDisplaySettings function changes the settings of the default display device to the specified graphics mode. To change the settings of a specified display device, use the ChangeDisplaySettingsEx function. LONG ChangeDisplaySettings( LPDEVMODE lpDevMode, // graphics mode DWORD dwflags // graphics mode options ); Parameters
Return ValuesThe ChangeDisplaySettings function returns one of the following values.
RemarksTo ensure that the DEVMODE structure passed to ChangeDisplaySettings is valid and contains only values supported by the display driver, use the DEVMODE returned by the EnumDisplaySettings function. When the display mode is changed dynamically, the WM_DISPLAYCHANGE message is sent to all running applications with the following message parameters.
Windows 95/98/Me: If the calling thread has any windows, ChangeDisplaySettings sends them the WM_DISPLAYCHANGE message immediately (for windows in all other threads, the message is sent when the thread can receive nonqueued messages). This may cause the shell to get its message too soon and could squash icons. To avoid this problem, have ChangeDisplaySettings do resolution switching by calling on a thread with no windows, for example, a new thread. Windows 95/98/Me: ChangeDisplaySettingsW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems. Requirements Windows NT/2000/XP/Vista: Included in Windows NT 3.5 and later. See AlsoDevice Contexts Overview, Device Context Functions, ChangeDisplaySettingsEx, CreateDC, DEVMODE, EnumDisplayDevices, EnumDisplaySettings, WM_DISPLAYCHANGE IntroductionA common problem that some or all Windows developers are facing is changing the Screen Resolution dynamically. The resolution of the user screen is not necessarily the same as that of the development environment screen. Here what we will do is, in the requirement and installation note, we will mention that the user has to use xxxx * xxx resolution for better performance. Is this good?. If the end user does not have enough experience, what will happen? In this case, the user tries to open that product in lesser or greater resolution than the developed screen resolution and that will create some problems; problem in the sense that some portion of the window won't be visible to the user if he uses a lesser resolution or the window will become stretched if he uses a higher resolution. So here, you will get the ultimate solution for this sort of problem. Through this article I will explain how we can:
Getting the end user resolutionIn dotnet we can access the values of the user's screen resolution through the public static Screen PrimaryScreen {get;} This property is a read-only property which has a return of type Screen screen = Screen.PrimaryScreen; int S_width=screen.Bounds.Width; int S_height=screen.Bounds.Height; Note: You can see the implementation of this logic in the project file (see download file above). You can apply this logic to any event you want. Changing the resolution to our software/product compatible resolutionOur next goal is to set the screen resolution. Here you have to put in a little effort. In .NET there is no supported built-in class or classes for setting the resolution. You can achieve this through calling some Win32 API. Before continuing, better read something about COM Interop service in dotnet (early and late binding), attribute, DLL import attribute and Platform Invoke. I will explain the above technologies a little. I can't go into detail because here, my aim is something different. Using the
If you want to use this unmanaged code definition here in your managed environment what you will do? There comes the Note: You can see the implementation of this logic in the project file (see download file above). You can apply this logic to any event that you want. Here you are calling the Win32 API's User32.dll files. class User32 { [DllImport("user32.dll")] public static extern int EnumDisplaySettings ( string deviceName, int modeNum, ref DEVMODE devMode ); [DllImport("user32.dll")] public static extern int ChangeDisplaySettings( ref DEVMODE devMode, int flags); public const int ENUM_CURRENT_SETTINGS = -1; public const int CDS_UPDATEREGISTRY = 0x01; public const int CDS_TEST = 0x02; public const int DISP_CHANGE_SUCCESSFUL = 0; public const int DISP_CHANGE_RESTART = 1; public const int DISP_CHANGE_FAILED = -1; }
If you want to use the Win32 API functions then you have to give the full signature of the function that you are going to use in your managed environment. public static extern int EnumDisplaySettings (string deviceName, int modeNum, ref DEVMODE devMode); And the remaining lines are self explanatory. If you check the signature of both functions above, you can see
[StructLayout(LayoutKind.Sequential)] public struct DEVMODE { [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; ... } Note: You will get the full code block in project file that you can download at the top of this article. You can apply this logic to any event that you want. Special care needs to be taken to make sure that the types are the right size and that fixed length strings are appropriately defined. In this case The function DEVMODE dm = new DEVMODE(); dm.dmDeviceName = new String (new char[32]); dm.dmFormName = new String (new char[32]); dm.dmSize = (short)Marshal.SizeOf (dm); if (0 != User32.EnumDisplaySettings (null, User32.ENUM_CURRENT_SETTINGS, ref dm)) { At this point the dm.dmPelsWidth = iWidth; dm.dmPelsHeight = iHeight; int iRet = User32.ChangeDisplaySettings ( ref dm, User32.CDS_UPDATEREGISTRY); The code in the download does this a little differently to cope with various error conditions and to test that we can set the display mode before we actually do so. I would encourage you to look at the full source file and see what it does. That's all there is to it. Now we achieved our second goal. |
改变屏幕分辨率
最新推荐文章于 2022-10-02 00:14:30 发布