呼叫計算器並返回結果的例子

2021-04-15 08:30:54 字數 2511 閱讀 4136

api呼叫計算器的例子,實現不同程式的資料交換。此方法同樣適用其他office元件的呼叫(可能要稍作修改)

基本原理:尋找計算器的edit控制代碼,用sendmessage返回結果,並不算複雜。

模組**:

private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as string, byval lpwindowname as string) as long

private declare function findwindowex lib "user32" alias "findwindowexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long

private declare function setwindowtext lib "user32" alias "setwindowtexta" (byval hwnd as long, byval lpstring as string) as long

private declare function sendmessage lib "user32" alias "sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) as long

private const wm_gettext = &hd

private const wm_gettextlength = &he

private const wm_settext = &hc

'下面主要是檢測計算器是否關閉

do ' 外層迴圈。

do while edithwnd <> 0 ' 內層迴圈。

calchwnd = findwindow("scicalc", vbnullstring)

edithwnd = findwindowex(calchwnd, 0, "edit", vbnullstring)

'判斷計算器是否關閉

if edithwnd = 0 then   ' 如果條件為 true...

check = false      ' 將標誌值設定為 false。

exit do            ' 終止內層迴圈。

else

'取計算器的值

edittext = space(sendmessage(edithwnd, wm_gettextlength, byval 0, byval 0))

sendmessage edithwnd, wm_gettext, byval len(edittext) + 1, byval edittext

'臨時賦給變數

presult = edittext

doevents     '這個起延時作用,否則不能正確返回數值

'判斷  presult的值是否為空,代表是否關閉計算器

if len(trim(presult)) <> 0 then

if right((presult), 1) = "." then

presult = mid(presult, len(presult) - 1)

end if

dblcal = ccur(presult)

end if

end if

loop

loop until check = false ' 立即終止外層迴圈

'控制項賦值並按要求四捨五入

' dblcal = ccur(dblcal)

ctl.value = roundtolarger(dblcal, intdecimals)

end function

public function roundtolarger(dblinput as currency, intdecimals as integer) as currency   '四捨五入

dim strformatstring as string

if dblinput <> 0 then

strformatstring = "#." & string(intdecimals, "#")

roundtolarger = format(dblinput, strformatstring)

else

roundtolarger = 0

end if

end function

窗體**:

private sub command0_click()

txtvalfrmcal me.text1, 3

end sub

private sub command5_click()

txtvalfrmcal me.text3, 1

end sub

private sub command8_click()

txtvalfrmcal me.text6, 2

end sub

VBA 通過API函式,呼叫計算器,模擬按鍵等操作

public declare sub sleep lib kernel32 byval dwmilliseconds as long public declare function findwindow lib user32 alias findwindowa byval lpclassname a...

c 資料結構 棧的應用 計算器

新年第一次發,這幾天在複習期末考試。但是好像已經複習完了但是還有十天多才考試感覺挺無聊的就寫個計算器。但是 太難看。裡一堆的if else。我還是學好catch throw吧!先貼出來。看看再修修補補,好好弄弄結構。買了明晚星戰的票。那今晚要不要出去high呢。pragma once ifndef ...

棧的應用 計算器 加,減,乘,除,括號

最初接觸此類的題目,是只涉及到的加減乘除四則運算,沒有符號的操作,因為在一次筆試中遇到了有括號的情況,抱著遇到問題一定要去解決的態度,筆試結束後嘗試將這道題目進行還原。這裡的思想主要是用到了棧,分為兩個棧,乙個棧存放數字,另外乙個棧存放運算子和括號 include include using nam...