1) 핸들을 알았을떄에, 해당frm에 메세지보내기
2) 핸들은 모르지만, 타이틀을 알았을때에 해당frm에 메세지보내기

using NoName;
using NoName.Text;//Encoding
using NoName.ComponentModel;//BackgroundWorker
using NoName.Timers;
using NoName.Windows.Forms;
using NoName.Runtime.InteropServices;

class MessageHandleCls
{
public int WM_COPYDATA=0x004A;
/*--------------------------------------------------------------------------------------------------*/
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int unMsg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/*--------------------------------------------------------------------------------------------------*/

IntPtr ____handle;

public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}

NoName.Timers.Timer tm = new NoName.Timers.Timer();
NoName.Timers.Timer tmmmm = new NoName.Timers.Timer();
Random rr = new Random();

public void running(IntPtr ____hwnd)
{
____handle=____hwnd;

tm.Elapsed += new ElapsedEventHandler(____time_tick);
tm.Interval = 1000;
tm.Start();

tmmmm.Elapsed += new ElapsedEventHandler(____time_tick_mmm);
tmmmm.Interval = 1000;
tmmmm.Start();
}
void ____time_tick(object sender, ElapsedEventArgs e)
{
int index=0;
int xpos;
string formatText="";

xpos = rr.Next() % 5;

if(xpos % 5 == 0) formatText="RIGHT";
else if(xpos % 5 == 1) formatText="LEFT";
else if(xpos % 5 == 2) formatText="DOWN";
else if(xpos % 5 == 3) formatText="RETURN";
else if(xpos % 5 == 4) formatText="SPACE";

byte[] dataByte=Encoding.UTF8.GetBytes(formatText);

COPYDATASTRUCT cpyData=new COPYDATASTRUCT();
cpyData.dwData=(IntPtr)0;
cpyData.cbData=formatText.Length;
cpyData.lpData=Marshal.AllocHGlobal(dataByte.Length);
Marshal.Copy(dataByte,0,cpyData.lpData,dataByte.Length);

IntPtr sndData=Marshal.AllocHGlobal(Marshal.SizeOf(cpyData));
Marshal.StructureToPtr(cpyData,sndData,true);

Console.WriteLine("Time:[" + DateTime.Now.ToString() + "]::" + ">>>>>>>>>>>SendStr:[" + sndData + "]");

IntPtr ____result=SendMessage(____handle, WM_COPYDATA, (IntPtr)index, sndData);

Marshal.FreeHGlobal(cpyData.lpData);
Marshal.FreeHGlobal(sndData);
}
void ____time_tick_mmm(object sender, ElapsedEventArgs e)
{
int index=0;
int xpos;
string formatText="";
IntPtr ____handle_mmm;

____handle_mmm = FindWindow(null, "DEBUGGING");

xpos = rr.Next() % 5;

if(xpos % 5 == 0) formatText="RIGHT";
else if(xpos % 5 == 1) formatText="LEFT";
else if(xpos % 5 == 2) formatText="DOWN";
else if(xpos % 5 == 3) formatText="RETURN";
else if(xpos % 5 == 4) formatText="SPACE";

byte[] dataByte=Encoding.UTF8.GetBytes(formatText);

COPYDATASTRUCT cpyData=new COPYDATASTRUCT();
cpyData.dwData=(IntPtr)0;
cpyData.cbData=formatText.Length;
cpyData.lpData=Marshal.AllocHGlobal(dataByte.Length);
Marshal.Copy(dataByte,0,cpyData.lpData,dataByte.Length);

IntPtr sndData=Marshal.AllocHGlobal(Marshal.SizeOf(cpyData));
Marshal.StructureToPtr(cpyData,sndData,true);

Console.WriteLine("Time:[" + DateTime.Now.ToString() + "]::" + ">>>>>>>>>>>SendStrTmp:[" + sndData + "]");

IntPtr ____result=SendMessage(____handle_mmm, WM_COPYDATA, (IntPtr)index, sndData);

Marshal.FreeHGlobal(cpyData.lpData);
Marshal.FreeHGlobal(sndData);
}
}

class WindowsManageCls : Form
{
const int WM_COPYDATA=0x004A;

public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}


MessageHandleCls nm = new MessageHandleCls();

public WindowsManageCls()
{
this.Text = "DEBUGGING";

nm.running(this.Handle);
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x0100, WM_SYSKEYDOWN = 0x0104;

if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
         switch (keyData.ToString())
         {
case "Return" :
DialogResult dialogResult = MessageBox.Show("Quit OK!!", "Inform",
              MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

if (dialogResult == DialogResult.OK) Application.Exit();
break;
case "Right"  :
break;
case "Left"   :
break;
         default:
             break;
         }
     }
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

try
{
switch(m.Msg)
{
case WM_COPYDATA :
COPYDATASTRUCT cds=(COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
byte[] rcvData=new byte[cds.cbData];
Marshal.Copy(cds.lpData,rcvData,0,cds.cbData);
String strText=Encoding.UTF8.GetString(rcvData);
Console.WriteLine("Time:[" + DateTime.Now.ToString() + "]::" + "ReceiveStr:[" + strText + "]");
break;
default:
break;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

class Program
{
public static void Main(string[] args)
{
Application.Run(new WindowsManageCls());
}
}






ex40.cs
0.01MB

//NoName->System 으로 변경//NoName->System 으로 변경//NoName->System 으로 변경//NoName->System 으로 변경//NoName->System 으로 변경
using NoName; 
using NoName.ComponentModel;
using NoName.Windows.Forms;
using NoName.Drawing;
using NoName.Drawing.Drawing2D;
using NoName.Timers;

class Program
{
public static void Main(string[] args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);


if(args.Length==1) 
{
if(args[0]=="E") Application.Run(new OtherManageCls());
}
}
}

class OtherManageCls : Form
{
int ____hatchstyle=0;
const int WM_KEYDOWN = 0x0100, WM_KEYUP = 0x0101, WM_CHAR = 0x0102, WM_SYSKEYDOWN = 0x0104, WM_SYSKEYUP = 0x0105, WM_PAINT = 0x000f, WM_SIZE = 0x0005;

public OtherManageCls()
{
this.Width = 700;
this.Height = 300;
}
void draw()
{
int xx,yy,width,height;

xx = this.Width / 4;
yy = this.Height / 4;
width = this.Width / 4 * 2;
height = this.Height / 4;

DrawBackground(0,0,this.Width,this.Height,____hatchstyle);
DrawForeground(xx,yy,width,height,____hatchstyle);

this.Text = "Time:[" + DateTime.Now.ToString() + "]" + ">>HatchStyle Number:[" + ____hatchstyle.ToString("00") + "]";
}
void DrawForeground(int xx, int yy, int width, int height, int ____hatchstyle)
{
HatchStyle hs = (HatchStyle)____hatchstyle;
Brush myBrush = new HatchBrush(hs, NoName.Drawing.Color.SteelBlue);
Pen myPen = new Pen(NoName.Drawing.Color.LightGray, 1);

NoName.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(xx, yy, width, height));
formGraphics.DrawRectangle(myPen , xx, yy, width, height);
}
void DrawBackground(int xx, int yy, int width, int height, int ____hatchstyle)
{
HatchStyle hs = (HatchStyle)____hatchstyle;
Brush myBrush = new HatchBrush(hs, NoName.Drawing.Color.LightYellow);
Pen myPen = new Pen(NoName.Drawing.Color.LightGray, 1);

NoName.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(xx, yy, width, height));
formGraphics.DrawRectangle(myPen , xx, yy, width, height);
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData.ToString())
{
case "Return" :
DialogResult dialogResult = MessageBox.Show("Quit OK!!", "Inform",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

if (dialogResult == DialogResult.OK) Application.Exit();
break;
case "Right"  :
____hatchstyle += 1;
if(____hatchstyle == 53) ____hatchstyle=0;
break;
case "Left"   :
____hatchstyle -= 1;
if(____hatchstyle == -1) ____hatchstyle=52;
break;
default:
break;
}

draw();
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

try
{
switch(m.Msg)
{
case WM_PAINT :
draw();
break;
default:
break;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

'c# 언어 > 중급과정' 카테고리의 다른 글

Data communication between Proesses!!  (0) 2022.03.25
SendMessage/FindWindow/Snd/Rcv Str  (0) 2022.03.15
HatchStyle,HatchBrush,Pen  (0) 2022.03.08
Hexa 4가지 항목으로 점수계산 로직  (0) 2022.03.06
LINQ 쿼리 소개(C#)  (0) 2022.02.23

using System.Drawing;
using System.Drawing.Drawing2D;

void drawforeground(int xx,int yy,int width,int height,int ____hatchstyle)
{
HatchStyle hs = (HatchStyle)____hatchstyle;//0-52까지 가능
Brush myBrush = new HatchBrush(hs, System.Drawing.Color.SteelBlue);
Pen myPen = new Pen(System.Drawing.Color.LightGray, 1);

System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(xx, yy, width, height));
formGraphics.DrawRectangle(myPen , xx, yy, width, height);
}
void drawforeground(int xx,int yy,int width,int height,int ____hatchstyle)
{
HatchStyle hs = (HatchStyle)____hatchstyle;//0-52까지 가능
Brush myBrush = new HatchBrush(hs, System.Drawing.Color.LightYellow);
Pen myPen = new Pen(System.Drawing.Color.LightGray, 1);

System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(xx, yy, width, height));
formGraphics.DrawRectangle(myPen , xx, yy, width, height);
}

ex31.cs
0.00MB
ex11.cs
0.02MB

 

1. Hexa,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

1. Horizon Direction
2. Vertical Direction
3. LeftToRight Direction
4. LightToLeft Direction

2. Tettris,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

1. Horizon Direction

파일첨부

frm_common.cs
0.02MB




LINQ 쿼리 소개(C#)

쿼리 는 데이터 소스에서 데이터를 검색하는 식입니다. 
쿼리는 일반적으로 특수화된 쿼리 언어로 표현됩니다. 
관계형 데이터베이스에는 SQL이 사용되고 XML에는 XQuery가 사용되는 것처럼 시간에 따라 다양한 형식의 데이터 소스에 대해 서로 다른 언어가 개발되었습니다. 
따라서 개발자는 지원해야 하는 데이터 소스의 형식이나 데이터 형식에 따라 새로운 쿼리 언어를 배워야 했습니다. 
LINQ는 다양한 데이터 소스 및 형식에 사용할 수 있는 일관된 모델을 제공함으로써 이러한 상황을 단순화합니다. 
LINQ 쿼리에서는 항상 개체를 사용합니다. 
XML 문서, SQL 데이터베이스, ADO.NET 데이터 세트, .NET 컬렉션 및 LINQ 공급자를 사용할 수 있는 다른 모든 형식에서 데이터를 쿼리하고 변환하는 데 동일한 기본 코딩 패턴을 사용합니다.

쿼리 작업의 세 부분
모든 LINQ 쿼리 작업은 다음과 같은 세 가지 고유한 작업으로 구성됩니다.

1. 데이터 소스 가져오기.(Data Source)
2. 쿼리 만들기.(Query)
3. 쿼리 실행.(Query Execution)


ex)

using System.Linq;
using System;

class IntroToLINQ
{
    static void Main()
    {
        // The Three Parts of a LINQ Query:
        // 1. Data source.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQuery)
        {
            Console.Write("{0,1} ", num);
        }
    }
}

 

/*execution display
D:\tmp\console>ex200
0 2 4 6
D:\tmp\console>
D:\tmp\console>
D:\tmp\console>
*/


설명) Form에 테트리스게임이 수행되고 인는중이라도, 백그라운드로 쓰레드 2개가 현재 동작하는 모습을 나타냅니다.
여러모로 실전활용가능한 모델입니다.

1. Form Activate & Other Statistic Possible(Threading Background)
2. BackgroundWorker
3. Form Drawing
4. Data set,get
5. Tris
6. Form - WM_PAINT Logic

basic !!

ex11.cs
0.01MB

Background class
Tris class divide!!!

ex12.cs
0.01MB

static class ----> #define instead!!

ex13.cs
0.01MB

design pattern various!!

ex14.cs
0.01MB

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;
using System.Runtime.InteropServices;


class Program
{
public static void Main(string[] args)
{
ContinueNoLoginCls nm = new ContinueNoLoginCls();
nm.running();
}
}

class ContinueNoLoginCls
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(int hWnd1, int hWnd2, string lpClassName, string lpWindowName);

const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const uint WM_CHAR = 0x0102;
const int VK_TAB = 0x09;
const int VK_ENTER = 0x0D;
const int VK_UP = 0x26;
const int VK_DOWN = 0x28;
const int VK_RIGHT = 0x27;
const int VK_F5 = 0x74;
const int VK_F6 = 0x75;
 
//According to SPY++ in IE

//ENTER key is
// P KEYDOWN
// P CHAR
// S CHAR
// P KEY_UP

//TAB key is
// P KEYDOWN
// P KEYUP

//DOWN, UP, RIGHT, LEFT is
// P KEYDOWN
// S KEYDOWN
// P KEYUP

//Letters is
// P KEYDOWN
// S KEYDOWN
// P CHAR
// S CHAR
// P KEYUP

IntPtr tailHandle = IntPtr.Zero;
IntPtr Handle_1 = IntPtr.Zero;
IntPtr Handle_2 = IntPtr.Zero;
//const string captionmsg="파일 탐색기";
//const string captionmsg="명령 프롬프트";
const string captionmsg="네이버 부동산 - Microsoft Edge";

public void running()
{
Console.Title = "";
tailHandle = FindWindow(null, captionmsg);

ConsoleKeyInfo keyinfo;

while(true)
{
keyinfo=Console.ReadKey(true);

debug(">>>>KEY:[" + keyinfo.Key.ToString() + "]");

if(keyinfo.Key==ConsoleKey.Enter)
{
debug(">>>>SendTab() method call!!");
SendTab();
}
else if(keyinfo.Key==ConsoleKey.RightArrow)
{
debug(">>>>SendArrowKey() method call!!");
SendChar(VK_F5);
}
else if(keyinfo.Key==ConsoleKey.Spacebar)
{
debug(">>>>SendArrowKey() method call!!");
SendArrowKey(VK_F5);
}

else if(keyinfo.Key==ConsoleKey.Escape)
{
debug(">>>>Quit() method call!!");
break;
}
}
}

void debug(string str)
{
Console.WriteLine(str);
}

    private void SendEnter()
    {
        PostMessage(tailHandle, WM_KEYDOWN, (IntPtr)VK_ENTER, IntPtr.Zero);
        PostMessage(tailHandle, WM_CHAR, (IntPtr)VK_ENTER, IntPtr.Zero);
        SendMessage(tailHandle, WM_CHAR, (IntPtr)VK_ENTER, IntPtr.Zero);
        PostMessage(tailHandle, WM_KEYUP, (IntPtr)VK_ENTER, IntPtr.Zero);
    }

    private void SendTab()
    {
        PostMessage(tailHandle, WM_KEYDOWN, (IntPtr)VK_TAB, IntPtr.Zero);
        PostMessage(tailHandle, WM_KEYUP, (IntPtr)VK_TAB, IntPtr.Zero);
    }

    private void SendArrowKey(int key)
    {
        PostMessage(tailHandle, WM_KEYDOWN, (IntPtr)key, IntPtr.Zero);
        SendMessage(tailHandle, WM_KEYDOWN, (IntPtr)key, IntPtr.Zero);
        PostMessage(tailHandle, WM_KEYUP, (IntPtr)key, IntPtr.Zero);
    }

    private void SendChar(int key)
    {
        //Keydown wParam values are 0x020 less than WM_CHAR wParam
        PostMessage(tailHandle, WM_KEYDOWN, (IntPtr)(key - 0x020), IntPtr.Zero);
        SendMessage(tailHandle, WM_KEYDOWN, (IntPtr)(key - 0x020), IntPtr.Zero);
        PostMessage(tailHandle, WM_CHAR, (IntPtr)key, IntPtr.Zero);
        SendMessage(tailHandle, WM_CHAR, (IntPtr)key, IntPtr.Zero);
        PostMessage(tailHandle, WM_KEYUP, (IntPtr)(key - 0x020), IntPtr.Zero);
    }
}



Assembly.LoadFrom() : DLL 활용하여, Form을 띄우기
/*----------------------------------------------------------------------
Description : 클래스라이브러리로 생성되어진 폼을 띄울때에 사용되어진다.(폼형태만 가지고 인는것을 폼처럼 만들어서 띄울수있다.)
ex) 
1. frm's name : UAPDBMQ0010
2. dll's name : DBM.DLL
----------------------------------------------------------------------*/

sample code>>

string formName = "UAPDBMQ0010";
string dllName = formName.Substring(3,3); //DBM.DLL
string assembly = dllName + ".DLL";
string classname = dllName + "." + formName;

Assembly asm = Assembly.LoadFrom(assembly);
object o = asm.CreateInstance(classname);
var doc = (Form)o;

Form frm = doc;
frm.Tag = assembly;
if(frm == null) return null;

frm.MdiParent = this; //MDI CASE
frm.FormBorderStyle = FormBorderStyle.None; //NEW FORM
frm.Dock = DockStyle.Fill //POPUP, WHEN DELETE

frm.Text="";
frm.TopLevel=false;
frm.Parent=this;
frm.Show();

+ Recent posts