사용자 도구

사이트 도구

English

cuwin:화면전환_폼전환하는_방법:index

CUWIN 화면전환, 폼전환

일반적으로 하나의 시스템은 여러개의 화면으로 구성되어 있는 경우가 많습니다. 하나의 폼에서 다른 폼으로 넘어가는 방법에 대해서 설하겠습니다.

위 동영상을 보면, 화면 중앙에 일종의 에니메이션 효과가 보일 겁니다. 새로운 윈도우 폼이 열리는 과정을 멋있게 표현하려고, OS에서 이렇게 한것 같습니다만, 자동화 현장에서는 구지 필요없는 액션입니다. 그래서 이것을 지워보도록 하겠습니다.

아래 동영상을 보면, 지저분한(?) 에니메이션 없이 깔끔하게 다음 폼으로 전환되는 것을 보실 수 있습니다.

프로그램 시작부분 소스입니다. DisableAnimation 메소드가 바로 에니메이션 효과를 없애주는 코드입니다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;
 
namespace SwitchForms
{
    static class Program
    {
        public static readonly Form1 theForm1 = new Form1();
        public static readonly Form2 theForm2 = new Form2();
 
        [DllImport("Coredll.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
 
        [DllImport("coredll.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
 
        static void DisableAnimation(Form form)
        {
            const int WS_EX_NOANIMATION = 0x04000000;
            const int GWL_EX_STYLE = -20;
 
            int style = GetWindowLong(form.Handle, GWL_EX_STYLE);
            style |= WS_EX_NOANIMATION;
            SetWindowLong(form.Handle, GWL_EX_STYLE, style);
        }
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            DisableAnimation(theForm1);
            DisableAnimation(theForm2);
 
            Application.Run(theForm1);
        }
    }
}

위 소스중 이 부분을 눈여겨 보세요.

    public static readonly Form1 theForm1 = new Form1();
    public static readonly Form2 theForm2 = new Form2();

폼을 인스턴스화 하는 부분입니다. 이렇게 해야 보턴을 눌렀을때, 해당 폼을 Show하거나 Hide할 수 있습니다.

        private void button1_Click(object sender, EventArgs e)
        {
            Program.theForm2.Show();
            Program.theForm1.Hide();
        }

폼전환하는 부분의 소스입니다.

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace SwitchForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Program.theForm2.Show();
            Program.theForm1.Hide();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

소스 다운로드

한단계 뒤로

cuwin/화면전환_폼전환하는_방법/index.txt · 마지막으로 수정됨: 2017/11/25 02:11 저자 Comfile Technology