반응형

폼을 숨기고 프로그램을 시작해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            timer1.Enabled = true;
            timer1.Interval = 5000;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // 화면에 보이지 않는 폼 만들기 1
            //Opacity = 0;
            //ShowInTaskbar = false;
 
 
            // 화면에 보이지 않는 폼 만들기 2
            // 아래 두 명령어의 순서를 바꾸면 최소화된 윈도우의
            // 타이틀바가 화면에 남는다.
            WindowState = FormWindowState.Minimized;
            ShowInTaskbar = false;            
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            // 화면에 보이지 않으니 5초 후 자동 종료
            Close();
        }
    }
}
 

 

간단히 두 가지 방법으로 프로그램 시작 시 폼을 숨길 수 있다.

두 번째 방법은 명령 순서에 주의 한다.

 

반응형
Posted by J-sean
:
반응형

버튼이나 다른 컨트롤이 있는 폼에서 특정 키를 눌렀을때 원하는 동작을 하게 하기 위해 Form KeyDown 이벤트 핸들러를 작성해도 포커스를 갖고 있는 컨트롤이 먼저 키 이벤트를 처리하기 때문에 원하는 동작을 처리할 수 없다.

 

폼이 먼저 이벤트를 받기 위해서는 KeyPreview 프로퍼티를 true로 설정해야 한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            //button1.TabStop = false;
            //button2.TabStop = false;
            // TabStop = false로 하면 처음엔 제대로 작동하는거 같지만
            // 버튼을 한 번 클릭하고 나서는 버튼이 계속 포커스를 갖기
            // 때문에 제대로 작동하지 않는다.
 
            // Gets or sets a value indicating whether the form will
            // receive key events before the event is passed to the
            // control that has focus.
            // true if the form will receive all key events;
            // false if the currently selected control on the form
            // receives key events. The default is false.
            // 키가 눌렸을때 포커스를 갖고 있는 컨트롤보다 폼이 먼저
            // 키 이벤트를 받을 수 있게 한다.
            KeyPreview = true;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button1");
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button2");
        }
 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    MessageBox.Show("F1");
                    break;
 
                default:
                    break;
            }
        }
    }
}
 

 

버튼이 포커스를 갖고 있는 상태에서 F1키를 눌러도 폼이 KeyDown 이벤트를 먼저 처리한다.

 

반응형
Posted by J-sean
:
반응형

폼 디자이너 레이아웃 모드를 그리드(Grid)로 바꿔보자.

 

기본 폼 디자이너 레이아웃 모드(SnapLines)는 그리드가 없어 컨트롤 배치가 불편하다.

 

Tools - Options... 선택

 

Windows Forms Designer - General - Layout Mode - SnapToGrid 선택

옵션을 바꿔도 폼 디자이너에 바로 적용되지 않는다. 폼 디자이너 윈도우를 종료하고 다시 불러온다.

 

폼 디자이너에 그리드가 생겼다.

 

반응형
Posted by J-sean
: