開啟並啟用已存在程式視窗

2021-04-01 02:27:26 字數 2087 閱讀 6603

在實際的應用程式開發中,我們可能會在程式裡查詢另乙個程式是否已開啟,如果查詢的程式已開啟就把它啟用成當前的視窗。這裡分別用c#.***和vb.***兩種程式來實現了這個功能,這兩個程式裡都可以開啟乙個標題是"我的電腦"的視窗:

-------------------c#.***的實現---------------

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

private

static

extern

bool openicon(intptr hwnd);

private

static

extern

bool openicon(intptr hwnd);

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

private

static

extern

bool isiconic(intptr hwnd);

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

private

static

extern

int setforegroundwindow(intptr hwnd);

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

private

static

extern intptr findwindow(string strclassname, string strwindowname);

void

findandopenwindow(string title)

else}}

private

void button1_click(object sender, system.eventargs e)

-------------------vb.***的實現---------------

private

declare

function openicon lib "user32.dll" (byval hwnd as intptr) as

boolean

private

declare

function isiconic lib "user32.dll" (byval hwnd as intptr) as

boolean

private

declare

function setforegroundwindow lib "user32.dll" (byval hwnd as intptr) as

integer

private

declare

function findwindow lib "user32.dll" alias "findwindowa" (byval strclassname as

string, byval strwindowname as

string) as intptr

sub findandopenwindow(byval title as

string)

dim hwnd as intptr = findwindow(nothing, title)

ifnot hwnd.equals(intptr.zero) then

dim isicon as

boolean = isiconic(hwnd)

ifnot isicon then

setforegroundwindow(hwnd)

else

openicon(hwnd)

endif

endif

endsub

private

sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

findandopenwindow("我的電腦")

endsub

如何避免重複開啟已存在的窗體

通過按鈕事件定義了乙個開視窗的操作,每次點選按鈕就會彈出新視窗,但是多次點選會出現多個相同的視窗,這樣的對於之後的處理不方便,也不利於使用者的使用體驗,所以就需要在點選按鈕時判斷窗體是否已經存在,存在則啟用,不存在則新建 在按鈕點選事件中定義如下 boolean flag false 判斷標誌 fo...

開啟新視窗並輸出內容

一 介紹 開啟新視窗並輸出內容可以使用open 方法和close 方法來實現。open 方法 該方法用來開啟文件輸出流,並接收write 方法或writeln 方法的輸出,此方法可以不指定引數。語法 obj document.open url name obj 用來儲存open 方法返回的值,obj...

JS開啟新視窗並居中顯示

hml部分 onclick opennewwindow js部分 功能 開啟新得瀏覽器視窗並居中顯示 引數 url 需要開啟的url位址 name 開啟的視窗名稱,可以為null customwidth 視窗的寬度 customheight 視窗的高度 function opennewwindow ...