C# x 指定檔案路徑 x 列印 x notifyIcon應用
Form裡面要有 :
Button *1
notifyicon *1
contextmenustrip *1
printdocument *1
/////////////////////以下 程式碼/////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
System.IO.StreamReader fileToPrint;
System.Drawing.Font printFont;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.Text = "常駐程式";
}
private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
notifyIcon1.ShowBalloonTip(3000);
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
}
else
{
this.notifyIcon1.Visible = false;
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void OpenTMS_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void ExitCMS_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
System.GC.Collect();
System.Environment.Exit(System.Environment.ExitCode);
}
private void button1_Click(object sender, EventArgs e)
{
fileToPrint = new System.IO.StreamReader(@"D:\test.txt");
printFont = new System.Drawing.Font("Arial", 10);
printDocument1.Print();
fileToPrint.Close();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = fileToPrint.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * printFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
}
}
}
/////////////////////以上 程式碼/////////////////////
執行畫面 :
參考資料 : https://dotblogs.com.tw/chou/archive/2009/02/25/7284.aspx
http://ms-net.blogspot.tw/2008/04/notifyicon-contextmenustrip.html
沒有留言:
張貼留言