2012-11-10 3 views
0

이 코드는 양식을 항상 맨 위에두고 투명하게 만듭니다.그래픽 그리기가 작동하지 않습니다.

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

    namespace HyperBox 
    { 

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 

     this.TopMost = true; // make the form always on top 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border 
     this.WindowState = FormWindowState.Maximized; // maximized 
     this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized 
     this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized 
     this.TransparencyKey = this.BackColor = Color.Red; // the color key to transparent, choose a color that you don't use 

     // Set the form click-through 
     int initialStyle = GetWindowLong(this.Handle, -20); 
     SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20); 
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] 
    static extern int GetWindowLong(IntPtr hWnd, int nIndex); 





    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 


    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); 


    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern int SetParent(int hWndChild, int hWndNewParent); 


    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern IntPtr FindWindow(
    [MarshalAs(UnmanagedType.LPTStr)] string lpClassName, 
    [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName); 
    [DllImport("user32.dll")] 
    public static extern IntPtr SetParent(
    IntPtr hWndChild,  // handle to window 
    IntPtr hWndNewParent // new parent window 
    ); 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     // draw what you want 
     e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width/2) - 4, (SystemInformation.WorkingArea.Height/2) - 20, 8, 40)); 
     e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width/2) - 20, (SystemInformation.WorkingArea.Height/2) - 4, 40, 8)); 


    } 
    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 

    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

     IntPtr hwndf = this.Handle; 
     IntPtr hwndParent = FindWindow("chrome.exe", null); 
     SetParent(hwndf, hwndParent); 



    } 

} 
    } 

문제는 내가 그래픽을 그릴 때 아무 것도 그리지 않는다는 것입니다. 좌표가 약 100 ~이면 작동합니다. 그러나 위의 방법을 수행하면 아무 일도 일어나지 않습니다. 픽셀조차도 아닙니다. 누군가가 왜 이런 일이 일어 났는지 설명하고 고정 스 니펫을 다시 게시 해 주시겠습니까? 감사합니다. Layne

+1

투명도 색상이 '빨강'이며 '빨강'으로 그리는 중입니다. 다른 색상으로 시도해 보셨습니까? – ja72

+1

@ ja72 TransparencyKey는 양식 및 컨트롤의 BackColors에만 사용됩니다. 이것이 그가 때때로 그의 직사각형의 일부를 볼 수있는 이유입니다. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey(v=vs.100).aspx – TyCobb

답변

1

OnPaint은 화면이 아닌 양식의 그래픽 개체를 제공합니다. 양식이 아닌 시스템의 작업 영역을 기준으로 직사각형을 채 웁니다. 직사각형 좌표를 조정하고 그래픽을 표시 할 위치에 양식을 배치해야합니다. (0, 0) 위치의 사각형은 양식의 클라이언트 영역의 왼쪽 위 모서리입니다. 또한 기본 폼 클래스에 노출 된 ClientRectangle을 호출하여 해당 사각형에 액세스 할 수 있어야합니다.

양식 외부에서 그림을 그리는 데이 질문을보십시오. Draw/Paint Outside Form 양식에 그림을 그리지 않으려면 올바른 방향으로 시작해야하지만 위치를 조정하고 크기를 조정하는 것이 더 쉬울 것입니다. 필요에 따라 양식을 작성하십시오.

EDIT 문제를 디버깅하는 동안 최소한 테두리 종류를 추가하는 것이 좋습니다. 이렇게하면 양식의 위치와 모니터 상태를 확인할 수 있습니다. 그런 다음 OnPaint에서 포인트를 끊을 때 숫자를 확인하여 사각형을 올바르게 작성하는지 확인하지만 양식의 클라이언트 영역 내에 그림을 그려야 문제가 해결 될 수 있습니다.

관련 문제