2017-01-21 4 views
0

私はC#でウィンドウフォームを持っていますが、 "X"トップバーがなくても孤独なボタンのように見えます。私はそれを単なるボタンにしました。私はボタンを長くタッチすると、それを画面上で動かすことができます。長いクリック時に移動するウィンドウフォームの作成方法C#

私はそれを移動することができますが、私はボタンをタッチすると、私はそれを移動することができますしたいウィンドウのフォームの背景に触れることに成功しました。

インターネットで見た後、私はこのコードを作ったが、私はmoveApp()mehtodに何を書くべきか分からない。

これは私のコードです:

namespace Print_My_Screen 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent();  
     } 

     private void PrintScreen() 
     { 

     Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

     Graphics graphics = Graphics.FromImage(printscreen as Image); 

     graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); 

     string subPath = @"C:\PrintMyScreen"; 
     bool exists = System.IO.Directory.Exists(subPath); 

     if (!exists) 
      System.IO.Directory.CreateDirectory(subPath); 

     printscreen.Save(@"C:\PrintMyScreen\printscreen.png", ImageFormat.Png); 
     printIt(); 

     MessageBox.Show("Your screen has been printed successfully","",MessageBoxButtons.OK,MessageBoxIcon.Information); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
     PrintScreen(); 
     } 

     private void printIt() 
     { 
     PrintDocument pd = new PrintDocument(); 
     // pd.PrintPage += PrintPage; 
     pd.PrintPage += (sender, args) => 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\PrintMyScreen\printscreen.png"); 
      img.RotateFlip(RotateFlipType.Rotate90FlipXY); 
      args.Graphics.DrawImage(img, args.MarginBounds); 
     }; 

     pd.Print(); 
     } 

     private void PrintPage(object o, PrintPageEventArgs e) 
     { 
     System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\PrintMyScreen\printscreen.png"); 
     Point loc = new Point(100, 100); 

     e.Graphics.DrawImage(img, loc); 
     img.Dispose(); 
     } 
     protected override void WndProc(ref Message m) 
     { 
     switch (m.Msg) 
     { 
      case 0x84: 
       base.WndProc(ref m); 
       if ((int)m.Result == 0x1) 
        m.Result = (IntPtr)0x2; 
       return; 
     } 
     base.WndProc(ref m); 
     } 

     private void moveApp() 
     { 

     } 

     public void Repeater(Button btn, int interval) 
     { 
     var timer = new Timer { Interval = interval }; 
     timer.Tick += (sender, e) => moveApp(); 
     btn.MouseDown += (sender, e) => timer.Start(); 
     btn.MouseUp += (sender, e) => timer.Stop(); 
     btn.Disposed += (sender, e) => 
     { 
      timer.Stop(); 
      timer.Dispose(); 
     }; 
     } 
    } 
} 

答えて

0

あなたは多くのことを欠落していません。

フォームを移動するには、フォームのキャプションに左ボタンが押されているというメッセージをウィンドウに送信する必要があります。 これは、以下を達成することができる。

public const int WM_NCLBUTTONDOWN = 0xA1; 
    public const int HT_CAPTION = 0x2; 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern bool ReleaseCapture(); 

次に、あなたのmoveappでそれを使用します。

private void moveApp() 
    { 
     ReleaseCapture(); 
     SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
    } 

もう一つは、あなたのRepeaterWindows.Forms.Timerを使用しているので、あなたはすべてのmoveappを入力しますintervalミリ秒。 一度だけ入力するには、Stopが必要です。
ので、Repeaterは次のようになります。

public void Repeater(Button btn, int interval) 
    { 
    var timer = new Timer { Interval = interval }; 
    timer.Tick += (sender, e) => {timer.Stop();moveApp();} 
    btn.MouseDown += (sender, e) => timer.Start(); 
    btn.MouseUp += (sender, e) => timer.Stop(); 
    btn.Disposed += (sender, e) => 
    { 
     timer.Stop(); 
     timer.Dispose(); 
    }; 
    } 

あなたのコンストラクタでリピータを呼び出すことを忘れないでください...私はあなたがそれを行う表示されません。

関連する問題