첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ Button 클래스에서 권한 상승 필요 표시 버튼을 사용하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ CustomButton.cs

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 커스텀 버튼
    /// </summary>
    public class CustomButton : Button
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter)

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandleRef">윈도우 핸들 참조</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32")]
        private static extern IntPtr SendMessage(HandleRef windowHandleRef, uint message, IntPtr wordParameter, IntPtr longParameter);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// BCM_SETSHIELD
        /// </summary>
        private uint BCM_SETSHIELD = 0x0000160c;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - CustomButton()

        /// <summary>
        /// 생성자
        /// </summary>
        public CustomButton()
        {
            FlatStyle = FlatStyle.System;

            if(!IsAdministratorRole())
            {
                ShowShield();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 관리자 역할 여부 구하기 - IsAdministratorRole()

        /// <summary>
        /// 관리자 역할 여부 구하기
        /// </summary>
        /// <returns>관리자 역할 여부</returns>
        private bool IsAdministratorRole()
        {
            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);

            return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        #endregion
        #region 방패 표시하기 - ShowShield()

        /// <summary>
        /// 방패 표시하기
        /// </summary>
        private void ShowShield()
        {
            IntPtr wordParameter = new IntPtr(0);
            IntPtr longParamerer = new IntPtr(1);

            SendMessage(new HandleRef(this, Handle), BCM_SETSHIELD, wordParameter, longParamerer);
        }

        #endregion
    }
}

 

▶ MainForm.cs

using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        #endregion
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,