Narayanan Dayalan's Blog

Each problem that I solved became a rule which served afterwards to solve other problems


Leave a comment

Get Screen Resolution

Sometimes you may need to get your current screen resolution or even your current working Area. .Net made so easy to get screen resolution in just one line of code. The below sample stimulates the scenario

C#

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Screen.PrimaryScreen.Bounds.Width + " x " + Screen.PrimaryScreen.Bounds.Height);
            //The below code gets Working area of screen, excluding taskbar, docked windows and doc panels
            MessageBox.Show(Screen.PrimaryScreen.WorkingArea.Width + " x " + Screen.PrimaryScreen.WorkingArea.Height);
        }

VB.Net

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(Screen.PrimaryScreen.Bounds.Width.ToString() + " x " + Screen.PrimaryScreen.Bounds.Height.ToString())
        'The below code gets Working area of screen, excluding taskbar, docked windows and doc panels
        MessageBox.Show(Screen.PrimaryScreen.WorkingArea.Width.ToString() + " x " + Screen.PrimaryScreen.Bounds.WorkingArea.ToString())
    End Sub


Leave a comment

Disable Close Button From Win Forms

This example shows us how to disable “Close” button from Window Forms. Here i have used IntropServices to accomplish this. The name itself says that we are going to use IntropServices like COM and other platform invoke services. Yes, of course, we are going to make use of UnManaged code in this session.

C#

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        const int MF_BYPOSITION = 0x400;

        [DllImport("User32")]
        private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

        [DllImport("User32")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("User32")]
        private static extern int GetMenuItemCount(IntPtr hWnd);

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            IntPtr hSysMenu = GetSystemMenu(this.Handle, false);
            int iMenuCount = GetMenuItemCount(hSysMenu);

            // the line will remove "Close" - System Menu
            RemoveMenu(hSysMenu, iMenuCount - 1, MF_BYPOSITION);
        }
    }
}

VB.NET


Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class Form2
    Const MF_BYPOSITION As Integer = &H400

    <DllImport("User32")> _
    Private Shared Function RemoveMenu(ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    End Function

    <DllImport("User32")> _
    Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
    End Function

    <DllImport("User32")> _
    Private Shared Function GetMenuItemCount(ByVal hWnd As IntPtr) As Integer
    End Function

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim hSysMenu As IntPtr = GetSystemMenu(Me.Handle, False)
        Dim iMenuCount As Integer = GetMenuItemCount(hSysMenu)

        ' the line will remove "Close" - System Menu
        RemoveMenu(hSysMenu, iMenuCount - 1, MF_BYPOSITION)
    End Sub
End Class
Output

Output