VB 改变分辨率
展开全部
找到一个C#的,先贴上来.vb的在下面...
vb代码中的提示:“需要重启”是极少数的情况,可以不用理它 。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 解决分辨率
{
public partial class Form1 : Form
{
//保存当前屏幕分辨率
int i = Screen.PrimaryScreen.Bounds.Width;
int j = Screen.PrimaryScreen.Bounds.Height;
public Form1()
{InitializeComponent();}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);
void ChangeRes()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = 1024;//宽
DevM.dmPelsHeight = 768;//高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
void FuYuan()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = i;//恢复宽
DevM.dmPelsHeight =j;//恢复高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
ChangeRes();
}
private void button1_Click(object sender, EventArgs e)
{
FuYuan();
this.Close();
}
=========================================================
vb的
'粘到代码编辑器里,啥控件都不用。。
'vb 改变屏幕分辨率.
'因为不断地调试,有些API或者Const调用可能不需要,麻烦lz删除.vb6.0 sp6 + windows xp sp3 测试通过
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (lpszDeviceName As String, iModeNum As Long, ByRef lpDevMode As DEVMODE) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_DISPLAYCHANGE = &H7E
Const HWND_BROADCAST = &HFFFF&
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_BITSPERPEL = &H40000
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const CDS_UPDATEREGISTRY = &H1
Const CDS_TEST = &H4
Const DISP_CHANGE_SUCCESSFUL = 0
Const DISP_CHANGE_RESTART = 1
Const BITSPIXEL = 12
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Dim ScreenWidth As Long, ScreenHeight As Long
Private Sub Form_Load()
ScreenWidth = Screen.Width / Screen.TwipsPerPixelX
ScreenHeight = Screen.Height / Screen.TwipsPerPixelY
Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
'Get the info into DevM
erg = EnumDisplaySettings(0&, 0&, DevM)
'This is what we 're going to change
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = 800 'ScreenWidth
DevM.dmPelsHeight = 600 'ScreenHeight
DevM.dmBitsPerPel = 32 '(can be 8, 16, 24, 32 or even 4)
erg = ChangeDisplaySettings(DevM, CDS_TEST)
'Check if succesfull
Select Case erg&
Case DISP_CHANGE_RESTART
an = MsgBox("需要重新启动来应用分辨率更改.请手动重启.", vbOKOnly + vbSystemModal, "提示")
End Select
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
erg = EnumDisplaySettings(0&, 0&, DevM)
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = ScreenWidth
DevM.dmPelsHeight = ScreenHeight
DevM.dmBitsPerPel = 32
erg = ChangeDisplaySettings(DevM, CDS_TEST)
Select Case erg&
Case DISP_CHANGE_RESTART
an = MsgBox("需要重新启动来应用分辨率更改.请手动重启.", vbOKOnly + vbSystemModal, "提示")
End Select
End Sub
vb代码中的提示:“需要重启”是极少数的情况,可以不用理它 。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 解决分辨率
{
public partial class Form1 : Form
{
//保存当前屏幕分辨率
int i = Screen.PrimaryScreen.Bounds.Width;
int j = Screen.PrimaryScreen.Bounds.Height;
public Form1()
{InitializeComponent();}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);
void ChangeRes()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = 1024;//宽
DevM.dmPelsHeight = 768;//高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
void FuYuan()
{
DEVMODE DevM = new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref DevM);
DevM.dmPelsWidth = i;//恢复宽
DevM.dmPelsHeight =j;//恢复高
DevM.dmDisplayFrequency = 60;//刷新频率
DevM.dmBitsPerPel = 32;//颜色象素
long result = ChangeDisplaySettings(ref DevM, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
ChangeRes();
}
private void button1_Click(object sender, EventArgs e)
{
FuYuan();
this.Close();
}
=========================================================
vb的
'粘到代码编辑器里,啥控件都不用。。
'vb 改变屏幕分辨率.
'因为不断地调试,有些API或者Const调用可能不需要,麻烦lz删除.vb6.0 sp6 + windows xp sp3 测试通过
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (lpszDeviceName As String, iModeNum As Long, ByRef lpDevMode As DEVMODE) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_DISPLAYCHANGE = &H7E
Const HWND_BROADCAST = &HFFFF&
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_BITSPERPEL = &H40000
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const CDS_UPDATEREGISTRY = &H1
Const CDS_TEST = &H4
Const DISP_CHANGE_SUCCESSFUL = 0
Const DISP_CHANGE_RESTART = 1
Const BITSPIXEL = 12
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Dim ScreenWidth As Long, ScreenHeight As Long
Private Sub Form_Load()
ScreenWidth = Screen.Width / Screen.TwipsPerPixelX
ScreenHeight = Screen.Height / Screen.TwipsPerPixelY
Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
'Get the info into DevM
erg = EnumDisplaySettings(0&, 0&, DevM)
'This is what we 're going to change
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = 800 'ScreenWidth
DevM.dmPelsHeight = 600 'ScreenHeight
DevM.dmBitsPerPel = 32 '(can be 8, 16, 24, 32 or even 4)
erg = ChangeDisplaySettings(DevM, CDS_TEST)
'Check if succesfull
Select Case erg&
Case DISP_CHANGE_RESTART
an = MsgBox("需要重新启动来应用分辨率更改.请手动重启.", vbOKOnly + vbSystemModal, "提示")
End Select
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
erg = EnumDisplaySettings(0&, 0&, DevM)
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = ScreenWidth
DevM.dmPelsHeight = ScreenHeight
DevM.dmBitsPerPel = 32
erg = ChangeDisplaySettings(DevM, CDS_TEST)
Select Case erg&
Case DISP_CHANGE_RESTART
an = MsgBox("需要重新启动来应用分辨率更改.请手动重启.", vbOKOnly + vbSystemModal, "提示")
End Select
End Sub
参考资料: 希望可以帮到你.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询