반응형

* VBNET IP Ping Check 예제...

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bCheck As Boolean
        Try
            bCheck = My.Computer.Network.Ping(TextBox1.Text)

            If bCheck Then
                MessageBox.Show("Ping Check Success...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Else
                MessageBox.Show("Ping Check Failed...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            bCheck = False
        End Try

    End Sub
End Class

 

*예제 결과

 

 

반응형
반응형

* 구조체를 Marshal 이용 바이트배열로 변환 하거나 바이트 배열화 된 구조체를 다시 원래 모습으로

   복귀 시키는 예제...

 

메인화면

전체 소스 코드

Form1.vb

 

Imports System.Runtime.InteropServices

Public Class Form1

    Structure t_TEST
        Public iTmp As Integer
        Public strTmp As String
        Public dbTmp As Double
        Public ftTmp As Single
    End Structure

    Dim tt As t_TEST = New t_TEST
    Dim bTmp() As Byte

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tt.dbTmp = 0.12321312313
        tt.iTmp = 123123
        tt.strTmp = "테스트1"
        tt.ftTmp = 0.123213F
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '구조체를 바이트 배열로...
        bTmp = StructToBytes(tt)
        label1.Text = bTmp.Length.ToString() + " bytes"
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        '바이트 배열을 다시 구조체로 변환...
        Dim tTmp As t_TEST = ByteToStruct(Of t_TEST)(bTmp)
        label2.Text = "int 값: " & tTmp.iTmp.ToString()
        label3.Text = "double 값: " & tTmp.dbTmp.ToString()
        label4.Text = "float 값: " & tTmp.ftTmp.ToString()
        label5.Text = "string 값: " & tTmp.strTmp
    End Sub

    Private Function StructToBytes(ByVal obj As Object) As Byte()

        '구조체 사이즈
        Dim iSize As Integer = Marshal.SizeOf(obj)

        '사이즈 만큼 메모리 할당
        Dim arr(iSize) As Byte
        Dim ptr As IntPtr = Marshal.AllocHGlobal(iSize)

        '구조체 주소값 가져오기
        Marshal.StructureToPtr(obj, ptr, False)
        '메모리 복사
        Marshal.Copy(ptr, arr, 0, iSize)
        '메모리 할당 받은거 해제
        Marshal.FreeHGlobal(ptr)

        Return arr

    End Function

    Private Function ByteToStruct(Of T As Structure)(ByVal buffer As Byte()) As T
        Dim size As Integer = Marshal.SizeOf(GetType(T))

        If size > buffer.Length Then
            Throw New Exception()
        End If

        Dim ptr As IntPtr = Marshal.AllocHGlobal(size)
        Marshal.Copy(buffer, 0, ptr, size)
        Dim obj As T = CType(Marshal.PtrToStructure(ptr, GetType(T)), T)
        Marshal.FreeHGlobal(ptr)
        Return obj
    End Function

End Class

 

* 실행한 결과...

 

결과화면

 

 

https://kdsoft-zeros.tistory.com/25

 

[C#] 구조체 를 바이트 배열로 또는 바이트 배열을 구조체로 변환

* 구조체를 Marshal 이용 바이트배열로 변환 하거나 바이트 배열화 된 구조체를 다시 원래 모습으로 복귀 시키는 예제... Form1.cs using System; using System.Collections.Generic; using System.ComponentMode..

kdsoft-zeros.tistory.com

 

반응형
반응형

InputMethodManager imm; 변수 선언

 

onCreate 함수 안에서 키보드 연결

imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

 

* 숨기기

* 보여지기

 

반응형
반응형

* VBNET API PC (종료, 재시작) 또는 Diagnostics.Process 이용 PC 종료 예제...

 

메인화면

 

전체 소스 코드

Form1.vb

 

Imports System.Runtime.InteropServices

Public Class Form1

    Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, _
                                                                                                ByVal lpMessage As String, _
                                                                                                ByVal dwTimeout As Integer, _
                                                                                                ByVal bForceAppsClosed As Integer, _
                                                                                                ByVal bRebootAfterShutdown As Integer) As Integer
    '1: 종료 전 사용자에게 알릴 메시지 , 2:종료 전 사용자에게 알릴 메시지, 3:종료까지 대기 시간, 4:프로그램 강제 종료 여부(False -> 강제 종료), 5:시스템 종료 후 다시 시작 여부(true -> 다시 시작)


    Dim bClick As Boolean = False
    Dim dtClick As DateTime

    Dim bThread As Boolean = True
    Dim thMain As System.Threading.Thread

    Dim bCheck As Boolean = False


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '크로스 스레드 오류 방지
        CheckForIllegalCrossThreadCalls = False

        thMain = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Thread_Tick))

        '1. 첫번째 방법 : 스레드
        thMain.IsBackground = True
        thMain.Start()

        '2 두번째 방법
        '10초 뒤 종료
        'System.Diagnostics.Process.Start("shutdown", "/s /f /t 10")
        'label2.Text = "10"
    End Sub


    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        dtClick = DateTime.Now
        bClick = True
    End Sub



    Private Function After_Time(ByVal dtNow As DateTime, ByVal dtBefore As DateTime) As Double
        Dim ts As TimeSpan = dtNow - dtBefore
        Return ts.TotalSeconds
    End Function

    Private Sub Thread_Tick()
        While (bThread)

            If bClick Then
                label2.Text = String.Format("{0:##0}", After_Time(DateTime.Now, dtClick))
				
                '5초 뒤에 PC 재시작
                '마지막인자 True 이면 재시작 False 이면 종료
                If After_Time(Date.Now, dtClick) >= 5 And Not bCheck Then
                    InitiateSystemShutdown("\\127.0.0.1", Nothing, 0, False, True)
                    bCheck = True
                End If

            End If

            System.Threading.Thread.Sleep(100)
        End While

    End Sub

    Private Sub Form1_FontChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FontChanged
        '스레드 변수가 nothing 이 아니면
        If Not thMain Is Nothing Then
            '스레드가 돌아가고 있으면.
            If thMain.IsAlive Then
                '스레드 강제 종료
                thMain.Abort()
            End If
        End If


    End Sub
End Class

 

* 두번째 방법으로는 아래와 같이 Diagnostics.Process 를 이용 PC 종료 예제 입니다.

/*
피시 강제 종료
System.Diagnostics.Process.Start("shutdown.exe", "-s -f /t 60")      //-t 초 즉 60초 뒤에 PC 종료...
피시 종료 카운트다운 때 아래 명령을 날리면 종료가 취소됨
System.Diagnostics.Process.Start("shutdown.exe", "-a")
피시 재시작
System.Diagnostics.Process.Start("shutdown.exe", "-r")
피시 로그오프
System.Diagnostics.Process.Start("shutdown.exe", "-l")
*/

 

 

*예제 결과

 

 

 

https://kdsoft-zeros.tistory.com/6

 

[VBNET] 시간 체크 함수 (Time Check Func)

Public Function After_Time(ByVal NowTime As Date, ByVal BeforeTime As Date) As Double Dim ts As TimeSpan = NowTime - BeforeTime Return ts.TotalSeconds End Function 반환 되는 값 1 은 1초 이며 0.5 초..

kdsoft-zeros.tistory.com

 

반응형
반응형

match_parent 와 wrap_content 차이


match_parent: 
부모가 가지는 길이를 모두 채울 때 사용. 해당 레이아웃을 취하는 컨테이너의 길이를 모두 채우는 것 


wrap_content :
해당 뷰가 그려질 수 있게 필요한 길이만 사용. 이 경우 절대적인 값도 넣을 수 있는데, 10px, 10dp, 10sp 처럼 수치와 단위를 써서 직접 길이 값을 지정하면 됩니다.

반응형
반응형

* C# API 를 이용 PC 종료 시키기 예제...

 

메인화면

전체 소스코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;


namespace CSharp_PCExit
{
    public partial class Form1 : Form
    {

        [DllImport("advapi32.dll")]
        public static extern void InitiateSystemShutdown(string lpMachineName,        //컴퓨터 이름 \\\\127.0.0.1
                                                         string lpMessage,            //종료 전 사용자에게 알릴 메시지
                                                         int dwTimeout,               //종료까지 대기 시간
                                                         bool bForceAppsClosed,       //프로그램 강제 종료 여부(False -> 강제 종료)
                                                         bool bRebootAfterShutdown);  //시스템 종료 후 다시 시작 여부(true -> 다시 시작)



        bool bClick = false;
        DateTime dtClick;

        bool bThread = true;
        System.Threading.Thread thMain;

        bool bCheck = false;

        public Form1()
        {
            InitializeComponent();

            //크로스 스레드 오류 방지
            CheckForIllegalCrossThreadCalls = false;

            thMain = new System.Threading.Thread(new System.Threading.ThreadStart(Thread_Tick));

            //1. 첫번째 방법 : 스레드
            thMain.IsBackground = true;
            thMain.Start();

            //2 두번째 방법
            //10초 뒤 종료
            //System.Diagnostics.Process.Start("shutdown", "/s /f /t 10");
            //label2.Text = "10";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dtClick = DateTime.Now;
            bClick = true;
        }

        private double After_Time(DateTime dtNow, DateTime dtBefore)
        {
            TimeSpan ts = dtNow - dtBefore;
            return ts.TotalSeconds;
        }

        //스레드 이벤트...
        void Thread_Tick()
        {
            while (bThread)
            {
                if (bClick)
                {
                    label2.Text = string.Format("{0:##0}", After_Time(DateTime.Now, dtClick));

                    if (After_Time(DateTime.Now, dtClick) >= 5 && !bCheck)
                    {
                        InitiateSystemShutdown("\\\\127.0.0.1" , null, 0, false, false );
                        bCheck = true;
                    }

                }


                System.Threading.Thread.Sleep(100);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (thMain != null)
            {
                if (thMain.IsAlive)
                {
                    thMain.Abort();
                }
                thMain = null;
            }
        }

    }
}

 

위 그림에서 보듯이 5초 후 PC 가 종료 되게끔 구현이 되어 있습니다. 5초는 임의로 정한 숫자일 뿐이고

나중에 사용자가 설정을 할 수 있게 하게 되면 얼마든지 시간 설정하여 자유롭게 PC 종료 하는 프로그램을

만들 수 있습니다. 

 

* 두번째 방법으로는 아래와 같이 Diagnostics.Process 를 이용 PC 종료 예제 입니다.

/*
피시 강제 종료
System.Diagnostics.Process.Start("shutdown.exe", "-s -f /t 60");      //-t 초 즉 60초 뒤에 PC 종료...
피시 종료 카운트다운 때 아래 명령을 날리면 종료가 취소됨
System.Diagnostics.Process.Start("shutdown.exe", "-a");
피시 재시작
System.Diagnostics.Process.Start("shutdown.exe", "-r");
피시 로그오프
System.Diagnostics.Process.Start("shutdown.exe", "-l");
 
*/

 

 

*예제 결과

 

 

 

https://kdsoft-zeros.tistory.com/111

 

[C#] 시간 체크 (Time Check)

* C# 시간 체크 (Time Check) 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;..

kdsoft-zeros.tistory.com

 

반응형
반응형

프로젝트 리소스 (Resources) 에 추가된 이미지 불러오기

소스 코드 상
picSound.Image = My.Resources.number_0_off



반응형
반응형

 

프로젝트 리소스 (Resources) 에 추가된 이미지 불러오기

소스 코드 상
picSound.Image = Properties.Resources.soundOFF;

반응형

+ Recent posts