2012年3月28日 星期三

[VC(Visual C++) ]- 修改顯示器解析度

[VC(Visual C++) ]- 修改顯示器解析度


本篇分享修改顯示器解析度範例有興趣的(C/P)同好歡迎來http://filemarkets.com/file/jashliao/c8fa8f5d/索取因為我不會上傳檔案分享哈哈 ^ ^












主要程式碼



001    // ChangeDisplayDlg.cpp : implementation file                

002    //                

003                    

004    #include "stdafx.h"                

005    #include "ChangeDisplay.h"                

006    #include "ChangeDisplayDlg.h"                

007                    

008    #ifdef _DEBUG                

009    #definenew DEBUG_NEW                

010    #undef THIS_FILE                

011    staticchar THIS_FILE[] = __FILE__;                

012    #endif                

013                    

014    /////////////////////////////////////////////////////////////////////////////                

015    // CAboutDlg dialog used for App About                

016                    

017    class CAboutDlg : public CDialog                

018    {                

019    public:                

020        CAboutDlg();            

021                    

022    // Dialog Data                

023        //{{AFX_DATA(CAboutDlg)            

024        enum { IDD = IDD_ABOUTBOX };            

025        //}}AFX_DATA            

026                    

027        // ClassWizard generated virtual function overrides            

028        //{{AFX_VIRTUAL(CAboutDlg)            

029        protected:            

030        virtualvoid DoDataExchange(CDataExchange* pDX);    // DDX/DDV support            

031        //}}AFX_VIRTUAL            

032                    

033    // Implementation                

034    protected:                

035        //{{AFX_MSG(CAboutDlg)            

036        //}}AFX_MSG            

037        DECLARE_MESSAGE_MAP()            

038    };                

039                    

040    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)                

041    {                

042        //{{AFX_DATA_INIT(CAboutDlg)            

043        //}}AFX_DATA_INIT            

044    }                

045                    

046    void CAboutDlg::DoDataExchange(CDataExchange* pDX)                

047    {                

048        CDialog::DoDataExchange(pDX);            

049        //{{AFX_DATA_MAP(CAboutDlg)            

050        //}}AFX_DATA_MAP            

051    }                

052                    

053    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)                

054        //{{AFX_MSG_MAP(CAboutDlg)            

055            // No message handlers        

056        //}}AFX_MSG_MAP            

057    END_MESSAGE_MAP()                

058                    

059    /////////////////////////////////////////////////////////////////////////////                

060    // CChangeDisplayDlg dialog                

061                    

062    CChangeDisplayDlg::CChangeDisplayDlg(CWnd* pParent /*=NULL*/)                

063        : CDialog(CChangeDisplayDlg::IDD, pParent)            

064    {                

065        //{{AFX_DATA_INIT(CChangeDisplayDlg)            

066        m_nWidthPixels = 1024;            

067        m_nHeightPixels = 768;            

068        m_nBitsPerPixel = 32;            

069        //}}AFX_DATA_INIT            

070        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32            

071        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);            

072    }                

073                    

074    void CChangeDisplayDlg::DoDataExchange(CDataExchange* pDX)                

075    {                

076        CDialog::DoDataExchange(pDX);            

077        //{{AFX_DATA_MAP(CChangeDisplayDlg)            

078        DDX_Text(pDX, IDC_EDIT1, m_nWidthPixels);            

079        DDX_Text(pDX, IDC_EDIT2, m_nHeightPixels);            

080        DDX_Text(pDX, IDC_EDIT3, m_nBitsPerPixel);            

081        //}}AFX_DATA_MAP            

082    }                

083                    

084    BEGIN_MESSAGE_MAP(CChangeDisplayDlg, CDialog)                

085        //{{AFX_MSG_MAP(CChangeDisplayDlg)            

086        ON_WM_SYSCOMMAND()            

087        ON_WM_PAINT()            

088        ON_WM_QUERYDRAGICON()            

089        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)            

090        //}}AFX_MSG_MAP            

091    END_MESSAGE_MAP()                

092                    

093    /////////////////////////////////////////////////////////////////////////////                

094    // CChangeDisplayDlg message handlers                

095                    

096    BOOL CChangeDisplayDlg::OnInitDialog()                

097    {                

098        CDialog::OnInitDialog();            

099                    

100        // Add "About..." menu item to system menu.            

101                    

102        // IDM_ABOUTBOX must be in the system command range.            

103        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);            

104        ASSERT(IDM_ABOUTBOX < 0xF000);            

105                    

106        CMenu* pSysMenu = GetSystemMenu(FALSE);            

107        if (pSysMenu != NULL)            

108        {            

109            CString strAboutMenu;        

110            strAboutMenu.LoadString(IDS_ABOUTBOX);        

111            if (!strAboutMenu.IsEmpty())        

112            {        

113                pSysMenu->AppendMenu(MF_SEPARATOR);    

114                pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);    

115            }        

116        }            

117                    

118        // Set the icon for this dialog.  The framework does this automatically            

119        //  when the application's main window is not a dialog            

120        SetIcon(m_hIcon, TRUE);            // Set big icon

121        SetIcon(m_hIcon, FALSE);        // Set small icon    

122                    

123        // TODO: Add extra initialization here            

124                    

125        return TRUE;  // return TRUE  unless you set the focus to a control            

126    }                

127                    

128    void CChangeDisplayDlg::OnSysCommand(UINT nID, LPARAM lParam)                

129    {                

130        if ((nID & 0xFFF0) == IDM_ABOUTBOX)            

131        {            

132            CAboutDlg dlgAbout;        

133            dlgAbout.DoModal();        

134        }            

135        else            

136        {            

137            CDialog::OnSysCommand(nID, lParam);        

138        }            

139    }                

140                    

141    // If you add a minimize button to your dialog, you will need the code below                

142    //  to draw the icon.  For MFC applications using the document/view model,                

143    //  this is automatically done for you by the framework.                

144                    

145    void CChangeDisplayDlg::OnPaint()                 

146    {                

147        if (IsIconic())            

148        {            

149            CPaintDC dc(this); // device context for painting        

150                    

151            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);        

152                    

153            // Center icon in client rectangle        

154            int cxIcon = GetSystemMetrics(SM_CXICON);        

155            int cyIcon = GetSystemMetrics(SM_CYICON);        

156            CRect rect;        

157            GetClientRect(&rect);        

158            int x = (rect.Width() - cxIcon + 1) / 2;        

159            int y = (rect.Height() - cyIcon + 1) / 2;        

160                    

161            // Draw the icon        

162            dc.DrawIcon(x, y, m_hIcon);        

163        }            

164        else            

165        {            

166            CDialog::OnPaint();        

167        }            

168    }                

169                    

170    // The system calls this to obtain the cursor to display while the user drags                

171    //  the minimized window.                

172    HCURSOR CChangeDisplayDlg::OnQueryDragIcon()                

173    {                

174        return (HCURSOR) m_hIcon;            

175    }                

176                    

177    void CChangeDisplayDlg::OnButton1()                 

178    {                

179        // TODO: Add your control notification handler code here             

180        UpdateData(TRUE);            

181        DEVMODE lpDevMode;            

182        lpDevMode.dmBitsPerPel = m_nBitsPerPixel;            

183        lpDevMode.dmPelsWidth = m_nWidthPixels;            

184        lpDevMode.dmPelsHeight = m_nHeightPixels;            

185        lpDevMode.dmSize = sizeof(lpDevMode);            

186        lpDevMode.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL;            

187        LONG result = ChangeDisplaySettings(&lpDevMode,0);            

188        if(result == DISP_CHANGE_SUCCESSFUL)            

189        {            

190            MessageBox("修改成功");        

191            ChangeDisplaySettings(&lpDevMode,CDS_UPDATEREGISTRY);        

192            //使用CDS_UPDATEREGISTRY表示次修改是持久的,        

193            //並在註冊表中寫入了相關的資料         

194        }            

195        else            

196        {            

197            MessageBox("修改失敗,恢復原有設置");        

198            ChangeDisplaySettings(NULL,0);        

199        }            

200    }                

 



 


沒有留言:

張貼留言