VisualStudio 93

[C#] Struct를 byte로 변환, byte를 Struct로 변환

static byte[] StructToByte(object obj) { //구조체 사이즈 int size = Marshal.SizeOf(obj); //사이즈 만큼 메모리 할당 받기 byte[] buffer = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); //구조체 주소값 가져오기 Marshal.StructureToPtr(obj, ptr, false); //메모리 복사 Marshal.Copy(ptr, buffer, 0, size); Marshal.FreeHGlobal(ptr); return buffer; } public static byte[] StructToByte(T structure) where T : struct { byte[] buffer..

VisualStudio/C# 2023.02.02

[WPF] Win32 api 사용하여 윈도우 창 제어하기 max창 드레그 및 축소화

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); private void pnlControlBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { WindowInteropHelper helper = new WindowInteropHelper(this); SendMessage(helper.Handle, 161, 2, 0); //DragM..

VisualStudio/WPF 2023.01.09

[WPF] 바인딩(onetime, oneway, twoway, onewaytosource)

C#에서 인터페이스에 나타나는 값을 동적으로 표현할때 데이터바인딩을 사용한다. XAML의 내용이 이렇게 선언하면 TEXTBOX안에 표시되는 내용이 ViewModel이라는 뷰모델 클래스의 name이라는 요소와 연결되게 된다. 여기서 설정하는 MODE는 onetime, oneway, twoway, onewaytosource 중에 하나로 설정하는데, -onetime 만약에 현재 뷰모델의 name이라는 요소가 string타입의 "사과"라고 저장되어 있다고 하자. 만약 MODE를 onetime으로 연결 한다면, 인터페이스의 텍스트블록 안에 "사과"라는 글자가 뜨게 된다. 하지만, 이때 단 한번만 바인딩이 되고 이후로는 연결이 끊어지게 되서 뷰모델 안의 string name을 바꿔도 텍스트 박스의 값이 영향을 받지..

VisualStudio/WPF 2023.01.09

[WPF] 사용자 정의컨트롤 디펜던시프로퍼티(DependencyProperty)

디펜던시프로퍼티 사용자 정의컨트롤을 만들어 사용 할 때 사용자 정의 컨트롤의 속성을 접근하여 get set을 하기 위한 프로퍼티 이다. 사용방법 정의컨트롤 생성 디펜던시 프로퍼티 생성 방법 PROPDP + Tab + Tab 하면 나오는 초기 디펜던시 프로퍼티 public int MyProperty { get { return (int)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static re..

VisualStudio/WPF 2023.01.05

[C#] Action.Invoke() vs Action() 차이

action.Invoke는 action?.Invoke와 같이 action 콜백이 null인지 검사하고 null이 아닐 때에만 Invoke 되도록 사용하고자 할 때 많이 사용한다. 기존 예외처리 if(_action !=null) _action.Invoke(3);// _action(3) 과 같은 코드 ? 키워드를 사용 한 예외처리 _action?.Invoke(3); //if(_action !=null)를 ?키워드를 사용하여 null임을 체크할 수 있다. action(); 는 action.Invoke()와 컴파일과정이 100프로 동일 하다. action이 null이면 NullReferenceException을 뱉기 때문에 위험하다 그래서 invoke를 사용하기 전에 null 검사를 해야 하며 기존 예외처리 처..

VisualStudio/C# 2023.01.05

[C#] nameof()와 default(T)

nameofnameof는 변수, 형식, 또는 멤버의 이름을 문자열로 반환합니다. 이는 하드코딩된 문자열을 대체하여 유지보수성을 높이는 데 유용합니다.컴파일 시 이름을 확인하므로, 오타나 잘못된 참조를 방지할 수 있습니다.코드 리팩토링 시 변수 이름이 변경되면, nameof도 자동으로 업데이트됩니다.주로 로깅, 예외 메시지, 또는 속성 이름을 출력하는 데 사용됩니다.using System;namespace NameofExample{ class Program { // 속성 정의 public string NameOfTest { get; set; } = "Initial Value"; static void Main(string[] args) { ..

VisualStudio/C# 2023.01.05