c 常見操作

2022-02-27 01:30:13 字數 3082 閱讀 8472

1. streamwriter - 檔案寫入類

streamwriter s = new streamwriter(address + "/menu.ini", true);

s.writeline(openfiledialog1.filename);

s.flush();

s.close();

2. streamreader - 檔案讀取類

streamreader sr = new streamreader(address + "/menu.ini");

while (sr.peek()>=0)

sr.close();

3. image - 影象類

image p = image.fromfile("/背景.jpg");

form f = new form(); // 建立mid視窗

f.mdiparent = this; // 設定父視窗

f.backgroundimage = p; // 設定mdi視窗的背景圖

f.show(); // 顯示mdi視窗

4. bitmap - 點陣圖類

// 建立位圖, bitmap類繼承於image類

bitmap bit;

bit = new bitmap("heart.bmp");

bit.maketransparent(color.white); // 設定透明色

protected override void onpaint(painteventargs e)

5. this.opacity - 控制項的不透明度

// 控制控制項透明程度,很有用。

6. c#中匯入dll檔案中的api

[system.runtime.interopservices.dllimportattribute("user32.dll")]

public static extern bool flashwindow(intptr handle, bool binvert);

7. 隱藏標題欄

this.controlbox = false;

8. 視窗始終處於最上面

this.topmost = ture;

9. screen - 桌面類

screen.primaryscreen.workingarea.height // 桌面的高

screen.primaryscreen.workingarea.width // 桌面的寬

screen.primaryscreen.bitsperpixel // 桌面的位深

10. 基本繪圖

graphics graphics;

pen mypen = new pen(color.blue, 2);

// 畫線

graphics = this.creategraphics();

graphics.drawline(mypen, 30, 60, 150, 60);

// 畫矩形

graphics = this.creategraphics();

graphics.drawrectangle(mypen, 30, 80, 120, 50);

// 畫橢圓

graphics = this.creategraphics();

rectangle myrectangle = new rectangle(160, 70, 100, 60);

graphics.drawellipse(mypen, myrectangle);

11. 獲得滑鼠在視窗中的座標

cursor.clip = new rectangle(this.location, this.size);

label1.text = "當前滑鼠的位置為:" + cursor.position;

12. 判斷鍵盤

protected override bool processcmdkey(ref message msg, keys keydata)

messagebox.show(strinfo, "資訊", messageboxbuttons.ok, messageboxicon.information);

}return base.processcmdkey(ref msg, keydata);

}13. 控制遠端計算機

//首先新增對 system.management的引用

private void closecomputer(string strname,string strpwd,string ip,string doinfo)

;mobj.invokemethod(doinfo, str);

}messagebox.show("操作成功");

}catch(exception ey)

}// 重啟遠端計算機

closecomputer(this.textbox2.text, this.textbox3.text, this.textbox1.text, "reboot");

// 關閉遠端計算機

closecomputer(this.textbox2.text, this.textbox3.text, this.textbox1.text, "shutdown");

14. ping的使用

ping pinginfo = new ping();

pingoptions pingopt = new pingoptions();

pingopt.dontfragment = true;

string myinfo = "hyworkhyworkhyworkhyworkhyworkhywork";

byte bufferinfo = encoding.ascii.getbytes(myinfo);

int timeout = 120;

pingreply reply = pinginfo.send(this.textbox1.text, timeout, bufferinfo, pingopt);

if (reply.status == ipstatus.success)

else

15. 檢查檔案是否存在

public int checkfileexit(string objfilepath)

c 流常見操作

記下輸入輸出流 檔案流 字串流的常見操作。vectorvclines wstring instr while getline wcin,instr 需要引入 include 預設控制台是ansi編碼,要輸出對應寬字元,需要制定locale來保證輸出時完成對應的編碼轉換,引入 include 並在開始...

c 常見容器操作

總結下常見c 容器操作。vector是陣列,list 是雙端鍊錶,deque時雙向佇列。分別引入vector list deque庫。vector deque支援隨機訪問和迭代器偏移,list deque支援雙向訪問,list只能自加自減。增 push back insert push front ...

c 常見容器操作

總結下常見c 容器操作。1.vector list和 deque vector是陣列,list 是雙端鍊錶,deque時雙向佇列。分別引入vector list deque庫。vector deque支援隨機訪問和迭代器偏移,list deque支援雙向訪問,list只能自加自減。增push bac...