using System; using System.Text; using System.Runtime.InteropServices; using System.Drawing.Printing; namespace WindowsFormsAppPrinterCLCS { [Guid(GetPrinter.ClassId)] public class GetPrinter { // COM用のGUID値 public const string ClassId = "F0C14F0A-20B3-4716-AF7E-21458B1219D4"; //SendMessage(データ転送) [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); //SendMessage(データ転送) [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam); const int CB_GETLBTEXT = 0x0148; const int CB_GETCOUNT = 0x0146; const int CB_GETLBTEXTLEN = 0x0149; public string GetSource(string pn) { string lineAll = ""; PrintDocument printDoc = new PrintDocument(); PaperSource pkSource = new PaperSource(); printDoc.PrinterSettings.PrinterName = pn; for (int i = 0; i <= printDoc.PrinterSettings.PaperSources.Count - 1; i++) { pkSource = printDoc.PrinterSettings.PaperSources[i]; lineAll += "," + i + ":" + pkSource.Kind + ":"+ pkSource.RawKind + ":" + pkSource.SourceName + Environment.NewLine; } return lineAll; } public string GetPaperHeightWidth(string printername,string customname) { string lineAll = ""; PrinterSettings settings = new PrinterSettings(); foreach (PaperSize size in settings.PaperSizes) { lineAll += "," + size.Kind + ":" + size.RawKind + ":" + size.PaperName + ":" + size.Height + ":" + size.Width + Environment.NewLine; } return lineAll; } public string GetComboText(int cbHandle, int i) { //https://bytes.com/topic/c-sharp/answers/831658-getting-setting-edititem-combobox-using-sendmessage StringBuilder ssb = new StringBuilder(256, 256); SendMessage(cbHandle, CB_GETLBTEXT, i, ssb); return ssb.ToString(); } public int GetComboCount(int cbHandle) { int CBcount = SendMessage(cbHandle, CB_GETCOUNT, 0, 0); return CBcount; } } }