Knowledge Base Nr: 00139 switchdisplay.cpp - http://www.swe-kaiser.de

Downloads:

MFC: WIN32: Umschalten der Bildschirmauflösung

  
BOOL CSwitchDisplayDlg::OnInitDialog()
{
CDialog::OnInitDialog();

...

//get and set from commandline
CString str = CProcessSupport::GetCommandlineParam("mode=");
GetDlgItem(IDC_EDIT1)->SetWindowText(str);

if (!str.IsEmpty())
{
SetTimer(0, 100, NULL); //muss über timer sein da sonst getmessage() in OnButton2() hängt
}

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

#define KEYSPEED 10

void CSwitchDisplayDlg::OnButton1()
{
int nErr = CProcessSupport::SendKeysToWindow("Shell_TrayWnd", NULL
, "CTRL+ESC#up#up#up#up#up#right#enter", KEYSPEED, 1000
, true, false); //thread
if (nErr>0)
{
while (1) //warten bis control panel aufgeschaltet
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) //control panel braucht die messageloop?!
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if (::FindWindow(NULL, "Control Panel") != 0)
break;
}

if (::FindWindow(NULL, "Control Panel") != 0)
break;

Sleep(10);
}
}
}

void CSwitchDisplayDlg::OnButton3()
{
int nErr = CProcessSupport::SendKeysToWindow( //"SysListView32", NULL //"Control Panel"
NULL, "Control Panel"
, "d#i#s#p#l#a#y#enter#", KEYSPEED, 10000
, true, false); //thread
if (nErr>0) //warten bis aufgeschaltet
{
while (1)
{
if (::FindWindow(NULL, "Display Properties") != 0)
break;

Sleep(10);
}
}
}

void CSwitchDisplayDlg::OnButton4()
{
CString str;

GetDlgItem(IDC_EDIT1)->GetWindowText(str);

int nMode = atoi(str);

str = "ctrl+shift+tab#tab#left#left#left#left#left#left#left#left#left#left#left#left#left#";
for (int n=0; n<nMode; n++)
str += "right#";

str += "enter";

int nErr = CProcessSupport::SendKeysToWindow(
NULL, "Display Properties"
, str, 20, 10000
, false, false); //thread

//nur wenn ein neuer modus eingestellt wurde messageboxen quittieren
Sleep(500);
if (FindWindow(NULL, "Display Properties") != 0)
{
str = "enter#tab#enter#";

int nErr = CProcessSupport::SendKeysToWindow(
NULL, "Display Properties"
, str, 20, 10000
, false, false); //thread
}
}

void CSwitchDisplayDlg::OnButton2()
{
HWND orgHwndNormal = 0;
HWND orgHwnd = 0;

//FALSCH: ist immer eigene app. vorgänger in wnd-liste holen!!!
//FALSCH: ist immer eigene app. vorgänger in wnd-liste holen!!!
orgHwndNormal = ::GetFocus();
if (!orgHwndNormal)
{
orgHwnd = ::GetForegroundWindow();
}
//FALSCH: ist immer eigene app. vorgänger in wnd-liste holen!!!
//FALSCH: ist immer eigene app. vorgänger in wnd-liste holen!!!

OnButton1();
OnButton3();
OnButton4();

//Control Panel schliessen
HWND hwnd = ::FindWindow(NULL, "Control Panel");
if (hwnd != 0)
{
int nErr = CProcessSupport::SendKeysToWindow(
NULL, "Control Panel"
, "alt+f4#", KEYSPEED, 10000
, false, false); //thread
}

if (orgHwndNormal)
::SetFocus(orgHwndNormal);
if (orgHwnd)
::SetForegroundWindow(orgHwnd);
}

void CSwitchDisplayDlg::OnTimer(UINT nIDEvent)
{
static bool s_bFirstTime = true; //verriegelung wichtig da messageloop in OnButton2()!
if (s_bFirstTime)
{
s_bFirstTime = false;
OnButton2(); //muss über timer sein da sonst getmessage() in OnButton2() hängt
PostQuitMessage(0);
}

CDialog::OnTimer(nIDEvent);
}