CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    May 2010
    Posts
    5

    Program to turn off monitor

    My requirement is to create a small application which can turn off the monitor when I press the screen lock key combination (Windows +L). This application should also turn off the machine when the screen saver is activated. It should activate the monitor again when we press any key or when I move my mouse. I am a newbie into the coding and I dont know how to do this. If you could help me to progress, it will be really helpful.

    I can deploy this application to 3 of My PCs and can turn off monitors to save power.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program to turn off monitor

    Turn on the Power Saving features of Windows. It will blank the monitor after x seconds, and put the pc to sleeep after x seconds

    Unless you want to re-write GINA, but I think Microsoft did a good job.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    May 2010
    Posts
    5

    Re: Program to turn off monitor

    Please help me.. Turning on the power settings may not be a solution for me. I would like to power off the monitor when I press the lock key combination (Windows +L)

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program to turn off monitor

    I don't know of any way to do that. Can you click anything to shut off the power to the monitor? I don't think so. If you did, how would you turn it back on?

    Plus, if we posted code to do so, people would be doing harm to others!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    May 2010
    Posts
    5

    Re: Program to turn off monitor

    Its really simple.. The below C# code will switch off the monitor when I click the Button in the form. And when I move my mouse or press any key, it will turn on the monitor again. But I want to make it in such a way that it should run as windows service and should turn off the monitor when the machine is locked. That all. And I am not sure how it is going to harm others.

    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 WindowsFormsApplication1
    {

    public partial class Form1 : Form
    {
    const uint SC_MONITORPOWER = 0xF170;
    const uint WM_SYSCOMMAND = 0x0112;
    const uint MONITOR_ON = 0x0001;
    const uint MONITOR_OFF = 0x0002;

    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

    public Form1()
    {
    InitializeComponent();
    }



    private void button1_Click_1(object sender, EventArgs e)
    {

    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

    }


    }
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program to turn off monitor

    Something like this? It still doesn't hook into windows security system (GINA)

    Code:
    Option Explicit
    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
    Private Const EM_GETLINECOUNT = &HBA
    
    
    Private Sub Text1_Change()
    
        Text1.Height = Me.TextHeight("A") * SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
    
    
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Program to turn off monitor

    It does not work SendMessage returns zero on using the monitor command.
    At least at my system. Maybe not all monitors support this shutting off.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Program to turn off monitor

    VB6 does not do windows services so you are barking up the wrong tree in this section of the forum.

    You would need to use VB.Net, C# or C++ to create the service.

    First step of course would be to create a windows service project. It is possible that there may be a hook in there somewhere that would allow you to tell when the system was locked. I honestly have never looked to see if such a thing exists.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program to turn off monitor

    not likely. people have tried to hack GINA for decades
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Program to turn off monitor

    Didn't even know GINA exists. What is she like?

    The project was not necessarily said to be a service. If the above function call would have worked, a standard application should do the trick.

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Program to turn off monitor

    Quote Originally Posted by WoF View Post
    The project was not necessarily said to be a service. If the above function call would have worked, a standard application should do the trick.
    Actually he said he wanted it to be a service, hence my previous response.

    Quote Originally Posted by arbrahul
    But I want to make it in such a way that it should run as windows service and should turn off the monitor when the machine is locked.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Apr 2009
    Posts
    394

    Re: Program to turn off monitor

    >VB6 does not do windows services so you are barking up the wrong tree in this section of the forum.

    Well Mr. Miser, here you are wrong as VB6.0 CAN DO SERVICES! Albiet not very well but it can.

    http://www.smsoft.ru/en/ntservice.htm

    http://www.vbrad.com/article.aspx?id=92

    http://articles.techrepublic.com.com...1-1049775.html

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Program to turn off monitor

    Really? I had looked into this a while back and could find nothing on it.

    I will look into the links you have provided. Thanks.
    Always use [code][/code] tags when posting code.

  14. #14
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Program to turn off monitor

    @DataMiser: You were right. I only saw:
    "My requirement is to create a small application which can turn off the monitor ..."

    Nevertheless, I suppose SendMessage would not work in a service either, if it refuses already in a usual application.

  15. #15
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program to turn off monitor

    Quote Originally Posted by WoF View Post
    Didn't even know GINA exists. What is she like?
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Debugging:

    WLX_SAS_ACTION_NONE
    WLX_SAS_ACTION_LOCK_WKSTA
    WLX_SAS_ACTION_LOGOFF
    WLX_SAS_ACTION_PWD_CHANGE
    WLX_SAS_ACTION_TASKLIST
    WLX_SAS_ACTION_SHUTDOWN_REBOOT
    WLX_SAS_ACTION_SHUTDOWN
    WLX_SAS_ACTION_SHUTDOWN_SLEEP
    WLX_SAS_ACTION_SHUTDOWN_HIBERNATE
    WLX_SAS_ACTION_SHUTDOWN_POWER_OFF
    Last edited by dglienna; May 3rd, 2010 at 11:39 AM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured