C#의 기본 WinForm에서 맨위에 테두리 창이 디자인 적으로 예쁘지 않기 때문에 해당 부분을 없애고 따로 창을 움직일 수 있는 이벤트를 주고자 한다.
MouseDown 이벤트 더블클릭
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int IParam);
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, 0x112, 0xf012, 0);
}
}
}
Form만 Load 이벤트 핸들러를 달아주고 나머지 박스들은 더블 클릭을 통해 자동으로 만들어진 껍질 사용
운영체제에 설치된 폰트 목록을 가져와 콤보박스에 넣는 함수
// Form1의 load 이벤트 핸들러
private void addFont(object sender, EventArgs e)
{
// 운영체제에 설치되어 있는 폰트 목록 가져오기
FontFamily[] fonts = FontFamily.Families;
// foreach 문을 통해 conboBox에 하나씩 넣기
foreach (FontFamily font in fonts)
comboFontBox.Items.Add(font.Name);
}
폰트 설정 변경에 공통으로 사용할 함수
void changeFont()
{
FontStyle style = FontStyle.Regular; // FontStyle 일반 스타일 설정
if(boldCheckBox.Checked)
style |= FontStyle.Bold; // Bold 효과 추가
if (italicCheckBox.Checked)
style |= FontStyle.Italic; // Italic 효과 추가
if (underlineCheckBox.Checked)
style |= FontStyle.Underline; // Underline 효과 추가
// 설정한 폰트로 바꾸기 Font("폰트 이름", int(사이즈), FontStyle);
textBox.Font = new Font((string)comboFontBox.SelectedItem, 12, style);
}
전체코드:
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 FontBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Form1의 load 이벤트 핸들러
private void addFont(object sender, EventArgs e)
{
// 운영체제에 설치되어 있는 폰트 목록 가져오기
FontFamily[] fonts = FontFamily.Families;
// foreach 문을 통해 conboBox에 하나씩 넣기
foreach (FontFamily font in fonts)
comboFontBox.Items.Add(font.Name);
}
// 폰트 변화에 공통적으로 사용할 함수
void changeFont()
{
FontStyle style = FontStyle.Regular; // FontStyle 일반 스타일 설정
if(boldCheckBox.Checked)
style |= FontStyle.Bold; // Bold 효과 추가
if (italicCheckBox.Checked)
style |= FontStyle.Italic; // Italic 효과 추가
if (underlineCheckBox.Checked)
style |= FontStyle.Underline; // Underline 효과 추가
// 설정한 폰트로 바꾸기 Font("폰트 이름", int(사이즈), FontStyle);
textBox.Font = new Font((string)comboFontBox.SelectedItem, 12, style);
}
// Bold 체크 박스 클릭 이벤트 핸들러
private void boldCheckBox_CheckedChanged(object sender, EventArgs e)
{
changeFont();
}
// Italic 체크 박스 클릭 이벤트 핸들러
private void italicCheckBox_CheckedChanged(object sender, EventArgs e)
{
changeFont();
}
// 콤보박스 클릭 이벤트 핸들러
private void comboFontBox_SelectedIndexChanged(object sender, EventArgs e)
{
changeFont();
}
// 밑줄 체크 박스 클릭 이벤트 핸들러
private void underlineCheckBox_CheckedChanged(object sender, EventArgs e)
{
changeFont();
}
}
}